From 4eff829f1e3f88b6738c9106b11316cbdb96af63 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 4 Dec 2020 12:36:26 +1100 Subject: [PATCH 1/6] Interpret CARGO_TARGET_DIR as target/ if its '' with spaces trimmed --- src/cargo/util/config/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 9a3c5b0df43..090216fbab5 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -450,6 +450,10 @@ impl Config { if let Some(dir) = &self.target_dir { Ok(Some(dir.clone())) } else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { + if dir.to_str().unwrap().trim() == "" { + return Ok(Some(Filesystem::new(self.cwd.join("target")))); + } + Ok(Some(Filesystem::new(self.cwd.join(dir)))) } else if let Some(val) = &self.build_config()?.target_dir { let val = val.resolve_path(self); From 9ceae6826e9ce3b5646e0e86dddfb345bd8251b5 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 5 Dec 2020 13:20:22 +1100 Subject: [PATCH 2/6] Error if the CARGO_TARGET_DIR is an empty string --- src/cargo/util/config/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 090216fbab5..cb75394eaba 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -451,7 +451,7 @@ impl Config { Ok(Some(dir.clone())) } else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { if dir.to_str().unwrap().trim() == "" { - return Ok(Some(Filesystem::new(self.cwd.join("target")))); + anyhow::bail!("The CARGO_TARGET_DIR environment variable is an empty string. Try adding the target directory name to it or deleting the variable.") } Ok(Some(Filesystem::new(self.cwd.join(dir)))) From 7a69aafbf369e1d9a2df8fe6692db42c08656515 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 23 Jan 2021 18:18:28 +1100 Subject: [PATCH 3/6] Add tests for target_dir; throw error if [build] target = '' and update messages --- src/cargo/util/config/mod.rs | 13 +++++++++---- tests/testsuite/config.rs | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index cb75394eaba..f6273212174 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -450,14 +450,19 @@ impl Config { if let Some(dir) = &self.target_dir { Ok(Some(dir.clone())) } else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { - if dir.to_str().unwrap().trim() == "" { - anyhow::bail!("The CARGO_TARGET_DIR environment variable is an empty string. Try adding the target directory name to it or deleting the variable.") + if dir.to_string_lossy() == "" { + anyhow::bail!("the target directory is set to an empty string") } Ok(Some(Filesystem::new(self.cwd.join(dir)))) } else if let Some(val) = &self.build_config()?.target_dir { - let val = val.resolve_path(self); - Ok(Some(Filesystem::new(val))) + let path = val.resolve_path(self); + + if val.raw_value() == "" { + anyhow::bail!("the target directory is set to an empty string") + } + + Ok(Some(Filesystem::new(path))) } else { Ok(None) } diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 0723d3cad97..9f7f1fa7be7 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -1483,3 +1483,20 @@ Caused by: unknown variant `invalid`, expected one of `debuginfo`, `none`, `symbols`", ); } + +#[cargo_test] +fn cargo_target_empty_cfg() { + write_config( + "\ +[build] +target-dir = '' +", + ); + + let config = new_config(); + + assert_error( + config.target_dir().unwrap_err(), + "the target directory is set to an empty string", + ); +} From 97707a880e5a9ea105dcad736047c93fc83e33d5 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 24 Feb 2021 17:56:20 +1100 Subject: [PATCH 4/6] Add tests --- src/cargo/util/config/mod.rs | 8 ++++++-- tests/testsuite/config.rs | 13 ++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index f6273212174..0955b2d1056 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -450,16 +450,20 @@ impl Config { if let Some(dir) = &self.target_dir { Ok(Some(dir.clone())) } else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { + // Check if the CARGO_TARGET_DIR environment variable is set to an empty string. if dir.to_string_lossy() == "" { - anyhow::bail!("the target directory is set to an empty string") + anyhow::bail!("the target directory is set to an empty string in the CARGO_TARGET_DIR environment variable.") } Ok(Some(Filesystem::new(self.cwd.join(dir)))) } else if let Some(val) = &self.build_config()?.target_dir { let path = val.resolve_path(self); + // Check if the target directory is set to an empty string in the config.toml file. if val.raw_value() == "" { - anyhow::bail!("the target directory is set to an empty string") + anyhow::bail!( + "the target directory is set to an empty string in the config.toml file.", + ) } Ok(Some(Filesystem::new(path))) diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 9f7f1fa7be7..6d5df194aa9 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -1497,6 +1497,17 @@ target-dir = '' assert_error( config.target_dir().unwrap_err(), - "the target directory is set to an empty string", + "the target directory is set to an empty string in the config.toml file.", ); } + +#[cargo_test] +fn cargo_target_empty_env() { + let project = project().build(); + + project.cargo("build") + .env("CARGO_TARGET_DIR", "") + .with_stderr("error: the target directory is set to an empty string in the CARGO_TARGET_DIR environment variable.") + .with_status(101) + .run() +} From 019b4e111ea6f39ac232e753579d469661cc3a2b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 25 Feb 2021 18:10:59 +1100 Subject: [PATCH 5/6] Fixes error message --- src/cargo/util/config/mod.rs | 9 +++++---- src/cargo/util/config/path.rs | 5 +++++ tests/testsuite/config.rs | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 0955b2d1056..d42de014712 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -452,7 +452,7 @@ impl Config { } else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { // Check if the CARGO_TARGET_DIR environment variable is set to an empty string. if dir.to_string_lossy() == "" { - anyhow::bail!("the target directory is set to an empty string in the CARGO_TARGET_DIR environment variable.") + anyhow::bail!("the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable") } Ok(Some(Filesystem::new(self.cwd.join(dir)))) @@ -461,9 +461,10 @@ impl Config { // Check if the target directory is set to an empty string in the config.toml file. if val.raw_value() == "" { - anyhow::bail!( - "the target directory is set to an empty string in the config.toml file.", - ) + anyhow::bail!(format!( + "the target directory is set to an empty string in {}", + val.value().definition + ),) } Ok(Some(Filesystem::new(path))) diff --git a/src/cargo/util/config/path.rs b/src/cargo/util/config/path.rs index f859b556306..6180d0f3f13 100644 --- a/src/cargo/util/config/path.rs +++ b/src/cargo/util/config/path.rs @@ -10,6 +10,11 @@ use std::path::PathBuf; pub struct ConfigRelativePath(Value); impl ConfigRelativePath { + /// Returns the underlying value. + pub fn value(&self) -> &Value { + &self.0 + } + /// Returns the raw underlying configuration value for this key. pub fn raw_value(&self) -> &str { &self.0.val diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 6d5df194aa9..65ea2e10bfd 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -1497,7 +1497,7 @@ target-dir = '' assert_error( config.target_dir().unwrap_err(), - "the target directory is set to an empty string in the config.toml file.", + "the target directory is set to an empty string in [..]/.cargo/config", ); } From b5b2e489d62385aecaa41276463fffdf5096227f Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 25 Feb 2021 18:48:05 +1100 Subject: [PATCH 6/6] Fix tests --- tests/testsuite/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 65ea2e10bfd..89c5b9f5f2a 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -1507,7 +1507,7 @@ fn cargo_target_empty_env() { project.cargo("build") .env("CARGO_TARGET_DIR", "") - .with_stderr("error: the target directory is set to an empty string in the CARGO_TARGET_DIR environment variable.") + .with_stderr("error: the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable") .with_status(101) .run() }