Skip to content

feat: Add info cargo subcommand#14141

Merged
bors merged 4 commits intorust-lang:masterfrom
0xPoe:rustin-patch-info
Aug 12, 2024
Merged

feat: Add info cargo subcommand#14141
bors merged 4 commits intorust-lang:masterfrom
0xPoe:rustin-patch-info

Conversation

@0xPoe
Copy link
Copy Markdown
Member

@0xPoe 0xPoe commented Jun 25, 2024

close #14081
close #948

fcp #14141 (comment)

This PR added a new info cargo subcommand.

Background

This adds a new subcommand to Cargo, cargo info. This subcommand would allow users to get information about a crate from the command line, without having to go to the web.

The main motivation for this is to make it easier to get information about a crate from the command line. Currently, the way to get information about a crate is to go to the web and look it up on crates.io or find the crate's source code and look at the Cargo.toml file. This is not very convenient, especially not all information is displayed on the crates.io page.
This command also has been requested by the community for a long time. You can find more discussion about this in cargo#948.

Another motivation is to make the workflow of finding and evaluating crates more efficient. In the current workflow, users can search for crates using cargo search, but then they have to go to the web to get more information about the crate. This is not very efficient, especially if the user is just trying to get a quick overview of the crate. This would allow users to quickly get information about a crate without having to leave the terminal.

Example usage:

./target/debug/cargo info clap --verbose
  Credential cargo:token get crates-io
clap #argument #cli #arg #parser #parse
A simple to use, efficient, and full-featured Command Line Argument Parser
version: 4.5.8 (latest 4.5.9)
license: MIT OR Apache-2.0
rust-version: 1.74
documentation: https://docs.rs/clap/4.5.8
repository: https://github.com/clap-rs/clap
crates.io: https://crates.io/crates/clap/4.5.8
features:
 +default         = [std, color, help, usage, error-context, suggestions]
  color           = [clap_builder/color]
  error-context   = [clap_builder/error-context]
  help            = [clap_builder/help]
  std             = [clap_builder/std]
  suggestions     = [clap_builder/suggestions]
  usage           = [clap_builder/usage]
  cargo           = [clap_builder/cargo]
  debug           = [clap_builder/debug, clap_derive?/debug]
  deprecated      = [clap_builder/deprecated, clap_derive?/deprecated]
  derive          = [dep:clap_derive]
  env             = [clap_builder/env]
  string          = [clap_builder/string]
  unicode         = [clap_builder/unicode]
  unstable-doc    = [clap_builder/unstable-doc, derive]
  unstable-styles = [clap_builder/unstable-styles]
  unstable-v5     = [clap_builder/unstable-v5, clap_derive?/unstable-v5, deprecated]
  wrap_help       = [clap_builder/wrap_help]
dependencies:
 +clap_builder@=4.5.8
  clap_derive@=4.5.8
owners:
  kbknapp (Kevin K.)
  github:rust-cli:maintainers (Maintainers)
  github:clap-rs:admins (Admins)
note: to see how you depend on clap, run `cargo tree --invert --package clap@4.5.8`
image

note: this is showing the --verbose output to show every thing the user can possibly see. Normal operation does not include

  • dependencies

Detailed design

Content Explanation Why
clap Name The basic information.
#argument #cli #arg #parser #parse Keywords (clickable) It's more like a category, which you can use to search for relevant alternatives.
A simple to use, efficient, and full-featured Command Line Argument Parser Description The basic information.
version: 4.5.8 (latest 4.5.9) Version The basic information.
license: MIT OR Apache-2.0 License When choosing a crate, it is crucial to consider the license.
rust-version: 1.74 MSRV When choosing a crate, it is crucial to make sure it can work with your MSRV.
documentation: https://docs.rs/clap/4.5.8 Documentation Link Use these links can find more docs and information.
repository: https://github.com/clap-rs/clap Repo Link Use these links can find more docs and information.
crates.io: https://crates.io/crates/clap/4.5.8 crates.io Link Use these links can find more docs and information.
features: Default Features And Other Features It helps for enabling features.
dependencies: All dependencies It indicates what it depends on.
owners: Owners It indicates who maintains the crate.
note: to see how you depend on clap, run cargo tree --invert --package clap@4.5.8 A note for cargo tree command It will prompt the user that the package is depended on under the workspace, and the dependencies can be viewed using the cargo tree command.

Rendering features

  • For features enabled by users, a + prefix and colored output are now used for better visibility.
  • For features enabled automatically, colored output is used to distinguish them.
  • For disabled features, non-colored output is used to clearly indicate their status.

Rendering deps

Only show dependencies in verbose mode.

  • For dependencies required by the package, a + prefix and colored output are now used for better visibility.
  • For dependencies that are optional and activated, colored output is used to distinguish them.
  • For dependencies that are optional and not activated, non-colored output is used to clearly indicate their status.

