Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prefer env variables over config files #2365

Merged
merged 1 commit into from
Feb 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::cell::{RefCell, RefMut, Ref, Cell};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::hash_map::{HashMap};
use std::env;
use std::ffi::OsString;
use std::fmt;
use std::fs::{self, File};
use std::io::prelude::*;
Expand Down Expand Up @@ -243,26 +242,29 @@ impl Config {
}

fn scrape_target_dir_config(&mut self) -> CargoResult<()> {
if let Some((dir, dir2)) = try!(self.get_string("build.target-dir")) {
if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
*self.target_dir.borrow_mut() = Some(self.cwd.join(dir));
} else if let Some((dir, dir2)) = try!(self.get_string("build.target-dir")) {
let mut path = PathBuf::from(dir2);
path.pop();
path.pop();
path.push(dir);
*self.target_dir.borrow_mut() = Some(path);
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
*self.target_dir.borrow_mut() = Some(self.cwd.join(dir));
}
Ok(())
}

fn get_tool(&self, tool: &str) -> CargoResult<PathBuf> {
let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::<String>();
if let Some(tool_path) = env::var_os(&var) {
return Ok(PathBuf::from(tool_path));
}

let var = format!("build.{}", tool);
if let Some(tool_path) = try!(self.get_path(&var)) {
return Ok(tool_path);
}

let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::<String>();
let tool = env::var_os(&var).unwrap_or_else(|| OsString::from(tool));
Ok(PathBuf::from(tool))
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1913,9 +1913,9 @@ test!(custom_target_dir {
fs::create_dir(p.root().join(".cargo")).unwrap();
File::create(p.root().join(".cargo/config")).unwrap().write_all(br#"
[build]
target-dir = "bar/target"
target-dir = "foo/target"
"#).unwrap();
assert_that(p.cargo("build").env("CARGO_TARGET_DIR", "foo/target"),
assert_that(p.cargo("build").env("CARGO_TARGET_DIR", "bar/target"),
execs().with_status(0));
assert_that(&p.root().join("bar/target/debug").join(&exe_name),
existing_file());
Expand Down