Skip to content

Commit f3e63d6

Browse files
committed
Auto merge of #8939 - Andy-Python-Programmer:master, r=alexcrichton
Throw error if CARGO_TARGET_DIR is an empty string This pull request makes the target dir to be target/ if `CARGO_TARGET_DIR` is `` with spaces trimmed and not delete the current project. Fixes: #8866
2 parents 6780fbd + b5b2e48 commit f3e63d6

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/cargo/util/config/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,24 @@ impl Config {
473473
if let Some(dir) = &self.target_dir {
474474
Ok(Some(dir.clone()))
475475
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
476+
// Check if the CARGO_TARGET_DIR environment variable is set to an empty string.
477+
if dir.to_string_lossy() == "" {
478+
anyhow::bail!("the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable")
479+
}
480+
476481
Ok(Some(Filesystem::new(self.cwd.join(dir))))
477482
} else if let Some(val) = &self.build_config()?.target_dir {
478-
let val = val.resolve_path(self);
479-
Ok(Some(Filesystem::new(val)))
483+
let path = val.resolve_path(self);
484+
485+
// Check if the target directory is set to an empty string in the config.toml file.
486+
if val.raw_value() == "" {
487+
anyhow::bail!(format!(
488+
"the target directory is set to an empty string in {}",
489+
val.value().definition
490+
),)
491+
}
492+
493+
Ok(Some(Filesystem::new(path)))
480494
} else {
481495
Ok(None)
482496
}

src/cargo/util/config/path.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ use std::path::PathBuf;
1010
pub struct ConfigRelativePath(Value<String>);
1111

1212
impl ConfigRelativePath {
13+
/// Returns the underlying value.
14+
pub fn value(&self) -> &Value<String> {
15+
&self.0
16+
}
17+
1318
/// Returns the raw underlying configuration value for this key.
1419
pub fn raw_value(&self) -> &str {
1520
&self.0.val

tests/testsuite/config.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,3 +1460,31 @@ strip = 'debuginfo'
14601460
let strip = p.strip.unwrap();
14611461
assert_eq!(strip, toml::StringOrBool::String("debuginfo".to_string()));
14621462
}
1463+
1464+
#[cargo_test]
1465+
fn cargo_target_empty_cfg() {
1466+
write_config(
1467+
"\
1468+
[build]
1469+
target-dir = ''
1470+
",
1471+
);
1472+
1473+
let config = new_config();
1474+
1475+
assert_error(
1476+
config.target_dir().unwrap_err(),
1477+
"the target directory is set to an empty string in [..]/.cargo/config",
1478+
);
1479+
}
1480+
1481+
#[cargo_test]
1482+
fn cargo_target_empty_env() {
1483+
let project = project().build();
1484+
1485+
project.cargo("build")
1486+
.env("CARGO_TARGET_DIR", "")
1487+
.with_stderr("error: the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable")
1488+
.with_status(101)
1489+
.run()
1490+
}

0 commit comments

Comments
 (0)