Skip to content

Commit 656f934

Browse files
committed
Fix issue-11010
1 parent 35ea623 commit 656f934

File tree

16 files changed

+131
-0
lines changed

16 files changed

+131
-0
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::collections::BTreeSet;
77
use std::collections::VecDeque;
88
use std::fmt::Write;
99
use std::path::Path;
10+
use std::str::FromStr;
1011

1112
use anyhow::Context as _;
1213
use cargo_util::paths;
@@ -196,6 +197,14 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
196197
print_dep_table_msg(&mut options.config.shell(), &dep)?;
197198

198199
manifest.insert_into_table(&dep_table, &dep)?;
200+
if let Some(option) = dep.optional {
201+
let rust_version_check = check_rust_version_for_optional_dependency(
202+
workspace.current().unwrap().rust_version(),
203+
)?;
204+
if option && rust_version_check {
205+
manifest.insert_into_feature_table(&dep)?;
206+
}
207+
}
199208
manifest.gc_dep(dep.toml_key());
200209
}
201210

@@ -469,6 +478,26 @@ fn check_invalid_ws_keys(toml_key: &str, arg: &DepOp) -> CargoResult<()> {
469478
Ok(())
470479
}
471480

481+
/// When the `--optional` option is added using `cargo add`, we need to
482+
/// check the current rust-version. As the `dep:` syntax is only avaliable
483+
/// starting with Rust 1.60.0
484+
///
485+
/// `true` means that the rust-version is None or the rust-version is higher
486+
/// than the version needed.
487+
///
488+
/// Note: Previous versions can only use the implicit feature name.
489+
fn check_rust_version_for_optional_dependency(
490+
rust_version: Option<&RustVersion>,
491+
) -> CargoResult<bool> {
492+
match rust_version {
493+
Some(version) => {
494+
let syntax_support_version = RustVersion::from_str("1.60.0")?;
495+
Ok(version.cmp(&syntax_support_version).is_ge())
496+
}
497+
None => Ok(true),
498+
}
499+
}
500+
472501
/// Provide the existing dependency for the target table
473502
///
474503
/// If it doesn't exist but exists in another table, let's use that as most likely users

src/cargo/util/toml_mut/dependency.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,47 @@ impl Display for WorkspaceSource {
921921
}
922922
}
923923

