Skip to content

Commit

Permalink
Fix rustc/rustdoc config values to be config-relative paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 10, 2021
1 parent 7b229bb commit 47a0291
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
20 changes: 12 additions & 8 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ impl Config {
})
}

fn string_to_path(&self, value: String, definition: &Definition) -> PathBuf {
fn string_to_path(&self, value: &str, definition: &Definition) -> PathBuf {
let is_path = value.contains('/') || (cfg!(windows) && value.contains('\\'));
if is_path {
definition.root(self).join(value)
Expand Down Expand Up @@ -1391,7 +1391,11 @@ impl Config {

/// Looks for a path for `tool` in an environment variable or the given config, and returns
/// `None` if it's not present.
fn maybe_get_tool(&self, tool: &str, from_config: &Option<PathBuf>) -> Option<PathBuf> {
fn maybe_get_tool(
&self,
tool: &str,
from_config: &Option<ConfigRelativePath>,
) -> Option<PathBuf> {
let var = tool.to_uppercase();

match env::var_os(&var) {
Expand All @@ -1408,13 +1412,13 @@ impl Config {
Some(path)
}

None => from_config.clone(),
None => from_config.as_ref().map(|p| p.resolve_program(self)),
}
}

/// Looks for a path for `tool` in an environment variable or config path, defaulting to `tool`
/// as a path.
fn get_tool(&self, tool: &str, from_config: &Option<PathBuf>) -> PathBuf {
fn get_tool(&self, tool: &str, from_config: &Option<ConfigRelativePath>) -> PathBuf {
self.maybe_get_tool(tool, from_config)
.unwrap_or_else(|| PathBuf::from(tool))
}
Expand Down Expand Up @@ -2084,10 +2088,10 @@ pub struct CargoBuildConfig {
pub jobs: Option<u32>,
pub rustflags: Option<StringList>,
pub rustdocflags: Option<StringList>,
pub rustc_wrapper: Option<PathBuf>,
pub rustc_workspace_wrapper: Option<PathBuf>,
pub rustc: Option<PathBuf>,
pub rustdoc: Option<PathBuf>,
pub rustc_wrapper: Option<ConfigRelativePath>,
pub rustc_workspace_wrapper: Option<ConfigRelativePath>,
pub rustc: Option<ConfigRelativePath>,
pub rustdoc: Option<ConfigRelativePath>,
pub out_dir: Option<ConfigRelativePath>,
}

Expand Down
4 changes: 2 additions & 2 deletions src/cargo/util/config/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ impl ConfigRelativePath {
/// Values which don't look like a filesystem path (don't contain `/` or
/// `\`) will be returned as-is, and everything else will fall through to an
/// absolute path.
pub fn resolve_program(self, config: &Config) -> PathBuf {
config.string_to_path(self.0.val, &self.0.definition)
pub fn resolve_program(&self, config: &Config) -> PathBuf {
config.string_to_path(&self.0.val, &self.0.definition)
}
}

Expand Down
27 changes: 26 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4075,7 +4075,21 @@ fn rustc_wrapper() {

#[cargo_test]
fn rustc_wrapper_relative() {
let p = project().file("src/lib.rs", "").build();
Package::new("bar", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
let wrapper = tools::echo_wrapper();
let exe_name = wrapper.file_name().unwrap().to_str().unwrap();
let relative_path = format!("./{}", exe_name);
Expand All @@ -4090,6 +4104,17 @@ fn rustc_wrapper_relative() {
.env("RUSTC_WORKSPACE_WRAPPER", &relative_path)
.with_stderr_contains(&running)
.run();
p.build_dir().rm_rf();
p.change_file(
".cargo/config.toml",
&format!(
r#"
build.rustc-wrapper = "./{}"
"#,
exe_name
),
);
p.cargo("build -v").with_stderr_contains(&running).run();
}

#[cargo_test]
Expand Down

0 comments on commit 47a0291

Please sign in to comment.