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 c08ad3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,25 +1577,31 @@ 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()) {
// Beware `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)?;
// The convention `$CARGO_HOME` can be changed by ENV `CARGO_HOME`, like renaming $HOME/.cargo into $HOME/my_home,
// So the `config` must be joined with the actual `cargo_home`.
let home_config_without_extention = cargo_home.join("config");
let already_walked = stash.contains(&home_config_without_extention);

// If we haven't walked the $CARGO_HOME directory yet, walk it to pick up that standard location for information.
if !already_walked {
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 c08ad3b

Please sign in to comment.