Skip to content

SQLX_OFFLINE_DIR Not being respected during build #3961

@psionic-k

Description

@psionic-k

I have found these related issues/pull requests

https://github.com/launchbadge/sqlx/pull/3848/files

This looks like it contains a cause. My build was relying on environment variables set by Nix, not .env files being present. In the new version, I can't see where the variable is being read, only the .env, which I can't propagate in into the Crane build.

Quite sleepy so I might be telling lies. Going to crash. Thanks in advance.

Description

Behavior has regressed since 0.8.5. Working build became non-working.

I'm building within Nix and the behavior of this variable is critical because the cargo vendoring done in Crane will filter the .sqlx directory no matter what. Renaming to sqlx allows the queries through. I have observed that the sqlx folder is present where the command fails, but sqlx fails indicating that it does not believe prepared queries are available:

**error: builder for '/nix/store/xc93ccbc0p81d5r74v5rf8ndpjnkai3r-prizeforge-api-deps-0.1.0.drv' failed with exit code 101;
       last 25 log lines:
       >
       > error: set `DATABASE_URL` to use query macros online, or run `cargo sqlx prepare` to update the query cache
       >    --> /nix/store/k171hv5c2zzldkyvl0sjzgc5b46yb9x6-vendor-cargo-deps/cfaedbcdbce5b96bd0898f837f2c1c8ff348b251f88c2841bd7728c3c093809a/models-0.1.0/src/user.rs:278:9
       >     |
       > 278 | /         sqlx::query!(
       > 279 | |             r#"
       > 280 | |             DELETE FROM users WHERE user_id = $1;
       > 281 | |             "#,
       > 282 | |             user_id as _
       > 283 | |         ).execute(pool)
       >     | |_________^
       >     |
       >     = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)
       >**

Everything else I've checked seems consistent. The variable is set and was respected in 0.8.5, successfully finding the successfully included prepared queries. The variable is set and not respected in 0.8.6.

I can possibly create some workaround with the build command to move the file into place just before use.

Reproduction steps

  • prepare queries
  • build with Crane and nix, with the following expression:
          craneBuild = rec {
            args = {
              src = ./.;
              pname = name;
              inherit version;
              buildInputs = [ ];

              SQLX_OFFLINE = "true";
              SQLX_OFFLINE_DIR = "sqlx";
            };
            cargoArtifacts = craneLib.buildDepsOnly args;
            buildArgs = args // {
              inherit cargoArtifacts;
              doCheck = false;
              cargoBuildCommand = "cargo build --profile release -v --features prod";
            };
            package = craneLib.buildPackage buildArgs;
          };

SQLx version

0.8.6

Enabled SQLx features

"runtime-tokio-rustls", "any", "postgres", "chrono", "ipnetwork"

Database server and version

psql (17.5, server 16.3 (Debian 16.3-1.pgdg120+1))

Operating system

NixOS (Debian container serves postgres)

Rust version

1.88.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions