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: Don't parse the home directory more than once #3078

Merged
merged 4 commits into from
Sep 9, 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
17 changes: 10 additions & 7 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::{RefCell, RefMut, Cell};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::hash_map::{HashMap};
use std::collections::hash_map::HashMap;
use std::collections::HashSet;
use std::env;
use std::fmt;
use std::fs::{self, File};
Expand Down Expand Up @@ -675,14 +676,18 @@ fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
where F: FnMut(File, &Path) -> CargoResult<()>
{
let mut current = pwd;
let mut stash: HashSet<PathBuf> = HashSet::new();

loop {
let possible = current.join(".cargo").join("config");
if fs::metadata(&possible).is_ok() {
let file = try!(File::open(&possible));

try!(walk(file, &possible));

stash.insert(possible);
}

match current.parent() {
Some(p) => current = p,
None => break,
Expand All @@ -696,12 +701,10 @@ fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
human("Cargo couldn't find your home directory. \
This probably means that $HOME was not set.")
}));
if !pwd.starts_with(&home) {
let config = home.join("config");
if fs::metadata(&config).is_ok() {
let file = try!(File::open(&config));
try!(walk(file, &config));
}
let config = home.join("config");
if !stash.contains(&config) && fs::metadata(&config).is_ok() {
let file = try!(File::open(&config));
try!(walk(file, &config));
}

Ok(())
Expand Down
5 changes: 5 additions & 0 deletions tests/cargotest/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ pub fn project(name: &str) -> ProjectBuilder {
ProjectBuilder::new(name, paths::root().join(name))
}

// Generates a project layout inside our fake home dir
pub fn project_in_home(name: &str) -> ProjectBuilder {
ProjectBuilder::new(name, paths::home().join(name))
}

// === Helpers ===

pub fn main_file(println: &str, deps: &[&str]) -> String {
Expand Down
28 changes: 27 additions & 1 deletion tests/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::Write;
use std::fs::{self, File};

use cargotest::rustc_host;
use cargotest::support::{project, execs, paths};
use cargotest::support::{project, project_in_home, execs, paths};
use hamcrest::assert_that;

#[test]
Expand Down Expand Up @@ -852,3 +852,29 @@ fn build_rustflags_no_recompile() {
assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"),
execs().with_stdout("").with_status(0));
}

#[test]
fn build_rustflags_with_home_config() {
// We need a config file inside the home directory
let home = paths::home();
let home_config = home.join(".cargo");
fs::create_dir(&home_config).unwrap();
File::create(&home_config.join("config")).unwrap().write_all(br#"
[build]
rustflags = ["-Cllvm-args=-x86-asm-syntax=intel"]
"#).unwrap();

// And we need the project to be inside the home directory
// so the walking process finds the home project twice.
let p = project_in_home("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file("src/lib.rs", "");
p.build();

assert_that(p.cargo("build").arg("-v"),
execs().with_status(0));
}