Repository: rust-lang/cargo (rust)

Issue:
Duplicate warnings of `config` on Windows OS When using git-bash
### Problem

This only happend when I use the git bash and in the /tmp folder

Recently, I found the `cargo` on Windows will output the warnings of `config` twice 

1、with only `config` exists

```
cargo -vV
warning: `C:\Users\ticktockit\.cargo\config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
warning: `C:\Users\ticktockit\.cargo\config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
cargo 1.83.0-nightly (c1fa840a8 2024-08-29)
release: 1.83.0-nightly
commit-hash: c1fa840a85eca53818895901a53fae34247448b2
commit-date: 2024-08-29
host: x86_64-pc-windows-msvc
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:Schannel)
os: Windows 10.0.22631 (Windows 11 CoreCountrySpecific) [64-bit]
``` 

2. both `config` and `config.toml` exist

```
cargo -vV
warning: both `C:\Users\ticktockit\.cargo\config` and `C:\Users\ticktockit\.cargo\config.toml` exist. Using `C:\Users\ticktockit\.cargo\config`
warning: both `C:\Users\ticktockit\.cargo\config` and `C:\Users\ticktockit\.cargo\config.toml` exist. Using `C:\Users\ticktockit\.cargo\config`
cargo 1.83.0-nightly (c1fa840a8 2024-08-29)
release: 1.83.0-nightly
commit-hash: c1fa840a85eca53818895901a53fae34247448b2
commit-date: 2024-08-29
host: x86_64-pc-windows-msvc
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:Schannel)
os: Windows 10.0.22631 (Windows 11 CoreCountrySpecific) [64-bit]
```

### Steps

1. make sure the  `C:\Users\[Your PC user account]\.cargo\config` exists
2. using the `git-bash`
3. cd `/tmp`
4. run `cargo -vV` 

### Possible Solution(s)

It is not clear if this problem still exists in the latest version, but duplicate information needs to be removed

### Notes

_No response_

### Version

cargo 1.83.0-nightly (c1fa840a8 2024-08-29)
release: 1.83.0-nightly
commit-hash: c1fa840a85eca53818895901a53fae34247448b2
commit-date: 2024-08-29
host: x86_64-pc-windows-msvc
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:Schannel)
os: Windows 10.0.22631 (Windows 11 CoreCountrySpecific) [64-bit]

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"linyihai"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->

Pull request description:
fix(config): Don't double-warn about `$CARGO_HOME/config`
### What does this PR try to resolve?

The core requirements for this bug are
- You have a `$CARGO_HOME/.config`
- Your project is inside of `$HOME`

We have a check to make sure we don't double-walk `$CARGO/config` but
that check is *after* we warn about there being no `.toml` extension.

To fix this, we just need to move that check outside of the file lookup.
This required changing the `seen` check from checking whether walked the
config file to checking if we've walked the config dir.  As we only have
one file per directory, this should work.

Fixes #14560

### How should we test and review this PR?

test commit added the test, fix commit fixed the issue.

### Additional information


Patch:
diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs
index 83c918a03..8e9e2ac21 100644
--- a/src/cargo/core/compiler/mod.rs
+++ b/src/cargo/core/compiler/mod.rs
@@ -694,11 +694,6 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
     base.inherit_jobserver(&build_runner.jobserver);
     build_deps_args(&mut base, build_runner, unit)?;
     add_cap_lints(build_runner.bcx, unit, &mut base);
-    if cargo_rustc_higher_args_precedence(build_runner) {
-        if let Some(args) = build_runner.bcx.extra_args_for(unit) {
-            base.args(args);
-        }
-    }
     base.args(&unit.rustflags);
     if build_runner.bcx.gctx.cli_unstable().binary_dep_depinfo {
         base.arg("-Z").arg("binary-dep-depinfo");
@@ -758,11 +753,8 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
     }
 
     rustdoc.args(unit.pkg.manifest().lint_rustflags());
