Skip to content

Commit cd5b980

Browse files
committed
Auto merge of #4995 - alexcrichton:relative-env, r=matklad
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.
2 parents df11070 + e4f32bf commit cd5b980

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/cargo/util/config.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,16 @@ impl Config {
616616
fn maybe_get_tool(&self, tool: &str) -> CargoResult<Option<PathBuf>> {
617617
let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::<String>();
618618
if let Some(tool_path) = env::var_os(&var) {
619-
return Ok(Some(PathBuf::from(tool_path)));
619+
let maybe_relative = match tool_path.to_str() {
620+
Some(s) => s.contains("/") || s.contains("\\"),
621+
None => false,
622+
};
623+
let path = if maybe_relative {
624+
self.cwd.join(tool_path)
625+
} else {
626+
PathBuf::from(tool_path)
627+
};
628+
return Ok(Some(path))
620629
}
621630

622631
let var = format!("build.{}", tool);

tests/workspaces.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
extern crate cargotest;
33
extern crate hamcrest;
44

5+
use std::env;
6+
use std::fs::{self, File};
57
use std::io::{Read, Write};
6-
use std::fs::File;
78

89
use cargotest::sleep_ms;
910
use cargotest::support::{project, execs, git};
@@ -1807,3 +1808,51 @@ fn cargo_home_at_root_works() {
18071808
assert_that(p.cargo("build").arg("--frozen").env("CARGO_HOME", p.root()),
18081809
execs().with_status(0));
18091810
}
1811+
1812+
#[test]
1813+
fn relative_rustc() {
1814+
let p = project("the_exe")
1815+
.file("Cargo.toml", r#"
1816+
[package]
1817+
name = "foo"
1818+
version = "0.1.0"
1819+
"#)
1820+
.file("src/main.rs", r#"
1821+
use std::process::Command;
1822+
use std::env;
1823+
1824+
fn main() {
1825+
let mut cmd = Command::new("rustc");
1826+
for arg in env::args_os().skip(1) {
1827+
cmd.arg(arg);
1828+
}
1829+
std::process::exit(cmd.status().unwrap().code().unwrap());
1830+
}
1831+
"#)
1832+
.build();
1833+
assert_that(p.cargo("build"), execs().with_status(0));
1834+
1835+
let src = p.root()
1836+
.join("target/debug/foo")
1837+
.with_extension(env::consts::EXE_EXTENSION);
1838+
1839+
Package::new("a", "0.1.0").publish();
1840+
1841+
let p = project("lib")
1842+
.file("Cargo.toml", r#"
1843+
[package]
1844+
name = "lib"
1845+
version = "0.1.0"
1846+
1847+
[dependencies]
1848+
a = "0.1"
1849+
"#)
1850+
.file("src/lib.rs", "")
1851+
.build();
1852+
1853+
fs::copy(&src, p.root().join(src.file_name().unwrap())).unwrap();
1854+
1855+
let file = format!("./foo{}", env::consts::EXE_SUFFIX);
1856+
assert_that(p.cargo("build").env("RUSTC", &file),
1857+
execs().with_status(0));
1858+
}

0 commit comments

Comments
 (0)