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

Subcommand to add a new dependency to Cargo.toml #2179

Closed
kornelski opened this issue Nov 29, 2015 · 27 comments
Closed

Subcommand to add a new dependency to Cargo.toml #2179

kornelski opened this issue Nov 29, 2015 · 27 comments

Comments

@kornelski
Copy link
Contributor

kornelski commented Nov 29, 2015

I've grown to appreciate

npm install --save packagename

which automatically finds the package and adds it to package.json. composer require works in a similar way.

Cargo isn't as convenient for adding new dependencies to a project. I need to:

  1. Visit the crates.io website find the package I want
  2. Copy its latest version
  3. Paste it to Cargo.toml myself

That's a bit tedious, especially that I have to switch from terminal to a web browser.

I'd be much nicer if Cargo had a command for adding dependencies, e.g.

cargo add packagename

that would find the package on crates.io and add it with an appropriate version to [dependencies] in Cargo.toml.


edit: for now, the fix is to run cargo install cargo-edit that adds cargo add command

@steveklabnik
Copy link
Member

https://github.com/killercup/cargo-edit provides this

@kornelski
Copy link
Contributor Author

Good to see others think it's useful to have too. Judging by the long list of dependencies of cargo-edit it looks like it reimplements cargo's code for communicating with the registry, so the implementation would be much smaller if it were built into cargo.

@huonw
Copy link
Member

huonw commented Nov 29, 2015

This is a dupe of the #4, which was in fact closed pointing to cargo edit, since cargo install now makes it super easy to install it.

@kornelski
Copy link
Contributor Author

Knowing about existence of cargo-edit is a barrier.

For me it went like this:

$ cargo install package
…
specified package has no binaries

…grumble, grumble, advised to use cargo-edit…

$ cargo install cargo-edit
…
# long list of compiled deps here
…
$ cargo add package
No such subcommand

    Did you mean `doc`?
$ export PATH=$PATH:~/.cargo/bin
$ cargo add package

That's not a great experience. Having it listed in cargo -h and working out of the box would be much better.

@alexcrichton
Copy link
Member

Yeah as @huonw mentioned this is basically a dupe of #4 so I'm going to close this, but I think it's a good point that discovering subcommands may not be the easiest thing to do today. I've opened #2187 on this topic and we can perhaps have discussion over there!

@lig
Copy link

lig commented Nov 15, 2017

You are kidding me. An official package manager bug about not being able to add a dependency via its cli is closed because some other tool out there can do this. Sic.

@tom-james-watson
Copy link

For anyone else confused about the actual answer, first:

cargo install cargo-edit

after that, you can:

cargo add rand

etc.

@ZNackasha
Copy link

@tom-james-watson thanks for the awesome explanation but I still don't see why cargo-edit is not install by default.

@dwijnand
Copy link
Member

There's an intent to. It's now being tracked in #5586 and there's a WIP PR against that too.

@MauroMombelli
Copy link

just hit my head against this, while trying to make my first step in rust. SUPER frustrating

@lavoiesl
Copy link

lavoiesl commented Aug 21, 2019

Also, installing cargo-edit was a whopping 239 crates for me, which takes quite a while to download and install. Definitely not a great user experience.

@BurntSushi
Copy link
Member

Moderator note: Please don't post off topic and unconstructive rants about other software ecosystems in official Rust spaces.

@leodutra
Copy link

leodutra commented Aug 30, 2019

But this is constructive. It's a good example.

@lavoiesl
Copy link

I believe @BurntSushi refers to a comment that was deleted

@BurntSushi
Copy link
Member

BurntSushi commented Aug 30, 2019

Indeed. I'm leaving my comment as a reminder. :)

@leodutra
Copy link

leodutra commented Aug 31, 2019

Is there a specific reason for the add command, and maybe whole cargo-edit, to not be packed with the standard cargo?

Different hands working on each or some other more specific reason?
I got really curious about this second possibility... 😄

@goofballLogic
Copy link

I suspect there's a good reason. Perhaps someone could link a blog or something explaining this design decision?

@jcollum
Copy link

jcollum commented Dec 13, 2019

I was surprised by this too. Coming from node having a command that does "add this package and save it in the list of dependencies" should be available out of the gate. I mean, 90% of the time I install something I'm going to be using it as part of my project, so why not save it and save me the trouble of editing the file?

@kornelski
Copy link
Contributor Author

It's being worked on: #5586

@TomaszWaszczyk
Copy link

"just hit my head against this, while trying to make my first step in rust. SUPER frustrating"++;

@qm3ster
Copy link

qm3ster commented Oct 6, 2020

Until #5586, getting cargo add, cargo rm and cargo upgrade is as simple as doing:

cargo install cargo-edit

One more great thing about it is you don't have to reinstall it for every toolchain update.
It's just there, and works.

You can do it after installing rustup, or you can do it once you encounter the error: no such subcommand: add. It's really not a big deal.

Finding out it exists is the bigger deal. I understand the unwillingness to add it to documentation (eg to the error text when the subcommand is exactly add) because the api of the builtin might be different than this third party utility, but not giving usage instructions and saying it will change and so to use cargo add help would go a long way. Basically it would prevent the cli -> google cargo add -> github/cargo-add -> see it's deprecated -> click deprecation link -> cli journey.

@wongjiahau
Copy link

@qm3ster on top of that, cargo install cargo-edit somehow pulled around 200 packages:
image

@kornelski
Copy link
Contributor Author

It really matters whether it's built-in or something that needs to be installed. For years now, I've pushed for use of cargo-edit on https://lib.rs, but users still ask me to provide copy'n'paste alternative that works without it.

@lig
Copy link

lig commented Nov 20, 2020

@kornelski I'm curious is it possible to install cargo-edit package with the toolchain automatically. Also, it would be helpful for an unfamiliar user to see cargo-edit commands in cargo --help.

@kornelski
Copy link
Contributor Author

This is now tracked in #5586

@wolfsprite
Copy link

wolfsprite commented Apr 23, 2022

I can't believe I had to find this github issue in order to learn how to add dependencies from the terminal. :P

Please add the ability to add dependencies from crates.io to cargo, not involving having to learn that cargo-edit is a thing!

@epage
Copy link
Contributor

epage commented Apr 23, 2022

For anyone on this thread that didn't switch over to the issue posted above (#5586), cargo-add has been merged. It will take several months for it to go from the nightly to beta and then stable release channels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests