Skip to content

Commit

Permalink
fix: Avoid walking config twice
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Sep 25, 2024
1 parent 6628a6e commit 259f72d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,25 +1577,27 @@ impl GlobalContext {
}
}

fn walk_tree<F>(&self, pwd: &Path, home: &Path, mut walk: F) -> CargoResult<()>
fn walk_tree<F>(&self, pwd: &Path, cargo_home: &Path, mut walk: F) -> CargoResult<()>
where
F: FnMut(&Path) -> CargoResult<()>,
{
let mut stash: HashSet<PathBuf> = HashSet::new();

for current in paths::ancestors(pwd, self.search_stop_path.as_deref()) {
// Be aware `current` may be the parent of the `$CARGO_HOME`, in this case the next line also walk the `$CARGO_HOME`.
if let Some(path) = self.get_file_path(&current.join(".cargo"), "config", true)? {
walk(&path)?;
stash.insert(path);
}
}

// Once we're done, also be sure to walk the home directory even if it's not
// in our history to be sure we pick up that standard location for
// information.
if let Some(path) = self.get_file_path(home, "config", true)? {
if !stash.contains(&path) {
walk(&path)?;
let cargo_home_parent = cargo_home.parent().unwrap_or(cargo_home);
// If we haven't walked the $CARGO_HOME directory yet, walk it to pick up that standard location for information.
if !pwd.starts_with(cargo_home_parent) {
if let Some(path) = self.get_file_path(cargo_home, "config", true)? {
if !stash.contains(&path) {
walk(&path)?;
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,6 @@ f1 = 1
.with_stderr_data(str![[r#"
[WARNING] `[ROOT]/.cargo/config` is deprecated in favor of `config.toml`
[NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
[WARNING] `[ROOT]/.cargo/config` is deprecated in favor of `config.toml`
[NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
"#]])
.run();
Expand Down

0 comments on commit 259f72d

Please sign in to comment.