Repository: rust-lang/cargo (rust)

Issue:
add: Use single quotes for target headers
### Problem

Running `cargo add` with the `--target` option generates TOML code with double quotes, resulting in backslash-escaped double quotes in the target expression:

```toml
[target."cfg(target_os = \"linux\")".dependencies]
```

This is a bit difficult to read.

### Proposed Solution

Use single quotes instead:

```toml
[target.'cfg(target_os = "linux")'.dependencies]
```

### Notes

This matches the formatting used in the [Platform-specific dependencies](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies) section of the Cargo book.

Pull request:
fix(add): Avoid escaping double-quotes by using string literals 
### What does this PR try to resolve?

Fixes #14002

### How should we test and review this PR?



### Additional information



Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 07f76e8ec..e2a42023b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3119,9 +3119,9 @@ dependencies = [
 
 [[package]]
 name = "serde_spanned"
-version = "0.6.5"
+version = "0.6.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
+checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0"
 dependencies = [
  "serde",
 ]
@@ -3444,9 +3444,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
 
 [[package]]
 name = "toml"
-version = "0.8.12"
+version = "0.8.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3"
+checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335"
 dependencies = [
  "serde",
  "serde_spanned",
@@ -3456,18 +3456,18 @@ dependencies = [
 
 [[package]]
 name = "toml_datetime"
-version = "0.6.5"
+version = "0.6.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
+checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf"
 dependencies = [
  "serde",
 ]
 
 [[package]]
 name = "toml_edit"
-version = "0.22.12"
+version = "0.22.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef"
+checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38"
 dependencies = [
  "indexmap",
  "serde",
diff --git a/Cargo.toml b/Cargo.toml
index b88e0b3fd..ae1f13a62 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -96,8 +96,8 @@ tar = { version = "0.4.40", default-features = false }
 tempfile = "3.10.1"
 thiserror = "1.0.59"
 time = { version = "0.3.36", features = ["parsing", "formatting", "serde"] }
-toml = "0.8.12"
-toml_edit = { version = "0.22.12", features = ["serde"] }
+toml = "0.8.14"
+toml_edit = { version = "0.22.14", features = ["serde"] }
 tracing = "0.1.40" # be compatible with rustc_log: https://github.com/rust-lang/rust/blob/e51e98dde6a/compiler/rustc_log/Cargo.toml#L9
 tracing-chrome = "0.7.2"
 tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }


Test patch:
diff --git a/crates/resolver-tests/README.md b/crates/resolver-tests/README.md
deleted file mode 100644
index 4f84467bd..000000000
--- a/crates/resolver-tests/README.md
+++ /dev/null
@@ -1,13 +0,0 @@
-# resolver-tests
-
-## The aim
-
-This crate aims to test the resolution of Cargo's resolver. It implements a [SAT solver](https://en.wikipedia.org/wiki/SAT_solver) to compare with resolution of Cargo's resolver.    
-This ensures that Cargo's dependency resolution is proven valid by lowering to [SAT problem](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem). 
-
-## About the test
-
-The Cargo's resolver is very sensitive to what order it tries to evaluate constraints. This makes it incredibly difficult     
-to be sure that a handful of tests actually covers all the important permutations of decision-making. The tests not only needs    
-to hit all the corner cases, it needs to try all of the orders of evaluation. So we use fuzz testing to cover more permutations.    
-
diff --git a/crates/resolver-tests/src/lib.rs b/crates/resolver-tests/src/lib.rs
index 43dc49dd3..1ced997bd 100644
--- a/crates/resolver-tests/src/lib.rs
+++ b/crates/resolver-tests/src/lib.rs
@@ -31,7 +31,6 @@ pub fn resolve(deps: Vec<Dependency>, registry: &[Summary]) -> CargoResult<Vec<P
     resolve_with_global_context(deps, registry, &GlobalContext::default().unwrap())
 }
 
-// Verify that the resolution of cargo resolver can pass the verification of SAT
 pub fn resolve_and_validated(
     deps: Vec<Dependency>,
     registry: &[Summary],
@@ -199,9 +198,6 @@ fn log_bits(x: usize) -> usize {
     (num_bits::<usize>() as u32 - x.leading_zeros()) as usize
 }
 
-// At this point is possible to select every version of every package.
-// So we need to mark certain versions as incompatible with each other.
-// We could add a clause not A, not B for all A and B that are incompatible,
 fn sat_at_most_one(solver: &mut impl varisat::ExtendFormula, vars: &[varisat::Var]) {
     if vars.len() <= 1 {
         return;
@@ -214,8 +210,6 @@ fn sat_at_most_one(solver: &mut impl varisat::ExtendFormula, vars: &[varisat::Va
         solver.add_clause(&[vars[1].negative(), vars[2].negative()]);
         return;
     }
-    // There are more efficient ways to do it for large numbers of versions.
-    //
     // use the "Binary Encoding" from
     // https://www.it.uu.se/research/group/astra/ModRef10/papers/Alan%20M.%20Frisch%20and%20Paul%20A.%20Giannoros.%20SAT%20Encodings%20of%20the%20At-Most-k%20Constraint%20-%20ModRef%202010.pdf
     let bits: Vec<varisat::Var> = solver.new_var_iter(log_bits(vars.len())).collect();
@@ -263,7 +257,6 @@ struct SatResolveInner {
 impl SatResolve {
     pub fn new(registry: &[Summary]) -> Self {
         let mut cnf = varisat::CnfFormula::new();
-        // That represents each package version which is set to "true" if the packages in the lock file and "false" if it is unused.
         let var_for_is_packages_used: HashMap<PackageId, varisat::Var> = registry
             .iter()
             .map(|s| (s.package_id(), cnf.new_var()))
@@ -297,10 +290,6 @@ impl SatResolve {
 
         let empty_vec = vec![];
 
-        // Now different versions can conflict, but dependencies might not be selected.
-        // So we need to add clauses that ensure that if a package is selected for each dependency a version
-        // that satisfies that dependency is selected.
-        //
         // active packages need each of there `deps` to be satisfied
         for p in registry.iter() {
             for dep in p.dependencies() {
@@ -692,7 +681,7 @@ pub fn registry_strategy(
             vers
         });
 
-    // each version of each crate can depend on each crate smaller than it.
+    // each version of each crate can depend on each crate smaller then it.
     // In theory shrinkage should be 2, but in practice we get better trees with a larger value.
     let max_deps = max_versions * (max_crates * (max_crates - 1)) / shrinkage;
 
diff --git a/tests/testsuite/bad_config.rs b/tests/testsuite/bad_config.rs
index 3ed3cce12..15b718d4f 100644
--- a/tests/testsuite/bad_config.rs
+++ b/tests/testsuite/bad_config.rs
@@ -2922,16 +2922,16 @@ fn bad_target_cfg() {
         .with_stderr(
             "\
 [ERROR] error in [..]/foo/.cargo/config.toml: \
-could not load config key `target.\"cfg(not(target_os = \\\"none\\\"))\".runner`
+could not load config key `target.'cfg(not(target_os = \"none\"))'.runner`
 
 Caused by:
   error in [..]/foo/.cargo/config.toml: \
-  could not load config key `target.\"cfg(not(target_os = \\\"none\\\"))\".runner`
+  could not load config key `target.'cfg(not(target_os = \"none\"))'.runner`
 
 Caused by:
-  invalid configuration for key `target.\"cfg(not(target_os = \\\"none\\\"))\".runner`
+  invalid configuration for key `target.'cfg(not(target_os = \"none\"))'.runner`
   expected a string or array of strings, but found a boolean for \
-  `target.\"cfg(not(target_os = \\\"none\\\"))\".runner` in [..]/foo/.cargo/config.toml
+  `target.'cfg(not(target_os = \"none\"))'.runner` in [..]/foo/.cargo/config.toml
 ",
         )
         .run();
diff --git a/tests/testsuite/cargo_add/target_cfg/mod.rs b/tests/testsuite/cargo_add/target_cfg/mod.rs
index 762816b03..90acfc246 100644
--- a/tests/testsuite/cargo_add/target_cfg/mod.rs
+++ b/tests/testsuite/cargo_add/target_cfg/mod.rs
@@ -28,7 +28,7 @@ fn case() {
 
     snapbox::cmd::Command::cargo_ui()
         .arg("add")
-        .arg_line("my-package1 my-package2 --target cfg(unix)")
+        .arg_line("my-package1 my-package2 --target 'cfg(target_os=\"linux\")'")
         .current_dir(cwd)
         .assert()
         .success()
diff --git a/tests/testsuite/cargo_add/target_cfg/out/Cargo.toml b/tests/testsuite/cargo_add/target_cfg/out/Cargo.toml
index 7779fcabd..3977ca84f 100644
--- a/tests/testsuite/cargo_add/target_cfg/out/Cargo.toml
+++ b/tests/testsuite/cargo_add/target_cfg/out/Cargo.toml
@@ -5,6 +5,6 @@ name = "cargo-list-test-fixture"
 version = "0.0.0"
 edition = "2015"
 
-[target."cfg(unix)".dependencies]
+[target.'cfg(target_os="linux")'.dependencies]
 my-package1 = "99999.0.0"
 my-package2 = "99999.0.0"
diff --git a/tests/testsuite/cargo_add/target_cfg/stderr.term.svg b/tests/testsuite/cargo_add/target_cfg/stderr.term.svg
index 5a05eb01b..491ed7991 100644
--- a/tests/testsuite/cargo_add/target_cfg/stderr.term.svg
+++ b/tests/testsuite/cargo_add/target_cfg/stderr.term.svg
@@ -1,4 +1,4 @@
-<svg width="740px" height="110px" xmlns="http://www.w3.org/2000/svg">
+<svg width="751px" height="110px" xmlns="http://www.w3.org/2000/svg">
   <style>
     .fg { fill: #AAAAAA }
     .bg { background: #000000 }
@@ -20,9 +20,9 @@
   <text xml:space="preserve" class="container fg">
     <tspan x="10px" y="28px"><tspan class="fg-green bold">    Updating</tspan><tspan> `dummy-registry` index</tspan>
 </tspan>
-    <tspan x="10px" y="46px"><tspan class="fg-green bold">      Adding</tspan><tspan> my-package1 v99999.0.0 to dependencies for target `cfg(unix)`</tspan>
+    <tspan x="10px" y="46px"><tspan class="fg-green bold">      Adding</tspan><tspan> my-package1 v99999.0.0 to dependencies for target `cfg(target_os="linux")`</tspan>
 </tspan>
-    <tspan x="10px" y="64px"><tspan class="fg-green bold">      Adding</tspan><tspan> my-package2 v99999.0.0 to dependencies for target `cfg(unix)`</tspan>
+    <tspan x="10px" y="64px"><tspan class="fg-green bold">      Adding</tspan><tspan> my-package2 v99999.0.0 to dependencies for target `cfg(target_os="linux")`</tspan>
 </tspan>
     <tspan x="10px" y="82px"><tspan class="fg-green bold">     Locking</tspan><tspan> 3 packages to latest compatible versions</tspan>
 </tspan>
diff --git a/tests/testsuite/cargo_config/mod.rs b/tests/testsuite/cargo_config/mod.rs
index c1769fb53..e4ce992c3 100644
--- a/tests/testsuite/cargo_config/mod.rs
+++ b/tests/testsuite/cargo_config/mod.rs
@@ -88,7 +88,7 @@ build.rustflags = [\"--flag-global\", \"--flag-directory\"]
 extra-table.somekey = \"somevalue\"
 profile.dev.opt-level = 3
 profile.dev.package.foo.opt-level = 1
-target.\"cfg(target_os = \\\"linux\\\")\".runner = \"runme\"
+target.'cfg(target_os = \"linux\")'.runner = \"runme\"
 # The following environment variables may affect the loaded values.
 # CARGO_ALIAS_BAR=[..]cat dog[..]
 # CARGO_BUILD_JOBS=100
@@ -265,7 +265,7 @@ build.rustflags = [
 extra-table.somekey = \"somevalue\" # [ROOT]/home/.cargo/config.toml
 profile.dev.opt-level = 3 # [ROOT]/home/.cargo/config.toml
 profile.dev.package.foo.opt-level = 1 # [ROOT]/home/.cargo/config.toml
-target.\"cfg(target_os = \\\"linux\\\")\".runner = \"runme\" # [ROOT]/home/.cargo/config.toml
+target.'cfg(target_os = \"linux\")'.runner = \"runme\" # [ROOT]/home/.cargo/config.toml
 # The following environment variables may affect the loaded values.
 # CARGO_HOME=[ROOT]/home/.cargo
 ",
@@ -361,7 +361,7 @@ build.rustflags = [\"--flag-global\"]
 extra-table.somekey = \"somevalue\"
 profile.dev.opt-level = 3
 profile.dev.package.foo.opt-level = 1
-target.\"cfg(target_os = \\\"linux\\\")\".runner = \"runme\"
+target.'cfg(target_os = \"linux\")'.runner = \"runme\"
 
 ",
         )
@@ -513,7 +513,7 @@ build.rustflags = [\"--flag-global\"]
 extra-table.somekey = \"somevalue\"
 profile.dev.opt-level = 3
 profile.dev.package.foo.opt-level = 1
-target.\"cfg(target_os = \\\"linux\\\")\".runner = \"runme\"
+target.'cfg(target_os = \"linux\")'.runner = \"runme\"
 
 ",
         )