Fix `RUSTC=./relative-path` when building
authorAlex Crichton <alex@alexcrichton.com>
Wed, 31 Jan 2018 18:30:01 +0000 (10:30 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 1 Feb 2018 19:11:59 +0000 (11:11 -0800)
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/util/config.rs
tests/workspaces.rs

index 715180ffa7330f03dbc2b0bccc5fd029ff534f06..bd6bea77cc0bcd5cac915a5410214f24b3deef27 100644 (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);
index 5eb01a0801072027aeb05e329f2a75bd36f1c8db..77df9744891511054b93374332c6a557e2c0aaec 100644 (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));
+}