Skip to content

Commit 784696a

Browse files
committed
Auto merge of #9225 - ehuss:fix-target-dir-env-test, r=alexcrichton
Fix `cargo_target_empty_cfg` test with env var. The test `cargo_target_empty_cfg` does not work if the environment variable `CARGO_TARGET_DIR` is set. `Config` should not be looking at the "real" environment, but instead `self.env` which is controlled during tests. I also did some minor style/cleanup changes. Unblocks upstream sync.
2 parents cb0e8c3 + a0d0d01 commit 784696a

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/cargo/util/config/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,22 +471,25 @@ impl Config {
471471
pub fn target_dir(&self) -> CargoResult<Option<Filesystem>> {
472472
if let Some(dir) = &self.target_dir {
473473
Ok(Some(dir.clone()))
474-
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
474+
} else if let Some(dir) = self.env.get("CARGO_TARGET_DIR") {
475475
// Check if the CARGO_TARGET_DIR environment variable is set to an empty string.
476-
if dir.to_string_lossy() == "" {
477-
anyhow::bail!("the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable")
476+
if dir.is_empty() {
477+
bail!(
478+
"the target directory is set to an empty string in the \
479+
`CARGO_TARGET_DIR` environment variable"
480+
)
478481
}
479482

480483
Ok(Some(Filesystem::new(self.cwd.join(dir))))
481484
} else if let Some(val) = &self.build_config()?.target_dir {
482485
let path = val.resolve_path(self);
483486

484487
// Check if the target directory is set to an empty string in the config.toml file.
485-
if val.raw_value() == "" {
486-
anyhow::bail!(format!(
488+
if val.raw_value().is_empty() {
489+
bail!(
487490
"the target directory is set to an empty string in {}",
488491
val.value().definition
489-
),)
492+
)
490493
}
491494

492495
Ok(Some(Filesystem::new(path)))

0 commit comments

Comments
 (0)