transact-rs/sqlx · error · syn::Error
paths relative to the current file's directory are not curre
Error message
paths relative to the current file's directory are not currently supported
What it means
Beyond rejecting absolute paths, sqlx macros also reject bare relative paths that point directly at a file (no parent directory component). Because the macro can only resolve relative to the manifest directory (rustc's `SourceFile::path()` relative resolution is unstable, see rust issue 54725), a single-component path like `"users.sql"` is ambiguous and unsupported.
Source
Thrown at sqlx-macros-core/src/common.rs:21
pub(crate) fn resolve_path(path: impl AsRef<Path>, err_span: Span) -> syn::Result<PathBuf> {
let path = path.as_ref();
if path.is_absolute() {
return Err(syn::Error::new(
err_span,
"absolute paths will only work on the current machine",
));
}
// requires `proc_macro::SourceFile::path()` to be stable
// https://github.com/rust-lang/rust/issues/54725
if path.is_relative()
&& path
.parent()
.is_none_or(|parent| parent.as_os_str().is_empty())
{
return Err(syn::Error::new(
err_span,
"paths relative to the current file's directory are not currently supported",
));
}
let mut out_path = crate::manifest_dir().map_err(|e| syn::Error::new(err_span, e))?;
out_path.push(path);
Ok(out_path)
}
View on GitHub (pinned to 03af8bcc57)
Solutions
- Prefix the path with a directory relative to the crate root, e.g. `src/queries/users.sql` or `./src/users.sql`
- Move the .sql file into a directory (e.g. `queries/`) and reference it as `queries/users.sql`
- Remember: paths resolve from the crate's manifest directory, not the current source file
Example fix
// before
let q = query_file!("users.sql");
// after
let q = query_file!("src/queries/users.sql"); Defensive patterns
Strategy: validation
Validate before calling
let p = std::path::Path::new(file_arg);
assert!(p.is_relative() && p.parent().map_or(false, |d| !d.as_os_str().is_empty()),
"query file path must include a directory component relative to the crate root"); Prevention
- Reference query files with at least one directory, e.g. src/queries/x.sql
- Keep .sql files in a dedicated subdirectory of the crate
- Remember paths resolve from the manifest dir, not the current .rs file
When it happens
Trigger: Writing `query_file!("users.sql")` or `query_file!("./users.sql")` — a relative path with no directory component — in any `*_file!` macro.
Common situations: Placing a .sql file next to the source file and referencing it by bare filename; assuming the macro resolves relative to the current .rs file; refactoring a nested path down to a bare filename.
Related errors
- absolute paths will only work on the current machine
- query file path cannot be represented as a string
- failed to read query file at {}: {}
- expected exactly 1 column, got {}
- Cannot use both flatten and json
AI-assisted analysis of transact-rs/sqlx@03af8bcc57 (2026-09-03).
Data as JSON: /api/errors/03d1fae8803620b7.
Report an issue: GitHub.