-
-    if !cargo_rustc_higher_args_precedence(build_runner) {
-        if let Some(args) = build_runner.bcx.extra_args_for(unit) {
-            rustdoc.args(args);
-        }
+    if let Some(args) = build_runner.bcx.extra_args_for(unit) {
+        rustdoc.args(args);
     }
 
     let metadata = build_runner.metadata_for_doc_units[unit];
@@ -803,11 +795,6 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
 
     rustdoc::add_output_format(build_runner, unit, &mut rustdoc)?;
 
-    if cargo_rustc_higher_args_precedence(build_runner) {
-        if let Some(args) = build_runner.bcx.extra_args_for(unit) {
-            rustdoc.args(args);
-        }
-    }
     rustdoc.args(&unit.rustdocflags);
 
     if !crate_version_flag_already_present(&rustdoc) {
@@ -1110,10 +1097,8 @@ fn build_base_args(
 
     cmd.args(unit.pkg.manifest().lint_rustflags());
     cmd.args(&profile_rustflags);
-    if !cargo_rustc_higher_args_precedence(build_runner) {
-        if let Some(args) = build_runner.bcx.extra_args_for(unit) {
-            cmd.args(args);
-        }
+    if let Some(args) = build_runner.bcx.extra_args_for(unit) {
+        cmd.args(args);
     }
 
     // `-C overflow-checks` is implied by the setting of `-C debug-assertions`,
@@ -1984,19 +1969,3 @@ fn scrape_output_path(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoR
         .outputs(unit)
         .map(|outputs| outputs[0].path.clone())
 }
-
-/// Provides a way to change the precedence of `cargo rustc -- <flags>`.
-///
-/// This is intended to be a short-live function.
-///
-/// See <https://github.com/rust-lang/cargo/issues/14346>
-fn cargo_rustc_higher_args_precedence(build_runner: &BuildRunner<'_, '_>) -> bool {
-    build_runner.bcx.gctx.nightly_features_allowed
-        && build_runner
-            .bcx
-            .gctx
-            .get_env("__CARGO_RUSTC_ORIG_ARGS_PRIO")
-            .ok()
-            .as_deref()
-            != Some("1")
-}
diff --git a/src/cargo/util/context/mod.rs b/src/cargo/util/context/mod.rs
index 1db5b9a3c..ab283aa24 100644
--- a/src/cargo/util/context/mod.rs
+++ b/src/cargo/util/context/mod.rs
@@ -1581,20 +1581,21 @@ impl GlobalContext {
     where
         F: FnMut(&Path) -> CargoResult<()>,
     {
-        let mut stash: HashSet<PathBuf> = HashSet::new();
+        let mut seen_dir = HashSet::new();
 
         for current in paths::ancestors(pwd, self.search_stop_path.as_deref()) {
-            if let Some(path) = self.get_file_path(&current.join(".cargo"), "config", true)? {
+            let config_root = current.join(".cargo");
+            if let Some(path) = self.get_file_path(&config_root, "config", true)? {
                 walk(&path)?;
-                stash.insert(path);
             }
+            seen_dir.insert(config_root);
         }
 
         // Once we're done, also be sure to walk the home directory even if it's not
         // in our history to be sure we pick up that standard location for
         // information.
-        if let Some(path) = self.get_file_path(home, "config", true)? {
-            if !stash.contains(&path) {
+        if !seen_dir.contains(home) {
+            if let Some(path) = self.get_file_path(home, "config", true)? {
                 walk(&path)?;
             }
         }


Test patch:
diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs
index 6d0a3ff8e..8f9092c2f 100644
--- a/tests/testsuite/config.rs
+++ b/tests/testsuite/config.rs
@@ -16,7 +16,7 @@ use cargo::CargoResult;
 use cargo_test_support::compare::assert_e2e;
 use cargo_test_support::prelude::*;
 use cargo_test_support::str;
-use cargo_test_support::{paths, project, symlink_supported, t};
+use cargo_test_support::{paths, project, project_in_home, symlink_supported, t};
 use cargo_util_schemas::manifest::TomlTrimPaths;
 use cargo_util_schemas::manifest::TomlTrimPathsValue;
 use cargo_util_schemas::manifest::{self as cargo_toml, TomlDebugInfo, VecStringOrBool as VSOB};
@@ -299,6 +299,26 @@ f1 = 1
     assert_e2e().eq(&output, expected);
 }
 
+#[cargo_test]
+fn home_config_works_without_extension() {
+    write_config_at(
+        paths::cargo_home().join("config"),
+        "\
+[foo]
+f1 = 1
+",
+    );
+    let p = project_in_home("foo").file("src/lib.rs", "").build();
+
+    p.cargo("-vV")
+        .with_stderr_data(str![[r#"
+[WARNING] `[ROOT]/home/.cargo/config` is deprecated in favor of `config.toml`
+[NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
+
+"#]])
+        .run();
+}
+
 #[cargo_test]
 fn config_ambiguous_filename_symlink_doesnt_warn() {
     // Windows requires special permissions to create symlinks.
diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs
index 71ca898ae..be01f1465 100644
--- a/tests/testsuite/rustc.rs
+++ b/tests/testsuite/rustc.rs
@@ -795,66 +795,3 @@ windows
         )
         .run();
 }
-
-#[cargo_test]
-fn precedence() {
-    // Ensure that the precedence of cargo-rustc is only lower than RUSTFLAGS,
-    // but higher than most flags set by cargo.
-    //
-    // See rust-lang/cargo#14346
-    let p = project()
-        .file(
-            "Cargo.toml",
-            r#"
-            [package]
-            name = "foo"
-            edition = "2021"
-
-            [lints.rust]
-            unexpected_cfgs = "allow"
-            "#,
-        )
-        .file("src/lib.rs", "")
-        .build();
-
-    p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
-        .env("RUSTFLAGS", "--cfg from_rustflags")
-        .masquerade_as_nightly_cargo(&["cargo-rustc-precedence"])
-        .with_stderr_data(str![[r#"
-[COMPILING] foo v0.0.0 ([ROOT]/foo)
-[RUNNING] `rustc [..]-C strip=debuginfo [..]--cfg cargo_rustc -C strip=symbols --cfg from_rustflags`
-[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
-
-"#]])
-        .run();
-
-    // Ensure the short-live env var to work
-    p.cargo("clean").run();
-    p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
-        .env("RUSTFLAGS", "--cfg from_rustflags")
-        .env("__CARGO_RUSTC_ORIG_ARGS_PRIO", "1")
-        .masquerade_as_nightly_cargo(&["cargo-rustc-precedence"])
-        .with_stderr_data(
-            str![[r#"
-[COMPILING] foo v0.0.0 ([ROOT]/foo)
-[RUNNING] `rustc [..]--cfg cargo_rustc -C strip=symbols [..]-C strip=debuginfo [..]--cfg from_rustflags`
-[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
-
-"#]]
-        )
-        .run();
-
-    // Ensure non-nightly to work as before
-    p.cargo("clean").run();
-    p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
-        .env("RUSTFLAGS", "--cfg from_rustflags")
-        .with_stderr_data(
-            str![[r#"
-[COMPILING] foo v0.0.0 ([ROOT]/foo)
-[RUNNING] `rustc [..]--cfg cargo_rustc -C strip=symbols [..]-C strip=debuginfo [..]--cfg from_rustflags`
-[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
-
-"#]]
-        )
-        .run();
-}