Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import cargo remove into cargo #11099

Merged
merged 4 commits into from
Oct 6, 2022
Merged

Conversation

cassaundra
Copy link
Contributor

@cassaundra cassaundra commented Sep 16, 2022

What does this PR try to resolve?

This PR merges cargo remove from cargo-edit into cargo.

Motivation

With #10472, cargo-add was added to cargo. As part of that discussion, it was also proposed that cargo rm (now cargo remove) eventually be added as well.

Drawbacks

  • Additional code always opens the door for more bugs and features
    • The scope of this command is fairly small though
    • Known bugs and most known features were resolved before this merge proposal

Behavior

cargo remove operates on one or more dependencies from a manifest, removing them from a specified dependencies section (using the same flags as cargo-add) and from [features] activations if the dependency is optional. Feature lists themselves are not automatically removed when made empty. Like with cargo-add, the lock file is automatically updated.

Note: like cargo add, cargo remove refers to dependency names, rather than crate names, which can be different with the presence of the name field.

Note: cargo rm has been renamed to cargo remove, based on prior art and user feedback (see discussion). Although this renaming is arguably an improvement, adding an rm alias could make the switch easier for existing users of cargo-edit (at the cost of a naming conflict which would merit insta-stabilization).

Help output

$ cargo run -- remove --help
cargo-remove
Remove dependencies from a Cargo.toml manifest file

USAGE:
    cargo remove [OPTIONS] <DEP_ID>...

ARGS:
    <DEP_ID>...    Dependencies to be removed

OPTIONS:
    -p, --package [<SPEC>...]     Package to remove from
    -v, --verbose                 Use verbose output (-vv very verbose/build.rs output)
        --manifest-path <PATH>    Path to Cargo.toml
        --offline                 Run without accessing the network
    -q, --quiet                   Do not print cargo log messages
        --dry-run                 Don't actually write the manifest
    -Z <FLAG>                     Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help                    Print help information

SECTION:
        --dev                Remove as development dependency
        --build              Remove as build dependency
        --target <TARGET>    Remove as dependency from the given target platform

Example usage

cargo remove serde
cargo remove criterion httpmock --dev
cargo remove winhttp --target x86_64-pc-windows-gnu
cargo remove --package core toml

How should we test and review this PR?

This is following the pattern from cargo-add which was implemented in three different PRs (implementation, documentation, and completions), in the interest of reducing the focusing discussions in each PR and allowing cargo-add's behavior to settle to avoid documentation churn.

  1. feat: Import cargo-add into cargo #10472
  2. Document cargo-add #10578
  3. Completion support for cargo-add #10577

The remaining changes (documentation and shell completions) will follow shortly after.

Some work has already begun on this feature in #11059.

Work on this feature was carried out on the merge-rm branch of cargo-edit with PRs reviewed by @epage. If you are interested in seeing how this feature evolved to better match cargo's internals, you might find the commit history there to be helpful. As this PR is reviewed, changes will be made both here and on that branch, with the commit history being fully maintained on the latter.

cargo remove is structured like most other subcommands:

  • src/bin/cargo/commands/remove.rs contains the cli handling and top-level execution.
  • src/cargo/ops/cargo_remove.rs contains the implementation of the feature itself.

In order to support this feature, the remove_from_table util was added to util::toml_mut::manifest::LocalManifest.

Tests are split out into a separate commit to make it easier to review the production code and tests. Tests have been implemented with snapbox, structured similarly to the tests of cargo add.

Prior art

Insta-stabilization

In the discussion of cargo add's stabilization story (Zulip stream), it was brought up that the feature might benefit from being insta-stabilized to avoid making the cargo-edit version of the binary hard to access. Since cargo rm (from cargo-edit) was renamed to cargo remove here, such a conflict no longer exists, so this is less of a concern.

Since this feature is already has a had a long run of user testing in cargo-edit and doesn't have unsettled UI questions like cargo-add did, it might still be a candidate for insta-stabilization.

Deferred work

Necessary future work:

It was found in the review of cargo add that it was best to defer these first two items to focus the discussion and as there was still behavior churn during the review of cargo-add.

