Skip to content

SQLx attempts to read unrelated parent directory .env #4276

@gabe-lepo

Description

@gabe-lepo

I have found these related issues/pull requests

None

Description

On sqlx version 0.9.0 (not 0.8.6); sqlx::query_as!() seems to attempt to read the parent directory .env instead of identifying the CWD .env (and it's properly set DATABASE_URL value).

Error:

error: error reading dotenv file "~/sqlx-issue-parent/.env"
  --> src/main.rs:11:5
   |
11 | /     sqlx::query_as!(
12 | |         MyDataStruct,
13 | |         "INSERT INTO mytable (something, somewhere)
14 | |         VALUES ($1, $2)
...  |
17 | |         my_struct.location,
18 | |     );
   | |_____^
   |
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `rust-child-directory` (bin "rust-child-directory") due to 1 previous error

Example dir structure:

- sqlx-issue-parent/
  - .env
  - rust-child-directory/
    - .env
    - src
      - main.rs

Top-level .env contains some potentially invalid text like:

Top-level directory is non-rust (no Cargo.toml, cargo build does not get run here)

EMAIL_SENDER=someemail@somewhere.com
EMAIL_SENDER_PASSWORD=some space delimited password
SMTP_HOST=some.host.com

Child directory contains .env like:

We run cargo build here, sqlx should not care what the parent directory .env looks like

DATABASE_URL=postgres://postgres:pass@localhost:5432/tablename

Build error complains of error reading the .env from the parent directory. There is no need to read that env var file, the child directory contains the required DATABASE_URL value.

query_as!() call-site looks like:

fn main() {
    struct MyDataStruct {
        name: String,
        location: Option<String>,
    }
    let my_struct = MyDataStruct {
        name: String::from("Gabe"),
        location: Some(String::from("Here")),
    };

    sqlx::query_as!(
        MyDataStruct,
        "INSERT INTO mytable (someone, somewhere)
        VALUES ($1, $2)
        RETURNING *",
        my_struct.name,
        my_struct.location,
    );
}

Reproduction steps

  1. Create some arbitrary directory and place a potentially "invalid" .env (values containing spaces, dots, etc.) there
  2. Create a child directory within the parent
  3. cd <child-directory>;cargo init;cargo add sqlx --features postgres,runtime-tokio,uuid,time,macros
  4. Update rust source as outlined above
  5. cargo build from the child directory

Build error noting failure to read parent directory .env

Other notes:

  • Reverting to sqlx version 0.8.6 seems to solve the issue

SQLx version

0.9.0

Enabled SQLx features

["postgres", "runtime-tokio", "uuid", "time", "macros"]

Database server and version

PostgreSQL 18.4 on ARM (Debian)

Operating system

MacOS

Rust version

rustc 1.95.0 (59807616e 2026-04-14)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions