cargo add <package>

Created on 20 Mar 2014  Â·  25Comments  Â·  Source: rust-lang/cargo

This is probably a no brainer, but I thought I'd drop it in here as a yes-please feature request.

I just discovered that with bower and npm if you install a package into a local directory scope (eg. bower install moment) with a particular flag (in this case bower install moment -S) it not only installs the moment recent version of the package into the local folder; it updates the manifest file automatically:

{
  "name": "configtool",
  "version": "0.1.0",
  "dependencies": {
    "underscore": "1.5.2",
    "angular": "1.2.3",
    "angular-resource": "1.2.3",
    "angular-sanitize": "~1.2.3",
    "momentjs": "~2.5.1" // <---- This gets automatically added 
  }
}

This is a really lovely workflow to use, I thought I'd drop it in here as a feature request.

A-new-subcommand

Most helpful comment

Yeah, I feel like this is a missing feature, too.

All 25 comments

I definitely want cargo add <package-name> [FLAGS], which would add the package to the manifest, with optional flags (such as a Github URL if not using the central repo).

Does that sound like what you want?

yep, exactly.

There has been some work on this here and here.

@killercup any reason for making this a plugin instead of implementing it right into cargo itself?

@Turbo87 It's nice to test implementations and CLI interfaces in external projects. (Plugin → Built-in is way more easy than Built-in → Plugin.)

I see, thanks

I've written a help text for the potential new add command. Any feedback would be very welcome.

Add dependencies to the Cargo.toml file

Usage:
    cargo add [options] <crate>...

Options:
    -h, --help               Print this message
    --build                  Add to the build-dependencies section
    --dev                    Add to the dev-dependencies section
    --optional               Add as an optional dependency
    --features FEATURES      Space-separated list of features to also use
    --no-default-features    Do not use the `default` feature
    --target TRIPLE          Add dependency only for the target triple

This command will add the specified <crate> to your Cargo.toml file. A <crate>
can be anything of the following:

    1) crate name (e.g. `libc`, see https://crates.io/)
    2) crate name with a version (e.g. `[email protected]`)
    3) local path to another crate (e.g. `../libc`)
    4) git url (e.g. `https://github.com/rust-lang/libc`)
    5) git url with revision (e.g. `https://github.com/rust-lang/libc#473c10c`)

This command can be used to add multiple crates at once, but any specified
options will apply to all of them.

@Turbo87 nice! That's basically a complete docopt definition :)

The equivalent options should work for cargo rm. (Also, you might want to add --build for build-dependencies.)

@killercup Thanks for reminding me of the build-dependencies section. I thought I read about it somewhere but then later I couldn't find anything about it on http://doc.crates.io/manifest.html so I skipped it. I've updated the help text above.

I haven't thought about cargo rm yet, but it might be something for the future once git add is implemented.

@Turbo87 I've spend some time this weekend refactoring my cargo-edit project. It now has a cargo add binary that looks quite like what you suggested above. I haven't done any extensive testing, but the simple things should work: https://github.com/killercup/cargo-edit

cool, thanks for the feedback. I'll have a look at it once I have a bit of spare time.

With cargo-install plus cargo-edit, I'm gonna close this in favor of that repo, thanks @killercup!

Not sure this should be closed hey, all of npm's users (which is a pretty large number at this point) are used to passing a --save flag on install. If cargo doesn't support pinning versions out of the box, the flow will feel awkward to users coming from npm.

I reckon it'd be great for users if cargo supported add or install --save out of the box. I don't think it's reasonable for everyone new to Rust to find out about the cargo-edit through this issue when they get started.

We will probably pull cargo-edit into cargo itself, but there are a few issues that need fixed before that's considered. It has basically universal support though.

On Feb 22, 2016, 09:00 -0500, Yoshua [email protected], wrote:

Not sure this should be closed hey, all ofnpm's users (which is a pretty large number at this point) are used to passing a--saveflag on install. If cargo doesn't support pinning versions out of the box, the flow will feel awkward to users coming from npm.

I reckon it'd be great for users if cargo supportedaddorinstall --saveout of the box. I don't think it's reasonable for everyone new to Rust to find out about thecargo-editthrough this issue when they get started.

—
Reply to this email directly orview it on GitHub(https://github.com/rust-lang/cargo/issues/4#issuecomment-187186630).

Indeed, I'd love to move these in tree! The major blocker is having better support for modifying TOML because today artifacts such as formatting, comments, etc are lost when using cargo-edit.

What is the state of this? Will cargo add be a part of Cargo itself in the future? Or should it stay separated in cargo-edit?

The problems @alexcrichton mentioned in February are still present. There are a few crates that aim to make TOML editing less destructive, but I haven't seen one that is feature-complete enough to be used in cargo-edit. (If that changed since I last looked at it—I would love to see a PR for this! 😄)

Thanks.

Is there a tracking issue for the addition of this feature into Cargo itself (dependent on the destructive-editing issues being resolved)?

Even if it's not immediately implementable, keeping the issue closed seems like the wrong state for an unresolved issue, but perhaps there's another issue thread I've overlooked.

Yeah, I feel like this is a missing feature, too.

This should definitely be a native feature of cargo as a package manager. The fact that a third party plugin exists to do this does not mean any less so that this feature should come right out of the box. This issue should still be open.

Once @killercup releases the next version of cargo-edit, this should be possible to upstream. The non-destructive version is still in beta, though.

Last comment on 19 Feb 2018, huh? Now, what's the status of this?

Is it now viable to be merged into Cargo? @steveklabnik

https://github.com/rust-lang/cargo/issues/5586 is tracking this nowadays. last comment is from march. still desired, iirc there's still some issues.

This should be closed then in favor of #5586.

EDIT: Nvm, I just saw it is closed lmao.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SimonSapin picture SimonSapin  Â·  3Comments

JustAPerson picture JustAPerson  Â·  3Comments

alilleybrinker picture alilleybrinker  Â·  3Comments

tensorchen picture tensorchen  Â·  3Comments

briansmith picture briansmith  Â·  3Comments