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
- Create some arbitrary directory and place a potentially "invalid"
.env (values containing spaces, dots, etc.) there
- Create a child directory within the parent
cd <child-directory>;cargo init;cargo add sqlx --features postgres,runtime-tokio,uuid,time,macros
- Update rust source as outlined above
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)
I have found these related issues/pull requests
None
Description
On sqlx version
0.9.0(not0.8.6);sqlx::query_as!()seems to attempt to read the parent directory.envinstead of identifying the CWD.env(and it's properly setDATABASE_URLvalue).Error:
Example dir structure:
Top-level
.envcontains some potentially invalid text like:Child directory contains
.envlike:Build error complains of error reading the
.envfrom the parent directory. There is no need to read that env var file, the child directory contains the requiredDATABASE_URLvalue.query_as!()call-site looks like:Reproduction steps
.env(values containing spaces, dots, etc.) therecd <child-directory>;cargo init;cargo add sqlx --features postgres,runtime-tokio,uuid,time,macroscargo buildfrom the child directoryBuild error noting failure to read parent directory
.envOther notes:
0.8.6seems to solve the issueSQLx 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)