Skip to content

Commit 23440c0

Browse files
committed
config reading: use same_file for suppressing "both files" warning
This is 100% reliable on Unix, and better on Windows. (In this commit I avoid reindenting things to make review easier; the formatting will be fixed in the next commit.) Fixes #13667
1 parent 91f3e45 commit 23440c0

File tree

3 files changed

+10
-21
lines changed

3 files changed

+10
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ rand.workspace = true
184184
regex.workspace = true
185185
rusqlite.workspace = true
186186
rustfix.workspace = true
187+
same-file.workspace = true
187188
semver.workspace = true
188189
serde = { workspace = true, features = ["derive"] }
189190
serde-untagged.workspace = true

src/cargo/util/context/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,36 +1537,32 @@ impl GlobalContext {
15371537
let possible = dir.join(filename_without_extension);
15381538
let possible_with_extension = dir.join(format!("{}.toml", filename_without_extension));
15391539

1540-
if possible.exists() {
1540+
if let Ok(possible_handle) = same_file::Handle::from_path(&possible) {
15411541
if warn {
1542+
if let Ok(possible_with_extension_handle) =
1543+
same_file::Handle::from_path(&possible_with_extension)
1544+
{
15421545
// We don't want to print a warning if the version
15431546
// without the extension is just a symlink to the version
15441547
// WITH an extension, which people may want to do to
15451548
// support multiple Cargo versions at once and not
15461549
// get a warning.
1547-
let skip_warning = if let Ok(target_path) = fs::read_link(&possible) {
1548-
target_path == possible_with_extension
1549-
} else {
1550-
false
1551-
};
1552-
1553-
if !skip_warning {
1554-
if possible_with_extension.exists() {
1550+
if possible_handle != possible_with_extension_handle {
15551551
self.shell().warn(format!(
15561552
"both `{}` and `{}` exist. Using `{}`",
15571553
possible.display(),
15581554
possible_with_extension.display(),
15591555
possible.display()
15601556
))?;
1561-
} else {
1557+
}
1558+
} else {
15621559
self.shell().warn(format!(
15631560
"`{}` is deprecated in favor of `{filename_without_extension}.toml`",
15641561
possible.display(),
15651562
))?;
15661563
self.shell().note(
15671564
format!("if you need to support cargo 1.38 or earlier, you can symlink `{filename_without_extension}` to `{filename_without_extension}.toml`"),
15681565
)?;
1569-
}
15701566
}
15711567
}
15721568

tests/testsuite/config.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,8 @@ f1 = 1
348348
assert_eq!(gctx.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
349349

350350
// It should NOT have warned for the symlink.
351-
// But, currently it does!
352351
let output = read_output(gctx);
353-
let expected = "\
354-
[WARNING] both `[..]/.cargo/config` and `[..]/.cargo/config.toml` exist. Using `[..]/.cargo/config`
355-
";
356-
assert_match(expected, &output);
352+
assert_match("", &output);
357353
}
358354

359355
#[cargo_test]
@@ -378,12 +374,8 @@ f1 = 1
378374
assert_eq!(gctx.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
379375

380376
// It should NOT have warned for this situation.
381-
// But, currently it does!
382377
let output = read_output(gctx);
383-
let expected = "\
384-
[WARNING] both `[..]/.cargo/config` and `[..]/.cargo/config.toml` exist. Using `[..]/.cargo/config`
385-
";
386-
assert_match(expected, &output);
378+
assert_match("", &output);
387379
}
388380

389381
#[cargo_test]

0 commit comments

Comments
 (0)