-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Problem
I have an environment variable set using [env]
in $HOME/.cargo/config.toml
. For a particular project, I use a dedicated $CARGO_HOME
with its own config.toml
which sets that environment variable to a different value. However, I still observe that the value from my $HOME/.cargo/config.toml
is used.
Steps
$ cargo new bad-env-sourcing
Created binary (application) `bad-env-sourcing` package
$ cd bad-env-sourcing/
$ mkdir cargo-home
$ echo -e "[env.FOO]\nvalue='from-home'" >> $HOME/.cargo/config.toml
$ echo -e "[env.FOO]\nvalue='from-cargo-home'" > cargo-home/config.toml
$ cat src/main.rs
fn main() {
println!("run: {}", env!("FOO"));
}
$ cat build.rs
fn main() {
eprintln!("build-build: {}", env!("FOO"));
eprintln!("build-run: {}", std::env::var("FOO").unwrap());
println!("cargo:rerun-if-env-changed=FOO");
}
$ env CARGO_HOME=$PWD/cargo-home cargo run
Compiling bad-env-sourcing v0.1.0 (/local/home/jongje/bad-env-sourcing)
Finished dev [unoptimized + debuginfo] target(s) in 0.24s
Running `target/debug/bad-env-sourcing`
run: from-home
$ cat target/debug/**/stderr
build-build: from-home
build-run: from-home
We can confirm that both ~/.cargo/config.toml
and $CARGO_HOME/config.toml
are loaded by adding an envvar just to the latter and seeing that it's set for build scripts and main code alike:
$ echo -e "[env.BAR]\nvalue='from-cargo-home'" >> cargo-home/config.toml
$ cat src/main.rs
fn main() {
println!("run: {}", env!("FOO"));
println!("run BAR: {}", env!("BAR"));
}
$ cat build.rs
fn main() {
eprintln!("build-build: {}", env!("FOO"));
eprintln!("build-run: {}", std::env::var("FOO").unwrap());
println!("cargo:rerun-if-env-changed=FOO");
eprintln!("build-build BAR: {}", env!("BAR"));
eprintln!("build-run BAR: {}", std::env::var("BAR").unwrap());
println!("cargo:rerun-if-env-changed=BAR");
}
$ env CARGO_HOME=$PWD/cargo-home cargo run
Compiling bad-env-sourcing v0.1.0 (/local/home/jongje/bad-env-sourcing)
Finished dev [unoptimized + debuginfo] target(s) in 0.53s
Running `target/debug/bad-env-sourcing`
run: from-home
run BAR: from-cargo-home
$ cat target/debug/**/stderr
build-build: from-home
build-run: from-home
build-build BAR: from-cargo-home
build-run BAR: from-cargo-home
Possible Solution(s)
No response
Notes
When I build and run with a modified version of cargo
that prints Config
, I get, in part:
[src/bin/cargo/cli.rs:362] &config = Config {
home_path: Filesystem {
root: "/home/jongje/bad-env-sourcing/cargo-home",
},
...
env: {
...
"CARGO_HOME": "/home/jongje/bad-env-sourcing/cargo-home",
...
},
}
[src/cargo/core/compiler/compilation.rs:357] self.config.env_config()? = {
"FOO": EnvConfigValue {
inner: Value {
val: WithOptions {
value: "from-home",
force: false,
relative: false,
},
definition: Path(
"/local/home/jongje/.cargo/config.toml",
),
},
},
"BAR": EnvConfigValue {
inner: Value {
val: WithOptions {
value: "from-cargo-home",
force: false,
relative: false,
},
definition: Path(
"/home/jongje/bad-env-sourcing/cargo-home/config.toml",
),
},
},
}
I'm surprised that anything at all from $HOME/.cargo/config.toml
is used when $CARGO_HOME
is set. I would expect setting the latter to make Cargo forget all about $HOME/.cargo
. We should probably make that be the case, and ensure it's not just for [env]
.
Version
cargo 1.63.0 (fd9c4297c 2022-07-01)
release: 1.63.0
commit-hash: fd9c4297ccbee36d39e9a79067edab0b614edb5a
commit-date: 2022-07-01
host: aarch64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1n)
os: Amazon Linux AMI 2.0.0 [64-bit]