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
This commit is contained in:
Ian Jackson 2024-04-24 11:44:46 +01:00
parent 91f3e457ab
commit 23440c0dcd
3 changed files with 10 additions and 21 deletions

View File

@ -184,6 +184,7 @@ rand.workspace = true
regex.workspace = true
rusqlite.workspace = true
rustfix.workspace = true
same-file.workspace = true
semver.workspace = true
serde = { workspace = true, features = ["derive"] }
serde-untagged.workspace = true

View File

@ -1537,28 +1537,25 @@ impl GlobalContext {
let possible = dir.join(filename_without_extension);
let possible_with_extension = dir.join(format!("{}.toml", filename_without_extension));
if possible.exists() {
if let Ok(possible_handle) = same_file::Handle::from_path(&possible) {
if warn {
if let Ok(possible_with_extension_handle) =
same_file::Handle::from_path(&possible_with_extension)
{
// We don't want to print a warning if the version
// without the extension is just a symlink to the version
// WITH an extension, which people may want to do to
// support multiple Cargo versions at once and not
// get a warning.
let skip_warning = if let Ok(target_path) = fs::read_link(&possible) {
target_path == possible_with_extension
} else {
false
};
if !skip_warning {
if possible_with_extension.exists() {
if possible_handle != possible_with_extension_handle {
self.shell().warn(format!(
"both `{}` and `{}` exist. Using `{}`",
possible.display(),
possible_with_extension.display(),
possible.display()
))?;
} else {
}
} else {
self.shell().warn(format!(
"`{}` is deprecated in favor of `{filename_without_extension}.toml`",
possible.display(),
@ -1566,7 +1563,6 @@ impl GlobalContext {
self.shell().note(
format!("if you need to support cargo 1.38 or earlier, you can symlink `{filename_without_extension}` to `{filename_without_extension}.toml`"),
)?;
}
}
}

View File

@ -348,12 +348,8 @@ f1 = 1
assert_eq!(gctx.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
// It should NOT have warned for the symlink.
// But, currently it does!
let output = read_output(gctx);
let expected = "\
[WARNING] both `[..]/.cargo/config` and `[..]/.cargo/config.toml` exist. Using `[..]/.cargo/config`
";
assert_match(expected, &output);
assert_match("", &output);
}
#[cargo_test]
@ -378,12 +374,8 @@ f1 = 1
assert_eq!(gctx.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
// It should NOT have warned for this situation.
// But, currently it does!
let output = read_output(gctx);
let expected = "\
[WARNING] both `[..]/.cargo/config` and `[..]/.cargo/config.toml` exist. Using `[..]/.cargo/config`
";
assert_match(expected, &output);
assert_match("", &output);
}
#[cargo_test]