Future Possibilities

The following are features which we might want to add to cargo remove in the future:

Additional information

Fixes #10520.

@rust-highfive
Copy link

r? @weihanglo

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 16, 2022
@cassaundra cassaundra force-pushed the cargo-remove branch 2 times, most recently from b797e37 to 471f8d9 Compare September 16, 2022 22:51
@cassaundra
Copy link
Contributor Author

Fixed tests on Windows.

Copy link
Contributor

@epage epage left a comment

Choose a reason for hiding this comment

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

I've not dug through all of the tests but overall, this looks good. None of my comments are blocking.

EDIT: With all of the prep work done, its crazy to see how little code this is

src/bin/cargo/commands/remove.rs Show resolved Hide resolved
src/bin/cargo/commands/remove.rs Show resolved Hide resolved
src/bin/cargo/commands/remove.rs Outdated Show resolved Hide resolved
@cassaundra
Copy link
Contributor Author

cassaundra commented Sep 20, 2022

Something else that might be useful as future work: if the user tries to remove a package that doesn't exist in the [dependencies] table, but does exist in another (e.g. [dev-dependencies]), display an informative hint to the user. We could also automatically remove the dependency for them, but I think this might be too aggressive.

Copy link
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.

With all of the prep work done, its crazy to see how little code this is

Totally agree! Thanks epage so much for previous efforts and cassaundra for posting this PR as well.

src/cargo/util/toml_mut/manifest.rs Outdated Show resolved Hide resolved
src/cargo/util/toml_mut/manifest.rs Outdated Show resolved Hide resolved
src/bin/cargo/commands/remove.rs Show resolved Hide resolved
src/cargo/util/toml_mut/manifest.rs Outdated Show resolved Hide resolved
@cassaundra cassaundra force-pushed the cargo-remove branch 2 times, most recently from f155447 to 13a2f06 Compare September 20, 2022 20:12
@cassaundra
Copy link
Contributor Author

cassaundra commented Sep 20, 2022

Fixed all nits that were mentioned, and fixed the lock file test. Tests are failing due to some network error it looks like (edit: working now 🤷‍♀️).

Copy link
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.

Looks great! No outstanding issue from me. Thank you.

@epage you can r+ it whenever you feel it's about time.

Edited: Oh. I feel like we still need an FCP, no?

@cassaundra
Copy link
Contributor Author

cassaundra commented Sep 20, 2022

Hmm, I thought I fixed it, but the extra space in [ "semver/std"] issue came back again. This might be an issue with toml_edit. Will fix in a moment.

@cassaundra
Copy link
Contributor Author

Hmm, I thought I fixed it, but the extra space in [ "semver/std"] issue came back again.

Newly added 56918df calls fmt on the feature values array after editing it.

@epage
Copy link
Contributor

epage commented Sep 20, 2022

@rfcbot fcp merge

While this doesn't require insta-stablization like cargo-add did because of the rename from cargo-rm to cargo-remove, this is also a lot less risky than cargo-add, having a much narrower scope and getting more runtime on the design being proposed for merge. I feel like there is little for us to learn through a separate stablization for this.

Like with cargo-add, there is work deferred out of this PR

Work has already started on these. So long as merging this PR doesn't happen near the cut to beta, we should be safe deferring these out of this PR (again, like with cargo-add).

@rfcbot
Copy link
Collaborator

rfcbot commented Sep 20, 2022

Team member @epage has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Sep 20, 2022
@joshtriplett
Copy link
Member

@rfcbot reviewed

@rfcbot concern cargo-rm alias

I do think we should have a cargo-rm alias, to help with transition and keep people's finger-memory working. I think that's worth the additional transition effort.

@ehuss
Copy link
Contributor

ehuss commented Sep 21, 2022

A few questions:

  • Does this rebuild Cargo.lock like cargo add does now?
  • Does this remove all references to a dependency? That is, things like profiles, features, patch, replace, etc.
  • The description mentions workspace inheritance, but also says it was deferred since it is unstable. I believe it has been stable for some time, so I'm not clear on what that means.

@epage
Copy link
Contributor

