From 4ec06f48209c4a62172625f48187a25db6e67dd3 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 7 May 2023 10:01:43 -0700 Subject: [PATCH 1/2] Disallow RUSTUP_HOME in the [env] table. --- src/cargo/util/config/mod.rs | 9 +++++-- tests/testsuite/cargo_env_config.rs | 42 +++++++++++++++-------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 40102a62fc3..de8816f07b3 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1745,8 +1745,13 @@ impl Config { .env_config .try_borrow_with(|| self.get::("env"))?; - if env_config.get("CARGO_HOME").is_some() { - bail!("setting the `CARGO_HOME` environment variable is not supported in the `[env]` configuration table") + for disallowed in &["CARGO_HOME", "RUSTUP_HOME"] { + if env_config.contains_key(*disallowed) { + bail!( + "setting the `{disallowed}` environment variable is not supported \ + in the `[env]` configuration table" + ); + } } Ok(env_config) diff --git a/tests/testsuite/cargo_env_config.rs b/tests/testsuite/cargo_env_config.rs index 0b787bebd3c..938205d6443 100644 --- a/tests/testsuite/cargo_env_config.rs +++ b/tests/testsuite/cargo_env_config.rs @@ -59,29 +59,31 @@ fn env_invalid() { } #[cargo_test] -fn env_no_cargo_home() { +fn env_no_disallowed() { + // Checks for keys that are not allowed in the [env] table. let p = project() - .file("Cargo.toml", &basic_bin_manifest("foo")) - .file( - "src/main.rs", - r#" - fn main() { - } - "#, - ) - .file( - ".cargo/config", - r#" - [env] - CARGO_HOME = "/" - "#, - ) + .file("Cargo.toml", &basic_manifest("foo", "1.0.0")) + .file("src/lib.rs", "") .build(); - p.cargo("check") - .with_status(101) - .with_stderr_contains("[..]setting the `CARGO_HOME` environment variable is not supported in the `[env]` configuration table") - .run(); + for disallowed in &["CARGO_HOME", "RUSTUP_HOME"] { + p.change_file( + ".cargo/config", + &format!( + r#" + [env] + {disallowed} = "foo" + "# + ), + ); + p.cargo("check") + .with_status(101) + .with_stderr(&format!( + "[ERROR] setting the `{disallowed}` environment variable \ + is not supported in the `[env]` configuration table" + )) + .run(); + } } #[cargo_test] From 2fce10d0217a4182fafe37dd95484f04833339bc Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 7 May 2023 11:43:56 -0700 Subject: [PATCH 2/2] Add a comment describing why these are disallowed. --- src/cargo/util/config/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index de8816f07b3..7c386a2e238 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1745,6 +1745,20 @@ impl Config { .env_config .try_borrow_with(|| self.get::("env"))?; + // Reasons for disallowing these values: + // + // - CARGO_HOME: The initial call to cargo does not honor this value + // from the [env] table. Recursive calls to cargo would use the new + // value, possibly behaving differently from the outer cargo. + // + // - RUSTUP_HOME: Under normal usage with rustup, this will have no + // effect because the rustup proxy sets RUSTUP_HOME, and that would + // override the [env] table. If the outer cargo is executed directly + // circumventing the rustup proxy, then this would affect calls to + // rustc (assuming that is a proxy), which could potentially cause + // problems with cargo and rustc being from different toolchains. We + // consider this to be not a use case we would like to support, + // since it will likely cause problems or lead to confusion. for disallowed in &["CARGO_HOME", "RUSTUP_HOME"] { if env_config.contains_key(*disallowed) { bail!(