Some important notes

Downloading the crate from any Cargo compatible registry

The cargo info command will download the crate from any Cargo compatible registry. It will then extract the information from the Cargo.toml file and display it in the terminal.

If the crate is already in the local cache, it will not download the crate again. It will get the information from the local cache.

Pick the correct version from the workspace

When executed in a workspace directory, the cargo info command chooses the version that the workspace is currently using.

If there's a lock file available, the version from this file will be used. In the absence of a lock file, the command attempts to select a version that is compatible with the Minimum Supported Rust Version (MSRV). And the lock file will be generated automatically.

The following hierarchy is used to determine the MSRV:

  • First, the MSRV of the parent directory package is checked, if it exists.
  • If the parent directory package does not specify an MSRV, the minimal MSRV of the workspace is checked.
  • If neither the workspace nor the parent directory package specify an MSRV, the version of the current Rust compiler (rustc --version) is used.

Prior art

NPM

npm has a similar command called npm info.
For example:

$ npm info lodash

lodash@4.17.21 | MIT | deps: none | versions: 114
Lodash modular utilities.
https://lodash.com/

keywords: modules, stdlib, util

dist
.tarball: https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz
.shasum: 679591c564c3bffaae8454cf0b3df370c3d6911c
.integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
.unpackedSize: 1.4 MB

maintainers:
- mathias <mathias@qiwi.be>
- jdalton <john.david.dalton@gmail.com>
- bnjmnt4n <benjamin@dev.ofcr.se>

dist-tags:
latest: 4.17.21

published over a year ago by bnjmnt4n <benjamin@dev.ofcr.se>

Poetry

Poetry has a similar command called poetry show.

For example:

$ poetry show pendulum

name        : pendulum
version     : 1.4.2
description : Python datetimes made easy

dependencies
 - python-dateutil >=2.6.1
 - tzlocal >=1.4
 - pytzdata >=2017.2.2

required by
 - calendar >=1.4.0

insta-stable

As @weihanglo mentioned in #14141 (comment), commands that shadow third-party commands tend to be insta-stabilized to avoid an intermediate period where users can't access the third-party command (built-ins get priority) nor the built-in command (requires nightly)

For the cargo-info command, there are two commands that this would shadow

We might be able to get away with having this unstable but starting from the assumption of insta-stabilization.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 25, 2024

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-cli Area: Command-line interface, option parsing, etc. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 25, 2024
@0xPoe 0xPoe force-pushed the rustin-patch-info branch from e5feb74 to b9965c6 Compare June 25, 2024 12:50
@0xPoe 0xPoe marked this pull request as draft June 25, 2024 12:51
@weihanglo
Copy link
Copy Markdown
Member

I remembered last time when we imported new commands we insta-stablized them (cargo-add and cargo-remove) because built-in commands shadow external ones, making them unusable.

If the Cargo team are happy with making this a built-in command, I suppose we could do the same thing.

@0xPoe 0xPoe force-pushed the rustin-patch-info branch 4 times, most recently from f582df0 to 476969f Compare June 26, 2024 12:03
Comment thread src/bin/cargo/commands/info.rs
Comment thread src/cargo/ops/cargo_info/mod.rs Outdated
Comment thread src/cargo/ops/registry/info/view.rs Outdated
Comment thread src/cargo/ops/registry/mod.rs Outdated
Comment thread src/cargo/ops/registry/mod.rs
@0xPoe 0xPoe force-pushed the rustin-patch-info branch 5 times, most recently from 791dad8 to 242919d Compare July 7, 2024 14:38
@0xPoe
Copy link
Copy Markdown
Member Author

0xPoe commented Aug 2, 2024

You might also want to add cargo-info to src/doc/man/cargo.md. See #14339.

Added. Thank you!

@rfcbot rfcbot added finished-final-comment-period FCP complete to-announce and removed final-comment-period FCP — a period for last comments before action is taken labels Aug 3, 2024
@rfcbot
Copy link
Copy Markdown

rfcbot commented Aug 3, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@0xPoe
Copy link
Copy Markdown
Member Author

0xPoe commented Aug 4, 2024

@epage @weihanglo I guess this PR is ready to merge. Could you please help merge it when you have time? If there is anything I need to update, please let me know.
Thanks for your review! 💚 💙 💜 💛 ❤️

@epage Thank you for guiding me in this work, I really appreciate it. Thank you for all your time.

Copy link
Copy Markdown
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't look too closer to the implementation. That seems pretty solid. Thank you hi-rustin!

Comment thread src/cargo/ops/registry/mod.rs Outdated
Comment thread src/doc/man/cargo-info.md Outdated
Comment thread src/bin/cargo/commands/info.rs Outdated
Comment thread src/doc/man/cargo-info.md Outdated
Comment thread src/doc/man/cargo-info.md Outdated