epage commented Sep 21, 2022

Does this rebuild Cargo.lock like cargo add does now?

Yes

Does this remove all references to a dependency? That is, things like profiles, features, patch, replace, etc.

It removes it from features.

I hadn't though of cleaning up profiles, patch, and release. Would you consider that a blocker for merge, a blocker for going to beta, or important but non-blocker that can be put in an issue?

The description mentions workspace inheritance, but also says it was deferred since it is unstable. I believe it has been stable for some time, so I'm not clear on what that means.

The development happened in cargo-edit testing with released cargo. workspace inheritance will hit stable in 1.64.0 (tomorrow).

I figured that rather than blocking the last work and merge on 1.64.0, we could move forward as-is and then implement the workspace inheritance part in cargo master where we don't have to worry about what is in stable or not.

@epage
Copy link
Contributor

epage commented Sep 21, 2022

@joshtriplett

I do think we should have a cargo-rm alias, to help with transition and keep people's finger-memory working. I think that's worth the additional transition effort.

I'm fine with that and fine with that forcing insta-stablization (since I was proposing that anyways).

Would you consider this a blocker for merge or ok if we just move it to the Deferred list. I had already been wondering if it should be in the Deferred list but decided to wait to see what interest there was.

@cassaundra
Copy link
Contributor Author

cassaundra commented Sep 21, 2022

Extending the GC operation to cover profile, patch, and replace makes sense to me. As far as the rm alias, should that be done in this PR? It's a very small change that I can just go ahead and add (edit: now realizing that's what the above comment was already asking).

@bors
Copy link
Contributor

bors commented Oct 6, 2022

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

@bors bors merged commit c71f344 into rust-lang:master Oct 6, 2022
weihanglo added a commit to weihanglo/rust that referenced this pull request Oct 7, 2022
4 commits in 0b84a35c2c7d70df4875a03eb19084b0e7a543ef..3cdf1ab25dc4fe56f890e8c7330d53a23ad905d3

