Skip to content

Commit c08ad3b

Browse files
committed
fix: Avoid walking config twice
1 parent 6628a6e commit c08ad3b

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/cargo/util/context/mod.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1577,25 +1577,31 @@ impl GlobalContext {
15771577
}
15781578
}
15791579

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

15861586
for current in paths::ancestors(pwd, self.search_stop_path.as_deref()) {
1587+
// Beware `current` may be the parent of the `$CARGO_HOME`, in this case the next line also walk the `$CARGO_HOME`.
15871588
if let Some(path) = self.get_file_path(&current.join(".cargo"), "config", true)? {
15881589
walk(&path)?;
15891590
stash.insert(path);
15901591
}
15911592
}
15921593

1593-
// Once we're done, also be sure to walk the home directory even if it's not
1594-
// in our history to be sure we pick up that standard location for
1595-
// information.
1596-
if let Some(path) = self.get_file_path(home, "config", true)? {
1597-
if !stash.contains(&path) {
1598-
walk(&path)?;
1594+
// The convention `$CARGO_HOME` can be changed by ENV `CARGO_HOME`, like renaming $HOME/.cargo into $HOME/my_home,
1595+
// So the `config` must be joined with the actual `cargo_home`.
1596+
let home_config_without_extention = cargo_home.join("config");
1597+
let already_walked = stash.contains(&home_config_without_extention);
1598+
1599+
// If we haven't walked the $CARGO_HOME directory yet, walk it to pick up that standard location for information.
1600+
if !already_walked {
1601+
if let Some(path) = self.get_file_path(cargo_home, "config", true)? {
1602+
if !stash.contains(&path) {
1603+
walk(&path)?;
1604+
}
15991605
}
16001606
}
16011607

tests/testsuite/config.rs

-2
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ f1 = 1
315315
.with_stderr_data(str![[r#"
316316
[WARNING] `[ROOT]/.cargo/config` is deprecated in favor of `config.toml`
317317
[NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
318-
[WARNING] `[ROOT]/.cargo/config` is deprecated in favor of `config.toml`
319-
[NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
320318
321319
"#]])
322320
.run();

0 commit comments

Comments
 (0)