diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 731ea6cd6a0..53badb21287 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1685,8 +1685,15 @@ impl Config { } pub fn env_config(&self) -> CargoResult<&EnvConfig> { - self.env_config - .try_borrow_with(|| self.get::("env")) + let env_config = self + .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") + } + + Ok(env_config) } /// This is used to validate the `term` table has valid syntax. diff --git a/tests/testsuite/cargo_env_config.rs b/tests/testsuite/cargo_env_config.rs index 6ea34380d4c..1872d56c004 100644 --- a/tests/testsuite/cargo_env_config.rs +++ b/tests/testsuite/cargo_env_config.rs @@ -57,6 +57,32 @@ fn env_invalid() { .run(); } +#[cargo_test] +fn env_no_cargo_home() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file( + "src/main.rs", + r#" + fn main() { + } + "#, + ) + .file( + ".cargo/config", + r#" + [env] + CARGO_HOME = "/" + "#, + ) + .build(); + + p.cargo("build") + .with_status(101) + .with_stderr_contains("[..]setting the `CARGO_HOME` environment variable is not supported in the `[env]` configuration table") + .run(); +} + #[cargo_test] fn env_force() { let p = project()