Skip to content

Commit 29b53e0

Browse files
committed
Add format option to cargo tree to print the package version constraint
- Add a new formatting option {c} to `cargo tree` that prints the resolved version constraint of packages as specified in Cargo.toml. - Add a unit test to verify that the version constraint is printed correctly. - Update the `cargo tree` command documentation with the new option.
1 parent 4406c1b commit 29b53e0

File tree

10 files changed

+161
-32
lines changed

10 files changed

+161
-32
lines changed

src/cargo/ops/tree/format/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum Chunk {
1414
Repository,
1515
Features,
1616
LibName,
17+
Constraint,
1718
}
1819

1920
pub struct Pattern(Vec<Chunk>);
@@ -30,6 +31,7 @@ impl Pattern {
3031
RawChunk::Argument("r") => Chunk::Repository,
3132
RawChunk::Argument("f") => Chunk::Features,
3233
RawChunk::Argument("lib") => Chunk::LibName,
34+
RawChunk::Argument("c") => Chunk::Constraint,
3335
RawChunk::Argument(a) => {
3436
bail!("unsupported pattern `{}`", a);
3537
}
@@ -63,6 +65,7 @@ impl<'a> fmt::Display for Display<'a> {
6365
Node::Package {
6466
package_id,
6567
features,
68+
version_req,
6669
..
6770
} => {
6871
let package = self.graph.package_for_id(*package_id);
@@ -111,6 +114,9 @@ impl<'a> fmt::Display for Display<'a> {
111114
write!(fmt, "{}", target.crate_name())?;
112115
}
113116
}
117+
Chunk::Constraint => {
118+
write!(fmt, "{}", version_req)?;
119+
}
114120
}
115121
}
116122
}

src/cargo/ops/tree/graph.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::core::dependency::DepKind;
66
use crate::core::resolver::Resolve;
77
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
88
use crate::core::{FeatureMap, FeatureValue, Package, PackageId, PackageIdSpec, Workspace};
9-
use crate::util::CargoResult;
109
use crate::util::interning::{INTERNED_DEFAULT, InternedString};
10+
use crate::util::{CargoResult, OptVersionReq};
1111
use std::collections::{HashMap, HashSet};
1212

1313
#[derive(Debug, Copy, Clone)]
@@ -56,6 +56,7 @@ pub enum Node {
5656
/// Features that are enabled on this package.
5757
features: Vec<InternedString>,
5858
kind: CompileKind,
59+
version_req: OptVersionReq,
5960
},
6061
Feature {
6162
/// Index of the package node this feature is for.
@@ -332,13 +333,15 @@ impl<'a> Graph<'a> {
332333
Node::Package {
333334
package_id,
334335
features,
336+
version_req,
335337
..
336338
} => {
337339
// Do not treat duplicates on the host or target as duplicates.
338340
Node::Package {
339341
package_id: package_id.clone(),
340342
features: features.clone(),
341343
kind: CompileKind::Host,
344+
version_req: version_req.clone(),
342345
}
343346
}
344347
_ => unreachable!(),
@@ -376,12 +379,14 @@ pub fn build<'a>(
376379
let member_id = member.package_id();
377380
let features_for = FeaturesFor::from_for_host(member.proc_macro());
378381
for kind in requested_kinds {
382+
let version_req = OptVersionReq::Any;
379383
let member_index = add_pkg(
380384
&mut graph,
381385
resolve,
382386
resolved_features,
383387
member_id,
384388
features_for,
389+
version_req,
385390
target_data,
386391
*kind,
387392
opts,
@@ -409,6 +414,7 @@ fn add_pkg(
409414
resolved_features: &ResolvedFeatures,
410415
package_id: PackageId,
411416
features_for: FeaturesFor,
417+
version_req: OptVersionReq,
412418
target_data: &RustcTargetData<'_>,
413419
requested_kind: CompileKind,
414420
opts: &TreeOptions,
@@ -423,6 +429,7 @@ fn add_pkg(
423429
package_id,
424430
features: node_features,
425431
kind: node_kind,
432+
version_req: version_req,
426433
};
427434
if let Some(idx) = graph.index.get(&node) {
428435
return *idx;
@@ -513,12 +520,14 @@ fn add_pkg(
513520
}
514521
}
515522
};
523+
let dep_version_req = dep.version_req().clone();
516524
let dep_index = add_pkg(
517525
graph,
518526
resolve,
519527
resolved_features,
520528
dep_id,
521529
dep_features_for,
530+
dep_version_req,
522531
target_data,
523532
requested_kind,
524533
opts,

src/cargo/util/semver_ext.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use super::semver_eval_ext;
22
use semver::{Comparator, Op, Version, VersionReq};
3-
use std::fmt::{self, Display};
3+
use std::{
4+
cmp::Ordering,
5+
fmt::{self, Display},
6+
};
47

58
pub trait VersionExt {
69
fn is_prerelease(&self) -> bool;
@@ -174,6 +177,19 @@ impl OptVersionReq {
174177
}
175178
}
176179

180+
impl PartialOrd for OptVersionReq {
181+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
182+
Some(self.cmp(other))
183+
}
184+
}
185+
186+
// OptVersionReq is not meaningfully orderable, so we default to considering all values equal.
187+
impl Ord for OptVersionReq {
188+
fn cmp(&self, _: &Self) -> Ordering {
189+
Ordering::Equal
190+
}
191+
}
192+
177193
impl Display for OptVersionReq {
178194
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179195
match self {

src/doc/man/cargo-tree.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ strings will be replaced with the corresponding value:
167167
- `{r}` --- The package repository URL.
168168
- `{f}` --- Comma-separated list of package features that are enabled.
169169
- `{lib}` --- The name, as used in a `use` statement, of the package's library.
170+
- `{c}` --- The version constraint resolved for the package.
170171
{{/option}}
171172

172173
{{#option "`--prefix` _prefix_" }}

src/doc/man/generated_txt/cargo-tree.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ OPTIONS
166166
o {lib} — The name, as used in a use statement, of the
167167
package’s library.
168168

169+
o {c} — The version constraint resolved for the package.
170+
169171
--prefix prefix
170172
Sets how each line is displayed. The prefix value can be one of:
171173

src/doc/src/commands/cargo-tree.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ strings will be replaced with the corresponding value:</p>
170170
<li><code>{r}</code> — The package repository URL.</li>
171171
<li><code>{f}</code> — Comma-separated list of package features that are enabled.</li>
172172
<li><code>{lib}</code> — The name, as used in a <code>use</code> statement, of the package’s library.</li>
173+
<li><code>{c}</code> — The version constraint resolved for the package.</li>
173174
</ul>
174175
</dd>
175176

src/etc/man/cargo-tree.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ strings will be replaced with the corresponding value:
208208
.RS 4
209209
\h'-04'\(bu\h'+03'\fB{lib}\fR \[em] The name, as used in a \fBuse\fR statement, of the package\[cq]s library.
210210
.RE
211+
.sp
212+
.RS 4
213+
\h'-04'\(bu\h'+03'\fB{c}\fR \[em] The version constraint resolved for the package.
214+
.RE
211215
.RE
212216
.sp
213217
\fB\-\-prefix\fR \fIprefix\fR

0 commit comments

Comments
 (0)