Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error if CARGO_TARGET_DIR is an empty string #8939

Merged
merged 6 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,24 @@ 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 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 val = val.resolve_path(self);
Ok(Some(Filesystem::new(val)))
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!(format!(
"the target directory is set to an empty string in {}",
val.value().definition
),)
}

Ok(Some(Filesystem::new(path)))
} else {
Ok(None)
}
Expand Down
5 changes: 5 additions & 0 deletions src/cargo/util/config/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ use std::path::PathBuf;
pub struct ConfigRelativePath(Value<String>);

impl ConfigRelativePath {
/// Returns the underlying value.
pub fn value(&self) -> &Value<String> {
&self.0
}

/// Returns the raw underlying configuration value for this key.
pub fn raw_value(&self) -> &str {
&self.0.val
Expand Down
28 changes: 28 additions & 0 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,3 +1483,31 @@ 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 in [..]/.cargo/config",
);
}

#[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()
}