_crate_`@`_version_: Fetch from a registry with a version constraint of "_version_"

If the specified package is part of the current workspace, the information from the local Cargo.toml file will be
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we override the local query with registry query? This guide doesn't talk about that. Should we mention it or put an example for this use case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can specify a specific version(a different version) or you need to check it out of the workspace. I guess it's okay to not mention it. I believe checking it out of the workspace is quite straightforward.

return (Some(package_id), false);
}

if let Some(package_id) = ws
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that we handle duplicate package by max_by regardless MSRV of globally maximum. Should we have something reminding user when a query is ambiguous?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should mention this detail here. From the start, I provided all the details about how we select a package, but Ed thought it was too detailed. He believes it might create more work if we want to change these implementation details.

Comment thread src/cargo/ops/registry/info/mod.rs
@ehuss
Copy link
Copy Markdown
Contributor

ehuss commented Aug 10, 2024

I tried giving this a spin, and was very surprised to find that it was requesting my token for crates.io. Is there a reason it must do that? That seems like it will be a bad user experience if someone needs to enter a password or use a biometric or hardware key in order to use this command.

@0xPoe
Copy link
Copy Markdown
Member Author

0xPoe commented Aug 10, 2024

I tried giving this a spin, and was very surprised to find that it was requesting my token for crates.io. Is there a reason it must do that? That seems like it will be a bad user experience if someone needs to enter a password or use a biometric or hardware key in order to use this command.

I will take a look. It shouldn't require it. I remember I fixed it in my previous local test.

@0xPoe
Copy link
Copy Markdown
Member Author

0xPoe commented Aug 10, 2024

I tested it locally:

./target/debug/cargo logout
      Logout token for `crates-io` has been removed from local storage
note: This does not revoke the token on the registry server.
    If you need to revoke the token, visit <https://crates.io/me> and follow the instructions there.

cargo on  rustin-patch-info is 📦 v0.83.0 via 🐍 v3.12.4 via 🦀 v1.82.0-nightly ./target/debug/cargo info serde  
serde #serde #serialization #no_std
A generic serialization/deserialization framework
version: 1.0.204
license: MIT OR Apache-2.0
rust-version: 1.31
documentation: https://docs.rs/serde
homepage: https://serde.rs
repository: https://github.com/serde-rs/serde
crates.io: https://crates.io/crates/serde/1.0.204
features:
 +default      = [std]
  std          = []
  alloc        = []
  derive       = [serde_derive]
  rc           = []
  serde_derive = [dep:serde_derive]
  unstable     = []
note: to see how you depend on serde, run `cargo tree --invert --package serde@1.0.204`

It seemed it doesn't require it.

@0xPoe 0xPoe force-pushed the rustin-patch-info branch 2 times, most recently from 156c865 to a4c7dfc Compare August 10, 2024 06:53
Copy link
Copy Markdown
Member Author

@0xPoe 0xPoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔢 Self-check (PR reviewed by myself and ready for feedback.)

@epage
Copy link
Copy Markdown
Contributor

epage commented Aug 12, 2024

I also logged out and couldn't reproduce.

As we develop reproduction steps, we can iterate from there.

@bors r+

@bors
Copy link
Copy Markdown
Contributor

bors commented Aug 12, 2024

📌 Commit ba07215 has been approved by epage

It is now in the queue for this repository.

@bors
Copy link
Copy Markdown
Contributor

bors commented Aug 12, 2024

⌛ Testing commit ba07215 with merge f3fee6d...

@bors
Copy link
Copy Markdown
Contributor

bors commented Aug 12, 2024

☀️ Test successful - checks-actions
Approved by: epage
Pushing f3fee6d to master...

1 similar comment
@bors
Copy link
Copy Markdown
Contributor

bors commented Aug 12, 2024

☀️ Test successful - checks-actions
Approved by: epage
Pushing f3fee6d to master...

@bors
Copy link
Copy Markdown
Contributor

bors commented Aug 12, 2024

👀 Test was successful, but fast-forwarding failed: 422 Changes must be made through a pull request.

@ehuss
Copy link
Copy Markdown
Contributor

ehuss commented Aug 15, 2024

Opened #14409 regarding the authentication problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area: Command-line interface, option parsing, etc. A-cli-help Area: built-in command-line help A-completions Area: shell completions A-documenting-cargo-itself Area: Cargo's documentation A-interacts-with-crates.io Area: interaction with registries Command-login Command-logout Command-owner Command-publish Command-search Command-yank disposition-merge FCP with intent to merge finished-final-comment-period FCP complete relnotes Release-note worthy S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-cargo Team: Cargo

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Tracking Issue for merging cargo-information/cargo info to Cargo Something like cargo query or cargo info for information about a registry package

7 participants