Skip to content

Commit 256d9dd

Browse files
author
Jorge Aparicio
committed
fix: prefer env variables over config files
This patches fixes the precedence in these cases: - CARGO_TARGET_ROOT is now preferred over build.target-dir - RUSTC is now preferred over build.rustc - RUSTDOC is now preferred over build.rustdoc
1 parent 289411b commit 256d9dd

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/cargo/util/config.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::cell::{RefCell, RefMut, Ref, Cell};
22
use std::collections::hash_map::Entry::{Occupied, Vacant};
33
use std::collections::hash_map::{HashMap};
44
use std::env;
5-
use std::ffi::OsString;
65
use std::fmt;
76
use std::fs::{self, File};
87
use std::io::prelude::*;
@@ -243,26 +242,29 @@ impl Config {
243242
}
244243

245244
fn scrape_target_dir_config(&mut self) -> CargoResult<()> {
246-
if let Some((dir, dir2)) = try!(self.get_string("build.target-dir")) {
245+
if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
246+
*self.target_dir.borrow_mut() = Some(self.cwd.join(dir));
247+
} else if let Some((dir, dir2)) = try!(self.get_string("build.target-dir")) {
247248
let mut path = PathBuf::from(dir2);
248249
path.pop();
249250
path.pop();
250251
path.push(dir);
251252
*self.target_dir.borrow_mut() = Some(path);
252-
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
253-
*self.target_dir.borrow_mut() = Some(self.cwd.join(dir));
254253
}
255254
Ok(())
256255
}
257256

258257
fn get_tool(&self, tool: &str) -> CargoResult<PathBuf> {
258+
let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::<String>();
259+
if let Some(tool_path) = env::var_os(&var) {
260+
return Ok(PathBuf::from(tool_path));
261+
}
262+
259263
let var = format!("build.{}", tool);
260264
if let Some(tool_path) = try!(self.get_path(&var)) {
261265
return Ok(tool_path);
262266
}
263267

264-
let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::<String>();
265-
let tool = env::var_os(&var).unwrap_or_else(|| OsString::from(tool));
266268
Ok(PathBuf::from(tool))
267269
}
268270
}

tests/test_cargo_compile.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1913,9 +1913,9 @@ test!(custom_target_dir {
19131913
fs::create_dir(p.root().join(".cargo")).unwrap();
19141914
File::create(p.root().join(".cargo/config")).unwrap().write_all(br#"
19151915
[build]
1916-
target-dir = "bar/target"
1916+
target-dir = "foo/target"
19171917
"#).unwrap();
1918-
assert_that(p.cargo("build").env("CARGO_TARGET_DIR", "foo/target"),
1918+
assert_that(p.cargo("build").env("CARGO_TARGET_DIR", "bar/target"),
19191919
execs().with_status(0));
19201920
assert_that(&p.root().join("bar/target/debug").join(&exe_name),
19211921
existing_file());

0 commit comments

Comments
 (0)