You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In addition to users being used to this as they migrate from other ecosystems (see prior art), this provides guard rails to help the user avoid errors and even guide them to best practices, including
$ cargo-add 0.8.0 cargo-add 0.8.0Add dependency to a Cargo.toml manifest fileUSAGE: cargo add [OPTIONS] <CRATE>...ARGS: <CRATE>... Crates to be addedOPTIONS: --allow-prerelease Include prerelease versions when fetching from crates.io (e.g. '0.6.0-alpha') -B, --build Add crate as build dependency --branch <BRANCH> Specify a git branch to download the crate from -D, --dev Add crate as development dependency --features <FEATURES> Space-separated list of features to add. For an alternative approach to enabling features, consider installing the `cargo-feature` utility --git <URI> Specify a git repository to download the crate from -h, --help Print help information --manifest-path <PATH> Path to the manifest to add a dependency to --no-default-features Set `default-features = false` for the added dependency --offline Run without accessing the network --optional Add as an optional dependency (for use in features) -p, --package <PKGID> Package id of the crate to add this dependency to --path <PATH> Specify the path the crate should be loaded from --quiet Do not print any output in case of success -r, --rename <RENAME> Rename a dependency in Cargo.toml, https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml. Only works when specifying a single dependency --registry <REGISTRY> Registry to use --rev <REV> Specify a git branch to download the crate from -s, --sort Sort dependencies even if currently unsorted --tag <TAG> Specify a git branch to download the crate from --target <TARGET> Add as dependency to the given target platform --upgrade <METHOD> Choose method of semantic version upgrade. Must be one of "none" (exact version, `=` modifier), "patch" (`~` modifier), "minor" (`^` modifier), "all" (`>=`), or "default" (no modifier) [default: default] [possible values: none, patch, minor, all, default] -V, --version Print version information --vers <URI> Specify the version to grab from the registry(crates.io). You can also specify version as part of name, e.g `cargo add bitflags@0.3.2`This command allows you to add a dependency to a Cargo.toml manifest file. If <crate> is a github or gitlab repository URL, or a local path, `cargo add` will try to automatically get the crate name and set the appropriate `--git` or`--path` value.Please note that Cargo treats versions like '1.2.3' as '^1.2.3' (and that '^1.2.3' is specified as '>=1.2.3 and <2.0.0'). By default, `cargo add` will use this format, as it is the one that the crates.io registry suggests. One goal of`cargo add` is to prevent you from using wildcard dependencies (version set to '*').
Example commands
cargo add regex
cargo add regex serde
cargo add regex@1
cargo add regex@~1.0
cargo add regex --vers 1.0
cargo add regex --upgrade minor # for some reason this is the policy name for `^`
cargo add https://github.com/rust-lang/regex
cargo add regex --git https://github.com/rust-lang/regex
cargo add ../dependency
cargo add dep --path ../dependency
name@data where data can be version, git (with fragment for branch), etc
-E / --exact, -T / --tilde, -C / --caret to control version requirement operator instead of --upgrade <policy> (also controlled through defaultSemverRangePrefix in config)
Can we define a policy around generated TOML to keep things well scoped?
The goal being to limit the burden to maintainers in dealing with an influx of formatting issues and to keep the UI from being so over complicated that it would be better to edit the file directly
Proposal: (1) Focus on the encouraged path being easy (Pit of Success) and (2) Be opinionated in the style but not in communication
Inspired by rustfmt making things consistent in the little syntax details but preserving higher level organization like newlines and ordering
Use consistent TOML style from run to run (string style, table style), regardless of the existing manifest's style
Preserving sorting where its used; this falls under the "communication" side
However, do not re-format existing code, that belongs more to a "rustfmt for manifests"
This means we'd remove the --sort flag
If the community and the manifest's design evolve where we might want to migrate styles, we either make this configurable in .cargo/config.toml or in the "rustfmt for manifests"
--upgrade <policy> isn't quite right
^ being called minor, should be semver
Policy names don't map well to what Rust developers are used to (operator characters)
Proposal: remove
Redundant with --vers <req> and <name>@<req> except when "take latest" which people are unlikely to do if they also care about the version requirement operator
Most people use default policy anyways
Several issues around the sourcing / constraining
--vers is redundant with @ and is an awkward name
--git and --path are redundant with accepting a path / git url directly
URL detection can be brittle and limits evolution
Proposal
As positional, accept zero or more <name>[:<version-req>] or <path>
Use : like pkgids
Don't accept git url's to keep detection logic simple
Accept --git <url>[#<type>=<ref>] where type is one of branch, tag, or rev
If possible, we auto detect the type from the remote and allow #<ref> (maybe only do this?)
Use a fragment since its client side, it won't conflict with any requirements from the server
Focus is on simplicity, rather than technical completeness, since the UX isn't intended to be exhaustive
Will get name from remote if needed
Alternative, replace --git <url> with git+<url> positional like Poetry
Remove --branch, --vers
Is our story around pre-release complete enough to have --allow-prelease?
See the above issue as one example
Proposal: remove this flag
Pre-releases are uncommon enough that requiring people to explicitly set the version rather than "pick latest" seems sufficient
Pre-releases can break from one to the next, so people probably should be explicit about what they are intended to use
Should we change the default operator when adding pre-release versions?
Activity
epage commentedon Jan 21, 2022
Draft of the proposal to merge
This is a proposal to merge cargo-add into cargo
Motivation
In addition to users being used to this as they migrate from other ecosystems (see prior art), this provides guard rails to help the user avoid errors and even guide them to best practices, including
*versionandpathfordependenciesbutpathonly fordev-dependencies(see Handle package cycles crate-ci/cargo-release#288 which led tocargo addshould "Do the Right Thing" for workspaces #480)--helpis able to provide a targeted subset of the Manifest format documentationDrawbacks
This is another area of consideration for new RFCs like rust-lang/rfcs#3143 or rust-lang/rfcs#2906
This is a high UX feature that will draw a lot of attention (ie Issue influx), e.g.
Cargo.toml#521cargo add: include only MAJOR.MINOR #126Behavior
Help output
Details
Example commands
For an exhaustive set of examples, see test
.tomlspecsParticular points
--vers,--features, etc)dependenciestable is sorted and preserve that sorting when adding a new dependency--renamewill remove a prior--rename--optionalis never removed (cargo-edit#298)--featuresremoves a prior one--vers,--path, and--gitoverwrite each otherAlternatives
Prior Art
git+is needed for inferring git dependencies--versand--gitflags--branch--upgrade <policy>argument to control the version requirement operator--sortto force sorting of dependenciesname@datawhere data can be version, git (with fragment for branch), etc-E/--exact,-T/--tilde,-C/--caretto control version requirement operator instead of--upgrade <policy>(also controlled throughdefaultSemverRangePrefixin config)--cachedfor using the lock file (cargo add: Be clever and infer version from Cargo.lock #41)--dev, it has--prefer-devwhich will only add the dependency if it doesn't already exist independenciesas well asdev-dependencies--mode update-lockfilewill ensure the lock file gets updated as well@<version><name>[@<version>]with path and repogithub:user/repo--save-exact/-Efor operators outside of the default@<version>@none--version/-vinstead of--vers(we use--versbecause of--version/-V)--sourceinstead ofpath(pathcorrelates to manifest field)--git/--branchlikecargo-add--git-urlinstead of--git--git-refinstead of--branch,--tag,--revFuture Possibilities
--localflagcargo add#512)cargo add: Be clever and infer version from Cargo.lock #41)cargo-rm,cargo-upgrade, andcargo-set-version(in that order of priority)cargo addsnippets in addition to or replacing the manifest snippetsFor more, see https://github.com/killercup/cargo-edit/issues?q=is%3Aissue+is%3Aopen+label%3Acargo-add
Questions
--sortflag.cargo/config.tomlor in the "rustfmt for manifests"--upgrade <policy>isn't quite right^being calledminor, should besemver--vers <req>and<name>@<req>except when "take latest" which people are unlikely to do if they also care about the version requirement operator--versis redundant with@and is an awkward name--gitand--pathare redundant with accepting a path / git url directly<name>[:<version-req>]or<path>:like pkgids--git <url>[#<type>=<ref>]where type is one ofbranch,tag,orrev#<ref>(maybe only do this?)--git <url>withgit+<url>positional like Poetry--branch,--vers--allow-prelease?--allow-prerelease, this helps encourage a best practice that prevents users from being brokendev/rust.nix: remove cargo-edit from extraPackages