From 47a02919cc454ab1653cd2054653befd705a9899 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 9 Jun 2021 17:46:18 -0700 Subject: [PATCH] Fix rustc/rustdoc config values to be config-relative paths. --- src/cargo/util/config/mod.rs | 20 ++++++++++++-------- src/cargo/util/config/path.rs | 4 ++-- tests/testsuite/build.rs | 27 ++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 71bbf78b456..1d865d4f136 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -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) @@ -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) -> Option { + fn maybe_get_tool( + &self, + tool: &str, + from_config: &Option, + ) -> Option { let var = tool.to_uppercase(); match env::var_os(&var) { @@ -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 { + fn get_tool(&self, tool: &str, from_config: &Option) -> PathBuf { self.maybe_get_tool(tool, from_config) .unwrap_or_else(|| PathBuf::from(tool)) } @@ -2084,10 +2088,10 @@ pub struct CargoBuildConfig { pub jobs: Option, pub rustflags: Option, pub rustdocflags: Option, - pub rustc_wrapper: Option, - pub rustc_workspace_wrapper: Option, - pub rustc: Option, - pub rustdoc: Option, + pub rustc_wrapper: Option, + pub rustc_workspace_wrapper: Option, + pub rustc: Option, + pub rustdoc: Option, pub out_dir: Option, } diff --git a/src/cargo/util/config/path.rs b/src/cargo/util/config/path.rs index 6180d0f3f13..a90cab2b268 100644 --- a/src/cargo/util/config/path.rs +++ b/src/cargo/util/config/path.rs @@ -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) } } diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index f703454e517..5b2f3786bd5 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -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); @@ -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]