Giter Club home page Giter Club logo

cargo-edit's Introduction

cargo edit

This tool extends Cargo to allow you to add, remove, and upgrade dependencies by modifying your Cargo.toml file from the command line.

Currently available subcommands:

Build Status Build Status Build status Coverage Status crates.io Join the chat at https://gitter.im/cargo-edit/Lobby

Installation

Packaging status

Ensure that you have a fairly recent version of rust/cargo installed. On Ubuntu you would also need to install libssl-dev and pkg-config packages.

$ cargo install cargo-edit

If you wish to use a bundled version of openssl:

$ cargo install cargo-edit --features vendored-openssl

Compiler support: requires rustc 1.44+

(Please check cargo's documentation to learn how cargo install works and how to set up your system so it finds binaries installed by cargo.)

Install a sub-set of the commands with cargo install -f --no-default-features --features "<COMMANDS>", where <COMMANDS> is a space-separated list of commands; i.e. add rm upgrade for the full set.

Available Subcommands

cargo add

cargo add is now integrated into cargo as of v1.62. If you want access in older versions of cargo, you'll need to install cargo-edit v0.9 or earlier.

Known differences from cargo-edit v0.9.1

  • cargo add <path> is unsupported, instead use cargo add --path <path>
  • cargo add <crate> +<feature> is unsupported, instead use cargo add <crate> -F <feature>
    • If adding multiple crates, qualify the feature like cargo add serde -F serde/derive serde_json
    • See rust-lang/cargo#10809

cargo rm

cargo rm is now integrated into cargo as of v1.66. If you want access in older versions of cargo, you'll need to install cargo-edit v0.11 or earlier.

cargo upgrade

Upgrade dependencies in your Cargo.toml to their latest versions.

To specify a version to upgrade to, provide the dependencies in the <crate name>@<version> format, e.g. cargo upgrade -p docopt@~0.9.0 -p serde@>=0.9,<2.0.

This command differs from cargo update, which updates the dependency versions recorded in the local lock file (Cargo.lock).

Examples

# Upgrade all dependencies for the current crate
$ cargo upgrade
# Upgrade docopt (to ~0.9) and serde (to >=0.9,<2.0)
$ cargo upgrade -p docopt@~0.9 -p serde@>=0.9,<2.0
# Upgrade all dependencies except docopt and serde
$ cargo upgrade --exclude docopt --exclude serde

Usage

$ cargo-upgrade upgrade --help
Upgrade dependency version requirements in Cargo.toml manifest files

Usage: cargo upgrade [OPTIONS]

Options:
      --dry-run               Print changes to be made without making them
      --manifest-path <PATH>  Path to the manifest to upgrade
      --rust-version <VER>    Override `rust-version`
      --ignore-rust-version   Ignore `rust-version` specification in packages
      --offline               Run without accessing the network
      --locked                Require `Cargo.toml` to be up to date
  -v, --verbose...            Use verbose output
  -Z <FLAG>                   Unstable (nightly-only) flags
  -h, --help                  Print help
  -V, --version               Print version

Version:
      --compatible [<allow|ignore>]    Upgrade to latest compatible version [default: allow]
  -i, --incompatible [<allow|ignore>]  Upgrade to latest incompatible version [default: ignore]
      --pinned [<allow|ignore>]        Upgrade pinned to latest incompatible version [default:
                                       ignore]

Dependencies:
  -p, --package <PKGID[@<VERSION>]>  Crate to be upgraded
      --exclude <PKGID>              Crates to exclude and not upgrade
      --recursive [<true|false>]     Recursively update locked dependencies

cargo set-version

Set the version in your Cargo.toml.

Examples

# Set the version to the version 1.0.0
$ cargo set-version 1.0.0
# Bump the version to the next major
$ cargo set-version --bump major
# Bump version to the next minor
$ cargo set-version --bump minor
# Bump version to the next patch
$ cargo set-version --bump patch

Usage

$ cargo-set-version set-version --help
Change a package's version in the local manifest file (i.e. Cargo.toml)

Usage: cargo set-version [OPTIONS] [TARGET]

Arguments:
  [TARGET]  Version to change manifests to

Options:
      --bump <BUMP>           Increment manifest version
  -m, --metadata <METADATA>   Specify the version metadata field (e.g. a wrapped libraries version)
      --manifest-path <PATH>  Path to the manifest to upgrade
  -p, --package <PKGID>       Package id of the crate to change the version of
      --all                   [deprecated in favor of `--workspace`]
      --workspace             Modify all packages in the workspace
      --dry-run               Print changes to be made without making them
      --exclude <EXCLUDE>     Crates to exclude and not modify
      --offline               Run without accessing the network
      --locked                Require `Cargo.toml` to be up to date
  -Z <FLAG>                   Unstable (nightly-only) flags
  -h, --help                  Print help
  -V, --version               Print version

For more on metadata, see the semver crate's documentation.

Related Cargo Commands

Contribution

Thanks for your interest - we gratefully welcome contributions.

Questions can be asked in issues, or on Gitter.

To help us help you get pull requests merged quickly and smoothly, open an issue before submitted large changes. Please keep the contents of pull requests and commits short. Commit messages should include the intent of the commit.

cargo-edit has a moderately comprehensive test suite. Contributions that add/improve tests are awesome. Please add tests for every change.

cargo-edit uses rustfmt for formatting and clippy for linting.

License

Apache-2.0/MIT

cargo-edit's People

Contributors

alsuren avatar bjgill avatar bors[bot] avatar cassaundra avatar cheesedosa avatar dandroid avatar dcjanus avatar dependabot-support avatar dherman avatar epage avatar homu avatar killercup avatar liushuyu avatar marcoieni avatar meltinglava avatar mrjohz avatar mseri avatar ordian avatar otavio avatar poliorcetics avatar rgardner avatar savente93 avatar sebasgarcep avatar smoelius avatar tbu- avatar tofay avatar tshepang avatar waynenilsen avatar whentze avatar yberreby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cargo-edit's Issues

`cargo add`: short caret versions

I'm not sure how to call it exactly, but it would be great if cargo add supported adding "short" caret versions (which are "just" versions in Cargo, without any sigils), i.e. major.minor instead of major.minor.patch. As far as I understand semver, this should really be the default mode, as patch versions should keep backwards compatibility and are safe to upgrade to. Adding full versions by default is too restrictive, I believe.

`cargo rm`: Integration tests

Like @rgardner described in #25:

  • removing an existing dependency works
  • removing an existing dependency from a specific section works
  • removing a non-existing dependency shows a nice error message
  • removing an dependency from a non-existing section shows a nice error message
  • remove the section when the last dependency has been removed

Adding multiple crates doesn't seem to work

I've never been able to get cargo add to add multiple crates at once. I always get a 404 from crates.io:

$ cargo add iron log mount
Command failed due to unhandled error: crates.io Error: NotFound

I've ensured I have the latest version installed and have tried using commas instead of spaces as the separator, but I always get this result.

`cargo list`: (optionally) print license types.

I've been working on a commercial project in Rust and I've just realized that with just 3 dependencies in Cargo.toml I have 61 transitive dependencies.
It would be nice if I could easily list the license for all of them, to be able to quickly identify potentially problematic crates (GPL comes to mind, but there are probably others that cannot simply be included in arbitrary commercial software).

Why is --optional mutually exclusive with --build and --dev?

--optional      Add as an optional dependency (for use in features). This does not work
                for `dev-dependencies` or `build-dependencies`.

Why is this? Several of my projects have a build dependency that is optional, and I always have to move things around manually since cargo add has this limitation.

Unfriendly error message when installing with `cargo install cargo-edit`

Obviously I need to install ssl and crypto libraries but perhaps a check and a better error message would be in order for future users.

   Compiling cargo-edit v0.1.6
error: linking with `cc` failed: exit code: 1
  |
  = note:

...

  = note: /usr/bin/ld: cannot find -lssl
          /usr/bin/ld: cannot find -lcrypto
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: failed to compile `cargo-edit v0.1.6`, intermediate artifacts can be found at `/tmp/cargo-install.s6heJAMi41R8`

Caused by:
  Could not compile `cargo-edit`.

cargo list does not work correctly in presence of path dependencies

% cargo new root
% cargo new child
% cd child
% cargo add --path ../root root
% cargo build
   Compiling root v0.1.0 (file:///private/tmp/child)
   Compiling child v0.1.0 (file:///private/tmp/child)
% cargo list
Could not list your stuff.

ERROR: Couldn't read version of root
Couldn't read version of root

`cargo add` enhancements

It might be a good idea to enable doing certain fixes (as suggested in cargo build error messages) either automatically or with a new switch.

For example, if I had a new rustc target named atom-unknown-linux-gnu, I might need to add it to Cargo toml in:

.cargo/registry/src/github.com-121aea75f9ef2ce2/glutin-0.3.5

or if using a nightly and getting an error suggestion #![feature(core_intrinsics)] is required in some crate, it could speed up adding that line to lib.rs located in:

.cargo/registry/src/github.com-121aea75f9ef2ce2/gfx-0.7.0/src

Preferably features such as these could land in cargo itself at some point in the future.

Change how `cargo list` works, add `[--dev|--build]` options

As mentioned in #42, #47, and #48, cargo list needs to be refactored.

I suggest to first change it to only allow cargo list [--dev|--build], which lists (dev-/build-)dependencies. This includes:

  • List dependencies by section
  • Correctly show dependencies with version, path, repository (cf. #48)
  • Indicate whether a dependency is marked as optional

Some time ago, I already created a few issues that describe further options. For the record, those are:

  • cargo list (bins | tests | examples) (#20)
  • cargo list features [--recursive] [--manifest-path] (#21)

Cargo install problem

As reported on irc by seekr, cargo install --git "https://github.com/killercup/cargo-edit.git" --branch "feature/hyper-hyper" does not work, most likely there's a problem with Cargo.toml files in the tests directories (thanks, sfackler). An easy fix would be to rename them.

`cargo add`: Nicer TOML Files

  • Don't mess up Cargo.toml structure too much, e.g. keep the ordering
  • Don't remove comments.
  • Use inline tables whenever possible, e.g. clippy = {version = "0.0.19", optional = true}

Do not remove comments from the Cargo.toml

Currently cargo-add removes comments from the Cargo.toml file

[/tmp/a (master#%)]% cat Cargo.toml
[package]
authors = ["author"]
name = "a"
version = "0.1.0"

# Comment
[/tmp/a (master#%)]% cargo add rustc-serialize                            
[/tmp/a (master#%)]% cat Cargo.toml           
[package]
authors = ["author"]
name = "a"
version = "0.1.0"

[dependencies]
rustc-serialize = "0.3.16"

It would be nice if it can leave them untouched. Especially in development it might happen that some comments are left in.

Separate Binaries

Move to current functionality into separate binaries:

  • #1: cargo list <section> [--tree] [--help] [--manifest-path]
  • #8: cargo add <name> [--version|--git|--path] [--dev|--build]
  • #6, #10: Update Readme, docs

cargo list --tree ignores recently added crates

Adding a dependendency shows up with cargo list:

$ cargo add lazy_static
$ cargo list
lazy_static 0.2.2
libc        0.2.17

However, cargo list --tree does not:

$ cargo list --tree
└── libc (0.2.17)

The dependency does not show up with --tree until I've done a build and the new crate is downloaded. Ideally cargo list would be consistent with cargo list --tree.

`cargo rm` breaks Cargo.toml

Given this Cargo.toml:

[package]
authors = ["Yohai Berreby (filsmick) <[email protected]>"]
name = "cargo-edit-bug"
version = "0.1.0"

[dependencies]
quick-error = "0.1.3"
time = "0.1.32"

cargo rm quick-error produces the following invalid .toml:

[package]
authors = ["Yohai Berreby (filsmick) <[email protected]>"]
name = "cargo-edit-bug"
version = "0.1.0"

[dependencies]
time = "0.1.32"
.1.3"
time = "0.1.32"

This is not related to the '-' in the crate name:

[dependencies]
foo = "0.1.3"
bar = "0.1.32"

becomes

[dependencies]
bar = "0.1.32"
ar = "0.1.32"

after cargo rm foo.

Last but not least: cargo rm bar on the previous example doesn't change Cargo.toml at all.

It seems related to the patch version: I couldn't reproduce the problem when a major.minor version was specified.

add support for editing `.cargo/config`

Quite often during development when I want to test out how changes in a deeper dependency affect a crate, I add that dependency to the paths attribute of .cargo/config. (and AFAIK that is the only way to achieve that specific override behaviour.

I would propose to use a additional --override flag which would then use the following argument as the local path:

cargo add --override /my/local/crate

I would also understand it if you would prefer a more explicit usage, requiring the --path attribute like so:

cargo add --override --path=/my/local/crate

I don't know what other functionality is provided trough the .cargo/config file (and how to integrate them), since that above use case was my primary motivation.

I would love to get some feedback on the command syntax and/or if you would like to have support for editing .cargo/config file at all in cargo-edit.

Comments are not preserved when Cargo.toml is rewritten

This is probably out of this crate's control (as I imagine toml just skips over commented lines, as it probably should), but I was disappointed to find out that cargo add'ing a dependency rewrote my entire Cargo.toml, removing a commented-out feature that I wanted to save for later.

Feel free to close this as wontfix, but I think it would be really neat if the Cargo.toml could be rewritten while preserving comments, as there could be significant amounts of documentation in comments for the reasoning on various dependencies and features.

Add latest stable release of a crate

Right now, if not explicitely specified, cargo-add will add the latest release version of a crate. Sometimes the latest one is an alpha release and instead the last stable one should be used.

Example: *ring* v0.6.0-alpha vs v0.5.3

`cargo list`: show package source if not from crates.io

cargo list --tree is very useful in finding where multiple package versions are pulled into the dependency graph.

With packages sourced not from crates.io, but from local paths or remote git repos, the version number can be misleading, because the repo might have the same version as the crates.io package, but they are still different packages. Could cargo list show the package source if it's not crates.io?

Publish to crates.io, test with `cargo install`

The first implementation of cargo install landed last night! 🎉

So, let's get cargo-edit ready for a 0.1 release and publish it to crates.io. Ideally, I want to cargo install cargo-edit tomorrow!

A few things to note:

  • This will be a 0.1 release not promising any kind of stability of anything, least of all the CLI arguments!
  • For testing, cargo install --git https://github.com/killercup/cargo-edit.git should work
  • I just notices cargo install --vers is equivalent to cargo add --ver. Maybe we want to change to also use --vers?

cargo add mixes up dashes and underscores

I just ran cargo add generic_matrix. It worked, even though the crate on crates.io is actually named generic-matrix. It added the version bound based on the correct package, but of course when trying to build afterwards, cargo couldn't find generic_matrix.

`cargo add`: Be clever and infer version from Cargo.lock

Like @gankro and @filsmick described on Reddit, cargo add could learn to add dependencies with versions that are already used by another dependency.

Let's say I have my-http-thingy = "0.4.2" already in my Cargo.toml. Now, we run cargo add my-url-parser. Instead of fetching the latest version (e.g., 6.6.6), we should first suggest to use 1.3.37 which my-http-thingy already depends on (and which is incompatible to the latest version).

I'm not sure if we should do this in every case. If part of my dependency tree looks like a (0.2.1) → b (0.4.2) → c (0.6.0) → d (1.2.0) and I want to cargo add d, is the version c depends on still relevant? How should we tell this to a user?

Should we give the user a choice or should we only do this when a special argument is given (e.g., --infer-versions, or --keep-dependencies-flat)? (Personally, I thing interactivity is okay for cargo add, even though it might make the code more complex and we should offer an op-out).

'cargo add' crashes without a direct internet connection

$ RUST_BACKTRACE=1 cargo add libc
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: failed init', ../src/libcore/result.rs:799
stack backtrace:
   1:     0x2b52832599bf - std::sys::backtrace::tracing::imp::write::h6f1d53a70916b90d
   2:     0x2b528325cf5d - std::panicking::default_hook::{{closure}}::h137e876f7d3b5850
   3:     0x2b528325beea - std::panicking::default_hook::h0ac3811ec7cee78c
   4:     0x2b528325c488 - std::panicking::rust_panic_with_hook::hc303199e04562edf
   5:     0x2b528325c322 - std::panicking::begin_panic::h6ed03353807cf54d
   6:     0x2b528325c260 - std::panicking::begin_panic_fmt::hc321cece241bb2f5
   7:     0x2b528325c1e1 - rust_begin_unwind
   8:     0x2b52832979cf - core::panicking::panic_fmt::h27224b181f9f037f
   9:     0x2b528324c114 - core::result::unwrap_failed::h27844398571de25d
  10:     0x2b528324e57c - curl::http::handle::Handle::new::hc96cb285218d129a
  11:     0x2b528315bd51 - cargo_add::fetch::get_latest_dependency::h387b0ad0c0352809
  12:     0x2b5283161fcc - cargo_add::handle_add::h5b1228548cd89fc4
  13:     0x2b52831636c7 - cargo_add::main::ha18a7ae612e2c426
  14:     0x2b5283264a26 - __rust_maybe_catch_panic
  15:     0x2b528325b761 - std::rt::lang_start::h538f8960e7644c80
  16:     0x2b528457d9f3 - __libc_start_main
  17:     0x2b5283155818 - <unknown>

This is on a machine that requires using a proxy to access URLs.

I get the same error even if I set HTTP_PROXY environment variables.

Adding 'extern crate' to main.rs/lib.rs

If the Cargo project contains a single library or executable, it might be nice to have a flag that automatically adds a extern crate X; statement to the main.rs or lib.rs file.

Have you thought about this?

cargo add: Don't add yanked versions

I recently stumbled upon this problem when trying to add 'sdl2_image' using cargo-edit.
The most recent version is yanked, which means that it shouldn't be added.
(To see it, go here)

Update dependency on curl

I'm trying to diagnose why installation of cargo-edit with the MSVC toolchain is failing, and based on discussion in alexcrichton/curl-rust#92, it looks like the problem is you're depending on too old a version of curl. Could the dependency be updated?

`cargo add`: Get version info from crates.io

When not specifying the version of a crate (e.g. cargo add regex), automatically make a request to crates.io to determine it (see comments below).

Wildcard dependencies like regex = "*" are evil.

Possible errors to consider and handle:

  • "crate not found"
  • "could not connect to crates.io"
  • "could not parse crates.io response"

cargo install: "failed to unpack package `cargo-edit v0.1.1`"

I am trying to install cargo-edit but it fails with cargo install

I get this error message:

failed to unpack package `cargo-edit v0.1.1`

Caused by:
  No such file or directory (os error 2)

I get the same error when I try installing via cargo-extras

failed to compile `cargo-extras v0.3.0`, intermediate artifacts can be found at `/home/azerupi/target-install`

Caused by:
  unable to get packages from source

Caused by:
  failed to unpack package `cargo-edit v0.1.1`

Caused by:
  No such file or directory (os error 2)

This is probably connected with this cargo issue: rust-lang/cargo#2326

cargo list shows an error if [dependencies] section is absent

The following is an entirely valid manifest with no dependencies:

[package]
name = "root"
version = "0.1.0"
authors = ["Vladimir Matveev <[email protected]>"]

However, cargo list shows an error when run against it:

% cargo list
Could not list your stuff.

ERROR: Couldn't read section dependencies
Couldn't read section dependencies

Adding an empty [dependencies] section fixes it, but it should be unnecessary.

Display currently used version of dependencies in cargo-list

Currently cargo list only displays the version-requirement specified in Cargo.toml. It would be nice to be able to list that along with the actual selected version of each package (the version displayed by cargo list --tree).

I have some code which works but I'm also testing with listing the latest version from crates.io too, might be interesting to merge the two functionalities together (optional of course, since finding the latest version requires an internet connection), along with optional coloring of the dependencies depending on if they are up to date or not. If there is enough interest I can clean it up and make a PR.

Consider allowing multiple crates.io dependencies to `cargo add`

When starting a new project, I'll often know about several things I'll need/want, e.g. hyper serde serde_json for a typical web project, or for a library project quickcheck quickcheck_macros as dev-dependencies. It might nice to allow adding multiple crates.io in one go, e.g.

$ cargo add hyper serde serde_json
$ cargo add --dev quickcheck quickcheck_macros

I guess this may not allow specifying versions (although one could do something like cargo add hyper --vers 1.0 serde --vers 2.0, maybe?).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.