924+
impl Dependency {
925+
/// Convert denpency as feature to TOML.
926+
/// Panics if the path is relative
927+
pub fn to_toml_as_feature(&self, crate_root: &Path) -> toml_edit::Item {
928+
assert!(
929+
crate_root.is_absolute(),
930+
"Absolute path needed, got: {}",
931+
crate_root.display()
932+
);
933+
let features: toml_edit::Value = vec![self.as_feature()].iter().collect();
934+
toml_edit::value(features)
935+
}
936+
937+
pub fn as_feature(&self) -> String {
938+
if let Some(rename) = &self.rename {
939+
format!("dep:{}", rename)
940+
} else {
941+
format!("dep:{}", self.name)
942+
}
943+
}
944+
945+
/// Check whether `dep:<dep>` is defined in the features section.
946+
///
947+
/// `true` if there is a define `dep:<dep>`
948+
/// `false` if `dep:<dep>` is not used ever.
949+
///
950+
/// Make sure that `dep:<dep>` is included in one of features in the features table.
951+
pub fn is_active_as_feature(&self, item: &mut toml_edit::Item) -> bool {
952+
item.as_table_like_mut().unwrap().iter().any(|(_, values)| {
953+
if let Some(values) = &values.as_array() {
954+
values.iter().any(|value| match value.as_str() {
955+
Some(val) => val.to_string().eq(&self.as_feature()),
956+
None => false,
957+
})
958+
} else {
959+
false
960+
}
961+
})
962+
}
963+
}
964+
924965
#[cfg(test)]
925966
mod tests {
926967
use std::path::Path;

src/cargo/util/toml_mut/manifest.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,25 @@ impl LocalManifest {
363363
Ok(())
364364
}
365365

366+
/// Add feature entry to a Cargo.toml.
367+
pub fn insert_into_feature_table(&mut self, dep: &Dependency) -> CargoResult<()> {
368+
let crate_root = self
369+
.path
370+
.parent()
371+
.expect("manifest path is absolute")
372+
.to_owned();
373+
let dep_key = dep.toml_key();
374+
let table = self.get_table_mut(&vec![String::from("features")])?;
375+
376+
// Check whether `dep:<dep>` is defined in the [features] section.
377+
if !dep.is_active_as_feature(table) {
378+
let new_feature = dep.to_toml_as_feature(&crate_root);
379+
table[dep_key] = new_feature;
380+
}
381+
382+
Ok(())
383+
}
384+
366385
/// Remove entry from a Cargo.toml.
367386
pub fn remove_from_table(&mut self, table_path: &[String], name: &str) -> CargoResult<()> {
368387
let parent_table = self.get_table_mut(table_path)?;

tests/testsuite/cargo_add/change_rename_target/out/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ version = "0.0.0"
66

77
[dependencies]
88
some-package = { package = "my-package2", version = "99999.0.0", optional = true }
9+
10+
[features]
11+
some-package = ["dep:some-package"]

tests/testsuite/cargo_add/detect_workspace_inherit_optional/out/primary/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ version = "0.0.0"
44

55
[dependencies]
66
foo = { workspace = true, optional = true }
7+
8+
[features]
9+
foo = ["dep:foo"]

tests/testsuite/cargo_add/optional/out/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ version = "0.0.0"
77
[dependencies]
88
my-package1 = { version = "99999.0.0", optional = true }
99
my-package2 = { version = "0.4.1", optional = true }
10+
11+
[features]
12+
my-package1 = ["dep:my-package1"]
13+
my-package2 = ["dep:my-package2"]

tests/testsuite/cargo_add/overwrite_git_with_path/out/primary/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ version = "0.0.0"
66

77
[dependencies]
88
cargo-list-test-fixture-dependency = { optional = true, path = "../dependency", version = "0.0.0" }
9+
10+
[features]
11+
cargo-list-test-fixture-dependency = ["dep:cargo-list-test-fixture-dependency"]

tests/testsuite/cargo_add/overwrite_inherit_optional_noop/out/primary/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ version = "0.0.0"
44

55
[dependencies]
66
foo = { workspace = true, optional = true }
7+
8+
[features]
9+
foo = ["dep:foo"]

tests/testsuite/cargo_add/overwrite_name_noop/out/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ version = "0.0.0"
77

88
[dependencies]
99
your-face = { version = "0.0.0", path = "dependency", optional = true, default-features = false, features = ["nose", "mouth"], registry = "alternative" }
10+
11+
[features]
12+
your-face = ["dep:your-face"]

tests/testsuite/cargo_add/overwrite_no_optional_with_optional/out/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ version = "0.0.0"
77
[dependencies]
88
my-package1 = { version = "99999.0.0", optional = true }
99
my-package2 = { version = "0.4.1", optional = true }
10+
11+
[features]
12+
my-package1 = ["dep:my-package1"]
13+
my-package2 = ["dep:my-package2"]

tests/testsuite/cargo_add/overwrite_optional/out/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ version = "0.0.0"
77
[dependencies]
88
my-package1 = { version = "99999.0.0", optional = true }
99
my-package2 = { version = "0.4.1", optional = true }
10+
11+
[features]
12+
my-package1 = ["dep:my-package1"]
13+
my-package2 = ["dep:my-package2"]

tests/testsuite/cargo_add/overwrite_path_noop/out/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ version = "0.0.0"
77

88
[dependencies]
99
your-face = { version = "0.0.0", path = "dependency", optional = true, default-features = false, features = ["nose", "mouth"], registry = "alternative" }
10+
11+
[features]
12+
your-face = ["dep:your-face"]

tests/testsuite/cargo_add/overwrite_path_with_version/out/primary/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ version = "0.0.0"
66

77
[dependencies]
88
cargo-list-test-fixture-dependency = { optional = true, version = "20.0" }
9+
10+
[features]
11+
cargo-list-test-fixture-dependency = ["dep:cargo-list-test-fixture-dependency"]

tests/testsuite/cargo_add/overwrite_rename_with_rename_noop/out/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ version = "0.0.0"
66

77
[dependencies]
88
a1 = { package = "versioned-package", version = "0.1.1", optional = true }
9+
10+
[features]
11+
a1 = ["dep:a1"]

tests/testsuite/cargo_add/overwrite_version_with_git/out/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ version = "0.0.0"
66

77
[dependencies]
88
versioned-package = { version = "0.3.0", optional = true, git = "[ROOTURL]/versioned-package" }
9+
10+
[features]
11+
versioned-package = ["dep:versioned-package"]

tests/testsuite/cargo_add/overwrite_version_with_path/out/primary/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ version = "0.0.0"
66

77
[dependencies]
88
cargo-list-test-fixture-dependency = { version = "0.0.0", optional = true, path = "../dependency" }
9+
10+
[features]
11+
cargo-list-test-fixture-dependency = ["dep:cargo-list-test-fixture-dependency"]

0 commit comments

Comments
 (0)