Skip to content

Commit 7ce2b7d

Browse files
committed
fix(add): Limit 'Features as of vX.Y.Z' to when relevant
This will only show the messaeg if we didn't already show a version req with full precision specified ... mostly. We'll also skip it if its a local or git dependency but we never show the version in those cases because it doesn't matter. The `precise_version` logic came from cargo-upgrade.
1 parent 73906ae commit 7ce2b7d

File tree

9 files changed

+51
-10
lines changed

9 files changed

+51
-10
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,16 @@ fn print_msg(shell: &mut Shell, dep: &DependencyEx, section: &[String]) -> Cargo
769769
if !activated.is_empty() || !deactivated.is_empty() {
770770
let prefix = format!("{:>13}", " ");
771771
let suffix = if let Some(version) = &dep.available_version {
772-
format!(" as of v{}", version)
772+
let version = version.to_string();
773+
let version_req = dep
774+
.version()
775+
.and_then(|v| semver::VersionReq::parse(v).ok())
776+
.and_then(|v| precise_version(&v));
777+
if version_req.as_deref() != Some(version.as_str()) {
778+
format!(" as of v{}", version)
779+
} else {
780+
"".to_owned()
781+
}
773782
} else {
774783
"".to_owned()
775784
};
@@ -832,3 +841,35 @@ fn find_workspace_dep(toml_key: &str, root_manifest: &Path) -> CargoResult<Depen
832841
))?;
833842
Dependency::from_toml(root_manifest.parent().unwrap(), toml_key, dep_item)
834843
}
844+
845+
fn precise_version(version_req: &semver::VersionReq) -> Option<String> {
846+
version_req
847+
.comparators
848+
.iter()
849+
.filter(|c| {
850+
matches!(
851+
c.op,
852+
// Only ops we can determine a precise version from
853+
semver::Op::Exact
854+
| semver::Op::GreaterEq
855+
| semver::Op::LessEq
856+
| semver::Op::Tilde
857+
| semver::Op::Caret
858+
| semver::Op::Wildcard
859+
)
860+
})
861+
.filter_map(|c| {
862+
// Only do it when full precision is specified
863+
c.minor.and_then(|minor| {
864+
c.patch.map(|patch| semver::Version {
865+
major: c.major,
866+
minor,
867+
patch,
868+
pre: c.pre.clone(),
869+
build: Default::default(),
870+
})
871+
})
872+
})
873+
.max()
874+
.map(|v| v.to_string())
875+
}

tests/testsuite/cargo_add/add_normalized_name_external/stderr.log

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
warning: translating `linked_hash_map` to `linked-hash-map`
33
warning: translating `Inflector` to `inflector`
44
Adding linked-hash-map v0.5.4 to dependencies.
5-
Features as of v0.5.4:
5+
Features:
66
- clippy
77
- heapsize
88
- heapsize_impl
@@ -11,7 +11,7 @@ warning: translating `Inflector` to `inflector`
1111
- serde_impl
1212
- serde_test
1313
Adding inflector v0.11.4 to dependencies.
14-
Features as of v0.11.4:
14+
Features:
1515
+ heavyweight
1616
+ lazy_static
1717
+ regex
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Adding cargo-list-test-fixture-dependency (local) to build-dependencies.
2-
Features as of v0.0.0:
2+
Features:
33
- one
44
- two

tests/testsuite/cargo_add/list_features_path/stderr.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Adding your-face (local) to dependencies.
2-
Features as of v0.1.3:
2+
Features:
33
+ mouth
44
+ nose
55
- eyes

tests/testsuite/cargo_add/list_features_path_no_default/stderr.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Adding your-face (local) to dependencies.
2-
Features as of v0.1.3:
2+
Features:
33
- eyes
44
- mouth
55
- nose
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Adding your-face (local) to dev-dependencies.
2-
Features as of v0.0.0:
2+
Features:
33
+ mouth
44
+ nose
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Adding your-face (local) to optional dependencies.
2-
Features as of v0.0.0:
2+
Features:
33
+ mouth
44
+ nose
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Adding your-face (local) to optional dependencies.
2-
Features as of v0.0.0:
2+
Features:
33
+ mouth
44
+ nose

tests/testsuite/cargo_add/overwrite_workspace_dep_features/stderr.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Adding foo (local) to dependencies.
2-
Features as of v0.0.0:
2+
Features:
33
+ default-base
44
+ default-merge-base
55
+ default-test-base

0 commit comments

Comments
 (0)