Skip to content

Commit 1490d10

Browse files
committed
Auto merge of #12101 - ehuss:disallow-rustup-home, r=weihanglo
Disallow RUSTUP_HOME in the [env] table. This adds a check to prevent RUSTUP_HOME from being set in the `[env]` config table under the same reasoning as was done in #11590. Cargo will likely behave incorrectly if this key is set in the config since it will not change the home used by the outer cargo itself. This is a breaking change, though I think it is unlikely to be used in practice. When cargo is executed via a rustup proxy, the proxy sets RUSTUP_HOME which overrides the `[env]` table entry. It may be feasible that someone is invoking cargo directly without the rustup wrappers, and then using this to steer the rustc invocations to a different rustup location, but I'm not sure that's a use case we need to be supporting. This is being added as a further assurance for #11590 to make sure the environment is configured as expected. We could potentially add other Rustup env vars to reject, but I'm not sure I want to delve into analyzing all the possible reasons or interactions for each one.
2 parents 27a41d6 + 2fce10d commit 1490d10

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

src/cargo/util/config/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1745,8 +1745,27 @@ impl Config {
17451745
.env_config
17461746
.try_borrow_with(|| self.get::<EnvConfig>("env"))?;
17471747

1748-
if env_config.get("CARGO_HOME").is_some() {
1749-
bail!("setting the `CARGO_HOME` environment variable is not supported in the `[env]` configuration table")
1748+
// Reasons for disallowing these values:
1749+
//
1750+
// - CARGO_HOME: The initial call to cargo does not honor this value
1751+
// from the [env] table. Recursive calls to cargo would use the new
1752+
// value, possibly behaving differently from the outer cargo.
1753+
//
1754+
// - RUSTUP_HOME: Under normal usage with rustup, this will have no
1755+
// effect because the rustup proxy sets RUSTUP_HOME, and that would
1756+
// override the [env] table. If the outer cargo is executed directly
1757+
// circumventing the rustup proxy, then this would affect calls to
1758+
// rustc (assuming that is a proxy), which could potentially cause
1759+
// problems with cargo and rustc being from different toolchains. We
1760+
// consider this to be not a use case we would like to support,
1761+
// since it will likely cause problems or lead to confusion.
1762+
for disallowed in &["CARGO_HOME", "RUSTUP_HOME"] {
1763+
if env_config.contains_key(*disallowed) {
1764+
bail!(
1765+
"setting the `{disallowed}` environment variable is not supported \
1766+
in the `[env]` configuration table"
1767+
);
1768+
}
17501769
}
17511770

17521771
Ok(env_config)

tests/testsuite/cargo_env_config.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,31 @@ fn env_invalid() {
5959
}
6060

6161
#[cargo_test]
62-
fn env_no_cargo_home() {
62+
fn env_no_disallowed() {
63+
// Checks for keys that are not allowed in the [env] table.
6364
let p = project()
64-
.file("Cargo.toml", &basic_bin_manifest("foo"))
65-
.file(
66-
"src/main.rs",
67-
r#"
68-
fn main() {
69-
}
70-
"#,
71-
)
72-
.file(
73-
".cargo/config",
74-
r#"
75-
[env]
76-
CARGO_HOME = "/"
77-
"#,
78-
)
65+
.file("Cargo.toml", &basic_manifest("foo", "1.0.0"))
66+
.file("src/lib.rs", "")
7967
.build();
8068

81-
p.cargo("check")
82-
.with_status(101)
83-
.with_stderr_contains("[..]setting the `CARGO_HOME` environment variable is not supported in the `[env]` configuration table")
84-
.run();
69+
for disallowed in &["CARGO_HOME", "RUSTUP_HOME"] {
70+
p.change_file(
71+
".cargo/config",
72+
&format!(
73+
r#"
74+
[env]
75+
{disallowed} = "foo"
76+
"#
77+
),
78+
);
79+
p.cargo("check")
80+
.with_status(101)
81+
.with_stderr(&format!(
82+
"[ERROR] setting the `{disallowed}` environment variable \
83+
is not supported in the `[env]` configuration table"
84+
))
85+
.run();
86+
}
8587
}
8688

8789
#[cargo_test]

0 commit comments

Comments
 (0)