Skip to content

Commit b398b9c

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 rust-lang#13667
1 parent 16f83db commit b398b9c

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
@@ -1542,36 +1542,32 @@ impl GlobalContext {
15421542
//
15431543
// Instead, if we got an error, we should bail, but perhaps
15441544
// that might be too much risk of being a breaking change.
1545-
if possible.exists() {
1545+
if let Ok(possible_handle) = same_file::Handle::from_path(&possible) {
15461546
if warn {
1547+
if let Ok(possible_with_extension_handle) =
1548+
same_file::Handle::from_path(&possible_with_extension)
1549+
{
15471550
// We don't want to print a warning if the version
15481551
// without the extension is just a symlink to the version
15491552
// WITH an extension, which people may want to do to
15501553
// support multiple Cargo versions at once and not
15511554
// get a warning.
1552-
let skip_warning = if let Ok(target_path) = fs::read_link(&possible) {
1553-
target_path == possible_with_extension
1554-
} else {
1555-
false
1556-
};
1557-
1558-
if !skip_warning {
1559-
if possible_with_extension.exists() {
1555+
if possible_handle != possible_with_extension_handle {
15601556
self.shell().warn(format!(
15611557
"both `{}` and `{}` exist. Using `{}`",
15621558
possible.display(),
15631559
possible_with_extension.display(),
15641560
possible.display()
15651561
))?;
1566-
} else {
1562+
}
1563+
} else {
15671564
self.shell().warn(format!(
15681565
"`{}` is deprecated in favor of `{filename_without_extension}.toml`",
15691566
possible.display(),
15701567
))?;
15711568
self.shell().note(
15721569
format!("if you need to support cargo 1.38 or earlier, you can symlink `{filename_without_extension}` to `{filename_without_extension}.toml`"),
15731570
)?;
1574-
}
15751571
}
15761572
}
15771573

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,13 +374,9 @@ 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);
383378
assert_match("", &output);
384-
let expected = "\
385-
[WARNING] both `[..]/.cargo/config` and `[..]/.cargo/config.toml` exist. Using `[..]/.cargo/config`
386-
";
387-
assert_match(expected, &output);
379+
assert_match("", &output);
388380
}
389381

390382
#[cargo_test]

0 commit comments

Comments
 (0)