Skip to content

Commit 32b17f3

Browse files
committed
Auto merge of #13793 - ijackson:symlink, r=epage
Fix warning suppression for config.toml vs config compat symlinks ### What does this PR try to resolve? Background: the cargo config file is being renamed from `.cargo/config` to `.cargo/config.toml`. There's code in new cargo to look for both files (for compatibility), to issue a warning when onliy the old filename is found, and also to issue a warning if both files are found. The warning suggests making a symlink if compatibility with old cargo is wanted. An attempt was made to detect when both the old and new files exists, but one is a symlink to the other, but as reported in #13667, this code is not effective. (It would work only if the symlink had the precise absolute pathname that cargo has decided to use for the lookup, which would be an unnatural way to make the link.) Logically, the warning should appear when both files exist *but are different*. That is the anomalous situation that will generate confusing behaviour. By "different" we ought to mean "aren't the very same file". That's what this MR implements, where possible. On Unix, we use the information from stat(2). That's not available on other platforms; on those, we arrange to also tolerate a symlink referring to precisely `config.toml` as a relative pathname, which is also fine, since by definition the target is then in the same directrory as `config`. Fixes #13667. ### How should we test and review this PR? I have interleaved the new tests with the commits that support them. In each case, a functional commit is followed by a test which fails just beforehand. (This can be observed by experimentally reordering the branch.) I have also done ad-hoc testing. ### Additional information I'm making the assumption that a symlink containing a relative path does something sane on Windows. This assumption may be unwarranted. If so, "Handle `config` -> `config.toml` (without full path)" needs to be dropped, and the test case needs to be `#[cfg(unix)]`. But also, in this case, we should probably put some warnings in the stdlib docs!
2 parents 70fb498 + 2f16838 commit 32b17f3

File tree

3 files changed

+90
-24
lines changed

3 files changed

+90
-24
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ rand.workspace = true
183183
regex.workspace = true
184184
rusqlite.workspace = true
185185
rustfix.workspace = true
186+
same-file.workspace = true
186187
semver.workspace = true
187188
serde = { workspace = true, features = ["derive"] }
188189
serde-untagged.workspace = true

src/cargo/util/context/mod.rs

+18-22
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-
// We don't want to print a warning if the version
1543-
// without the extension is just a symlink to the version
1544-
// WITH an extension, which people may want to do to
1545-
// support multiple Cargo versions at once and not
1546-
// 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() {
1542+
if let Ok(possible_with_extension_handle) =
1543+
same_file::Handle::from_path(&possible_with_extension)
1544+
{
1545+
// We don't want to print a warning if the version
1546+
// without the extension is just a symlink to the version
1547+
// WITH an extension, which people may want to do to
1548+
// support multiple Cargo versions at once and not
1549+
// get a warning.
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 {
1562-
self.shell().warn(format!(
1563-
"`{}` is deprecated in favor of `{filename_without_extension}.toml`",
1564-
possible.display(),
1565-
))?;
1566-
self.shell().note(
1567-
format!("if you need to support cargo 1.38 or earlier, you can symlink `{filename_without_extension}` to `{filename_without_extension}.toml`"),
1568-
)?;
15691557
}
1558+
} else {
1559+
self.shell().warn(format!(
1560+
"`{}` is deprecated in favor of `{filename_without_extension}.toml`",
1561+
possible.display(),
1562+
))?;
1563+
self.shell().note(
1564+
format!("if you need to support cargo 1.38 or earlier, you can symlink `{filename_without_extension}` to `{filename_without_extension}.toml`"),
1565+
)?;
15701566
}
15711567
}
15721568

tests/testsuite/config.rs

+71-2
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,29 @@ fn symlink_file(target: &Path, link: &Path) -> io::Result<()> {
184184
os::windows::fs::symlink_file(target, link)
185185
}
186186

187-
fn symlink_config_to_config_toml() {
187+
fn make_config_symlink_to_config_toml_absolute() {
188188
let toml_path = paths::root().join(".cargo/config.toml");
189189
let symlink_path = paths::root().join(".cargo/config");
190190
t!(symlink_file(&toml_path, &symlink_path));
191191
}
192192

193+
fn make_config_symlink_to_config_toml_relative() {
194+
let symlink_path = paths::root().join(".cargo/config");
195+
t!(symlink_file(Path::new("config.toml"), &symlink_path));
196+
}
197+
198+
fn rename_config_toml_to_config_replacing_with_symlink() {
199+
let root = paths::root();
200+
t!(fs::rename(
201+
root.join(".cargo/config.toml"),
202+
root.join(".cargo/config")
203+
));
204+
t!(symlink_file(
205+
Path::new("config"),
206+
&root.join(".cargo/config.toml")
207+
));
208+
}
209+
193210
#[track_caller]
194211
pub fn assert_error<E: Borrow<anyhow::Error>>(error: E, msgs: &str) {
195212
let causes = error
@@ -298,7 +315,33 @@ f1 = 1
298315
",
299316
);
300317

301-
symlink_config_to_config_toml();
318+
make_config_symlink_to_config_toml_absolute();
319+
320+
let gctx = new_gctx();
321+
322+
assert_eq!(gctx.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
323+
324+
// It should NOT have warned for the symlink.
325+
let output = read_output(gctx);
326+
assert_match("", &output);
327+
}
328+
329+
#[cargo_test]
330+
fn config_ambiguous_filename_symlink_doesnt_warn_relative() {
331+
// Windows requires special permissions to create symlinks.
332+
// If we don't have permission, just skip this test.
333+
if !symlink_supported() {
334+
return;
335+
};
336+
337+
write_config_toml(
338+
"\
339+
[foo]
340+
f1 = 1
341+
",
342+
);
343+
344+
make_config_symlink_to_config_toml_relative();
302345

303346
let gctx = new_gctx();
304347

@@ -309,6 +352,32 @@ f1 = 1
309352
assert_match("", &output);
310353
}
311354

355+
#[cargo_test]
356+
fn config_ambiguous_filename_symlink_doesnt_warn_backward() {
357+
// Windows requires special permissions to create symlinks.
358+
// If we don't have permission, just skip this test.
359+
if !symlink_supported() {
360+
return;
361+
};
362+
363+
write_config_toml(
364+
"\
365+
[foo]
366+
f1 = 1
367+
",
368+
);
369+
370+
rename_config_toml_to_config_replacing_with_symlink();
371+
372+
let gctx = new_gctx();
373+
374+
assert_eq!(gctx.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
375+
376+
// It should NOT have warned for this situation.
377+
let output = read_output(gctx);
378+
assert_match("", &output);
379+
}
380+
312381
#[cargo_test]
313382
fn config_ambiguous_filename() {
314383
write_config_extless(

0 commit comments

Comments
 (0)