transact-rs/sqlx · error · syn::Error

absolute paths will only work on the current machine

Error message

absolute paths will only work on the current machine

What it means

sqlx macros resolve `file`/query paths relative to the crate manifest directory so macros work across machines. An absolute path (one starting with `/` or a drive letter) is rejected at compile time because it would only resolve on the machine where it was written and break builds elsewhere (CI, teammates, Docker).

Source

Thrown at sqlx-macros-core/src/common.rs:8

use proc_macro2::Span;
use std::path::{Path, PathBuf};

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",
        ));
    }

View on GitHub (pinned to 03af8bcc57)

Solutions

  1. Replace the absolute path with a path relative to the crate's Cargo.toml (manifest directory), e.g. `src/queries/users.sql`
  2. Use the `CARGO_MANIFEST_DIR` convention mentally: the macro resolves relative paths from there
  3. If generating code, emit relative paths instead of absolute ones

Example fix

// before
let q = query_file!("/home/alice/app/src/queries/get_user.sql");
// after
let q = query_file!("src/queries/get_user.sql");
Defensive patterns

Strategy: validation

Validate before calling

// check before invoking the macro path convention
let p = std::path::Path::new(file_arg);
assert!(!p.is_absolute(), "use a path relative to the crate manifest dir");

Prevention

When it happens

Trigger: Writing `query_file = "/home/me/project/src/queries/users.sql"` (or any absolute path) in `query!`, `query_file!`, `query_as!` etc., then compiling.

Common situations: Copy-pasting a path from an IDE's absolute-path copy; generated code that embeds absolute paths; moving code between machines or into CI where the absolute path is invalid anyway.

Related errors


AI-assisted analysis of transact-rs/sqlx@03af8bcc57 (2026-09-03). Data as JSON: /api/errors/a1f9d3eeb39c8921. Report an issue: GitHub.