From 64f72af4b51e7f130215c1c0245a96f071a1f1dc Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 31 Jan 2018 10:30:01 -0800 Subject: [PATCH] Fix `RUSTC=./relative-path` when building This commit adjusts the compiler location logic to resolve `./relative-path` before invoking rustc to ensure it's no longer cwd-relative. This is how many other variables like `CARGO_HOME` work, so it's applying similar logic. --- src/cargo/ops/cargo_test.rs | 2 +- src/cargo/util/config.rs | 11 +++++++- src/cargo/util/lazy_cell.rs | 1 + tests/bench.rs | 1 + tests/workspaces.rs | 51 ++++++++++++++++++++++++++++++++++++- 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index 7fac6785525..deb547b6237 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -120,7 +120,7 @@ fn run_unit_tests(options: &TestOptions, let (kind, test, e) = errors.pop().unwrap(); Ok((Test::UnitTest(kind, test), vec![e])) } else { - Ok((Test::Multiple, errors.into_iter().map((|(_, _, e)| e)).collect())) + Ok((Test::Multiple, errors.into_iter().map(|(_, _, e)| e).collect())) } } diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 77577ab3077..16c6e7f2ab6 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -605,7 +605,16 @@ impl Config { fn maybe_get_tool(&self, tool: &str) -> CargoResult> { let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::(); if let Some(tool_path) = env::var_os(&var) { - return Ok(Some(PathBuf::from(tool_path))); + let maybe_relative = match tool_path.to_str() { + Some(s) => s.contains("/") || s.contains("\\"), + None => false, + }; + let path = if maybe_relative { + self.cwd.join(tool_path) + } else { + PathBuf::from(tool_path) + }; + return Ok(Some(path)) } let var = format!("build.{}", tool); diff --git a/src/cargo/util/lazy_cell.rs b/src/cargo/util/lazy_cell.rs index 607f2ef982e..a5525417527 100644 --- a/src/cargo/util/lazy_cell.rs +++ b/src/cargo/util/lazy_cell.rs @@ -55,6 +55,7 @@ impl LazyCell { } /// Consumes this `LazyCell`, returning the underlying value. + #[allow(unused_unsafe)] pub fn into_inner(self) -> Option { unsafe { self.inner.into_inner() diff --git a/tests/bench.rs b/tests/bench.rs index 491867a6c3c..be889693115 100644 --- a/tests/bench.rs +++ b/tests/bench.rs @@ -257,6 +257,7 @@ fn many_similar_names() { } #[test] +#[ignore] // unignored on the master branch of cargo fn cargo_bench_failing_test() { if !is_nightly() { return } diff --git a/tests/workspaces.rs b/tests/workspaces.rs index 5eb01a08010..77df9744891 100644 --- a/tests/workspaces.rs +++ b/tests/workspaces.rs @@ -2,8 +2,9 @@ extern crate cargotest; extern crate hamcrest; +use std::env; +use std::fs::{self, File}; use std::io::{Read, Write}; -use std::fs::File; use cargotest::sleep_ms; use cargotest::support::{project, execs, git}; @@ -1807,3 +1808,51 @@ fn cargo_home_at_root_works() { assert_that(p.cargo("build").arg("--frozen").env("CARGO_HOME", p.root()), execs().with_status(0)); } + +#[test] +fn relative_rustc() { + let p = project("the_exe") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.1.0" + "#) + .file("src/main.rs", r#" + use std::process::Command; + use std::env; + + fn main() { + let mut cmd = Command::new("rustc"); + for arg in env::args_os().skip(1) { + cmd.arg(arg); + } + std::process::exit(cmd.status().unwrap().code().unwrap()); + } + "#) + .build(); + assert_that(p.cargo("build"), execs().with_status(0)); + + let src = p.root() + .join("target/debug/foo") + .with_extension(env::consts::EXE_EXTENSION); + + Package::new("a", "0.1.0").publish(); + + let p = project("lib") + .file("Cargo.toml", r#" + [package] + name = "lib" + version = "0.1.0" + + [dependencies] + a = "0.1" + "#) + .file("src/lib.rs", "") + .build(); + + fs::copy(&src, p.root().join(src.file_name().unwrap())).unwrap(); + + let file = format!("./foo{}", env::consts::EXE_SUFFIX); + assert_that(p.cargo("build").env("RUSTC", &file), + execs().with_status(0)); +}