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.
This commit is contained in:
Alex Crichton 2018-01-31 10:30:01 -08:00
parent df11070cb5
commit e4f32bf34e
2 changed files with 60 additions and 2 deletions

View file

@ -616,7 +616,16 @@ impl Config {
fn maybe_get_tool(&self, tool: &str) -> CargoResult<Option<PathBuf>> {
let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::<String>();
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);

View file

@ -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));
}