Skip to content

Commit 7a69aaf

Browse files
Add tests for target_dir; throw error if [build] target = '' and update messages
1 parent 9ceae68 commit 7a69aaf

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/cargo/util/config/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,19 @@ impl Config {
450450
if let Some(dir) = &self.target_dir {
451451
Ok(Some(dir.clone()))
452452
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
453-
if dir.to_str().unwrap().trim() == "" {
454-
anyhow::bail!("The CARGO_TARGET_DIR environment variable is an empty string. Try adding the target directory name to it or deleting the variable.")
453+
if dir.to_string_lossy() == "" {
454+
anyhow::bail!("the target directory is set to an empty string")
455455
}
456456

457457
Ok(Some(Filesystem::new(self.cwd.join(dir))))
458458
} else if let Some(val) = &self.build_config()?.target_dir {
459-
let val = val.resolve_path(self);
460-
Ok(Some(Filesystem::new(val)))
459+
let path = val.resolve_path(self);
460+
461+
if val.raw_value() == "" {
462+
anyhow::bail!("the target directory is set to an empty string")
463+
}
464+
465+
Ok(Some(Filesystem::new(path)))
461466
} else {
462467
Ok(None)
463468
}

tests/testsuite/config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,3 +1483,20 @@ Caused by:
14831483
unknown variant `invalid`, expected one of `debuginfo`, `none`, `symbols`",
14841484
);
14851485
}
1486+
1487+
#[cargo_test]
1488+
fn cargo_target_empty_cfg() {
1489+
write_config(
1490+
"\
1491+
[build]
1492+
target-dir = ''
1493+
",
1494+
);
1495+
1496+
let config = new_config();
1497+
1498+
assert_error(
1499+
config.target_dir().unwrap_err(),
1500+
"the target directory is set to an empty string",
1501+
);
1502+
}

0 commit comments

Comments
 (0)