2022-10-03 19:13:21 +0000 to 2022-10-07 17:34:03 +0000
- fix(test): Distinguish 'testname' from escaped arguments (rust-lang/cargo#11190)
- Fix sparse registry lockfile urls containing 'registry+sparse+' (rust-lang/cargo#11177)
- doc(features2): polish docs a bit (rust-lang/cargo#11185)
- Import `cargo remove` into cargo (rust-lang/cargo#11099)
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 7, 2022
Update cargo

4 commits in 0b84a35c2c7d70df4875a03eb19084b0e7a543ef..3cdf1ab25dc4fe56f890e8c7330d53a23ad905d3

2022-10-03 19:13:21 +0000 to 2022-10-07 17:34:03 +0000
- fix(test): Distinguish 'testname' from escaped arguments (rust-lang/cargo#11190)
- Fix sparse registry lockfile urls containing 'registry+sparse+' (rust-lang/cargo#11177)
- doc(features2): polish docs a bit (rust-lang/cargo#11185)
- Import `cargo remove` into cargo (rust-lang/cargo#11099)
bors added a commit that referenced this pull request Oct 8, 2022
Use correct version of cargo in test

Fix `cargo_remove::offline` test using wrong version of cargo in test, the local version and calling instance of cargo instead of the one being tested.

Issue discovered in #10907 after merge of  #11099.
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Oct 9, 2022
Update cargo

4 commits in 0b84a35c2c7d70df4875a03eb19084b0e7a543ef..3cdf1ab25dc4fe56f890e8c7330d53a23ad905d3

2022-10-03 19:13:21 +0000 to 2022-10-07 17:34:03 +0000
- fix(test): Distinguish 'testname' from escaped arguments (rust-lang/cargo#11190)
- Fix sparse registry lockfile urls containing 'registry+sparse+' (rust-lang/cargo#11177)
- doc(features2): polish docs a bit (rust-lang/cargo#11185)
- Import `cargo remove` into cargo (rust-lang/cargo#11099)
bors added a commit that referenced this pull request Oct 10, 2022
Add completions for `cargo remove`

### What does this PR try to resolve?

This PR continues the deferred work of #11099 by adding bash and zsh completions for the cargo remove subcommand.

### How should we test and review this PR?

There doesn't seem to be much in the way of testing for these completions automatically, so I would suggest verifying that they work in practice and sufficiently cover the subcommand's surface area.

### Additional Information

I will also soon post a PR for cargo remove's documentation.
weihanglo pushed a commit to weihanglo/cargo that referenced this pull request Oct 10, 2022
…anglo

Add completions for `cargo remove`

### What does this PR try to resolve?

This PR continues the deferred work of rust-lang#11099 by adding bash and zsh completions for the cargo remove subcommand.

### How should we test and review this PR?

There doesn't seem to be much in the way of testing for these completions automatically, so I would suggest verifying that they work in practice and sufficiently cover the subcommand's surface area.

### Additional Information

I will also soon post a PR for cargo remove's documentation.
@ehuss ehuss added this to the 1.66.0 milestone Oct 10, 2022
bors added a commit that referenced this pull request Oct 13, 2022
Document `cargo remove`

### What does this PR try to resolve?

This PR continues the deferred work of #11099 by adding documentation for the cargo remove subcommand.

### How should we test and review this PR?

Ensure that the documentation renders correctly and appropriately covers the feature.
bors added a commit that referenced this pull request Oct 13, 2022
Document `cargo remove`

### What does this PR try to resolve?

This PR continues the deferred work of #11099 by adding documentation for the cargo remove subcommand.

### How should we test and review this PR?

Ensure that the documentation renders correctly and appropriately covers the feature.
bors added a commit that referenced this pull request Oct 14, 2022
Document `cargo remove`

### What does this PR try to resolve?

This PR continues the deferred work of #11099 by adding documentation for the cargo remove subcommand.

### How should we test and review this PR?

Ensure that the documentation renders correctly and appropriately covers the feature.
bors added a commit that referenced this pull request Nov 1, 2022
Clean up workspace dependencies after cargo remove

### What does this PR try to resolve?

After successful removal of an inherited dependency from a workspace member, clean up the root workspace manifest.

This PR is part of the continued working on cargo remove (#11099, see deferred work).

### How should we test and review this PR?

Make sure the tests cover all possible use cases. After posting this PR, I will post a short self-review regarding some design concerns.

### Additional information

#11194 is currently blocked on this feature.
@jujpenabe
Copy link

I just want to point out that a dependency can now be removed with the rm alias. But I don't see the suggestion in cargo remove --help or cargo help though, so maybe this should be added.

@weihanglo
Copy link
Member

@jujpenabe. Nice catch. I just created an issue #11348 for it.

bors added a commit that referenced this pull request Nov 19, 2022
Clean profile, patch, and replace in cargo remove

### What does this PR try to resolve?

This PR is part of the continued work on cargo remove (#11099, see deferred work).

After a successful removal of a dependency, clean up the profile, patch, and replace sections to remove all references to it.

**Note** the GC process was expanded to clean up not just references to the dependencies just removed, but also references of all dependencies. This was because there's not an easy way to determine which dependencies correspond to the given TOML keys, without either 1) figuring that out before the removal (therefore having to predict the behavior), or 2) returning that information from the remove function (somewhat unorthodox for an op).

### How should we review and test this PR?

Verify that the implementation makes sense and that the tests are sufficient.
bors added a commit that referenced this pull request May 27, 2023
fix(add): Reduce the chance we re-format the user's `[features]` table

### What does this PR try to resolve?

#11743 pointed out that we re-format the users `[features]` table when running `cargo add`  which was a bug introduced in #11099.

This reduces the chance people will run into this problem
- Reducing the scope of the `fmt` call
- Preserving formatting in a simple case

Actually removing the `fmt` case can make some common formatting cases more complex to do "right", so I'm punting on that for now.

### How should we test and review this PR?

Look at the individual commits as I show how each change improves the behavior of `cargo add`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge FCP with intent to merge finished-final-comment-period FCP complete S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-cargo Team: Cargo to-announce
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add cargo-rm (from cargo-edit) to cargo proper
9 participants