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

fix(upgrade): Update lockfile #756

Merged
merged 6 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ $ cargo upgrade --exclude docopt serde
```console
$ cargo-upgrade upgrade --help
cargo-upgrade [..]
Upgrade dependencies as specified in the local manifest file (i.e. Cargo.toml)
Upgrade dependency version requirements in Cargo.toml manifest files

USAGE:
cargo upgrade [OPTIONS] [DEPENDENCY]...
cargo upgrade [OPTIONS] [DEP_ID]...

ARGS:
<DEPENDENCY>... Crates to be upgraded
<DEP_ID>... Crates to be upgraded

OPTIONS:
--all [deprecated in favor of `--workspace`]
Expand All @@ -161,22 +161,11 @@ OPTIONS:
--workspace Upgrade all packages in the workspace
-Z <FLAG> Unstable (nightly-only) flags

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

If `<dependency>`(s) are provided, only the specified dependencies will be upgraded. The version to
upgrade to for each can be specified with e.g. `docopt@0.8.0` or `serde@>=0.9,<2.0`.

Dev, build, and all target dependencies will also be upgraded. Only dependencies from crates.io are
supported. Git/path dependencies will be ignored.

All packages in the workspace will be upgraded if the `--workspace` flag is supplied. The
`--workspace` flag may be supplied in the presence of a virtual manifest.
To only update Cargo.lock, see `cargo update`.

If the '--to-lockfile' flag is supplied, all dependencies will be upgraded to the currently locked
version as recorded in the Cargo.lock file. This flag requires that the Cargo.lock file is up-to-
date. If the lock file is missing, or it needs to be updated, cargo-upgrade will exit with an error.
If the '--to-lockfile' flag is supplied then the network won't be accessed.

```

Expand Down
43 changes: 22 additions & 21 deletions src/bin/upgrade/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,19 @@ use indexmap::IndexMap;
use semver::{Op, VersionReq};
use termcolor::{Color, ColorSpec, StandardStream, WriteColor};

/// Upgrade dependencies as specified in the local manifest file (i.e. Cargo.toml).
/// Upgrade dependency version requirements in Cargo.toml manifest files
#[derive(Debug, Args)]
#[clap(version)]
#[clap(after_help = "\
This command differs from `cargo update`, which updates the dependency versions recorded in the \
local lock file (Cargo.lock).

If `<dependency>`(s) are provided, only the specified dependencies will be upgraded. The version \
to upgrade to for each can be specified with e.g. `docopt@0.8.0` or `serde@>=0.9,<2.0`.

Dev, build, and all target dependencies will also be upgraded. Only dependencies from crates.io \
are supported. Git/path dependencies will be ignored.

All packages in the workspace will be upgraded if the `--workspace` flag is supplied. The \
`--workspace` flag may be supplied in the presence of a virtual manifest.
To only update Cargo.lock, see `cargo update`.

If the '--to-lockfile' flag is supplied, all dependencies will be upgraded to the currently locked \
version as recorded in the Cargo.lock file. This flag requires that the Cargo.lock file is \
up-to-date. If the lock file is missing, or it needs to be updated, cargo-upgrade will exit with \
an error. If the '--to-lockfile' flag is supplied then the network won't be accessed.")]
an error.")]
pub struct UpgradeArgs {
/// Crates to be upgraded.
#[clap(value_name = "DEP_ID")]
dependency: Vec<String>,

/// Path to the manifest to upgrade
Expand Down Expand Up @@ -76,7 +67,7 @@ pub struct UpgradeArgs {
offline: bool,

/// Upgrade all packages to the version in the lockfile.
#[clap(long, conflicts_with = "dependency")]
#[clap(long)]
to_lockfile: bool,

/// Crates to exclude and not upgrade.
Expand Down Expand Up @@ -141,7 +132,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
}

let manifests = args.resolve_targets()?;
let locked = load_lockfile(&manifests, args.offline).unwrap_or_default();
let locked = load_lockfile(&manifests, args.locked, args.offline).unwrap_or_default();

let selected_dependencies = args
.dependency
Expand All @@ -157,7 +148,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
let mut any_crate_modified = false;
let mut compatible_present = false;
let mut pinned_present = false;
for package in manifests {
for package in &manifests {
let mut manifest = LocalManifest::try_new(package.manifest_path.as_std_path())?;
let mut crate_modified = false;
let mut table = Vec::new();
Expand Down Expand Up @@ -329,8 +320,12 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
}
}

if args.locked && any_crate_modified {
anyhow::bail!("cannot upgrade due to `--locked`");
if any_crate_modified {
if args.locked {
anyhow::bail!("cannot upgrade due to `--locked`");
} else {
load_lockfile(&manifests, args.locked, args.offline)?;
}
}

let unused = selected_dependencies
Expand Down Expand Up @@ -360,6 +355,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {

fn load_lockfile(
targets: &[cargo_metadata::Package],
locked: bool,
offline: bool,
) -> CargoResult<Vec<cargo_metadata::Package>> {
// Get locked dependencies. For workspaces with multiple Cargo.toml
Expand All @@ -371,7 +367,10 @@ fn load_lockfile(
let mut cmd = cargo_metadata::MetadataCommand::new();
cmd.manifest_path(package.manifest_path.clone());
cmd.features(cargo_metadata::CargoOpt::AllFeatures);
let mut other = vec!["--locked".to_owned()];
let mut other = Vec::new();
if locked {
other.push("--locked".to_owned());
}
if offline {
other.push("--offline".to_owned());
}
Expand All @@ -392,7 +391,9 @@ fn find_locked_version(
let req = semver::VersionReq::parse(old_version).ok()?;
for p in locked {
if dep_name == p.name && req.matches(&p.version) {
return Some(p.version.to_string());
let mut v = p.version.clone();
v.build = semver::BuildMetadata::EMPTY;
return Some(v.to_string());
}
}
None
Expand Down Expand Up @@ -458,7 +459,7 @@ impl Dep {

fn locked_version_spec(&self) -> ColorSpec {
let mut spec = ColorSpec::new();
if self.locked_version.is_none() {
if self.locked_version.is_none() || self.latest_version.is_none() {
} else if self.locked_version != self.latest_version {
spec.set_fg(Some(Color::Yellow));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/cargo-upgrade/alt_registry/stderr.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
Updating '[ROOTURL]/alternative-registry' index
name old req locked latest new req note
==== ======= ====== ====== ======= ====
my-package1 0.1.1 - 99999.0.0 99999.0.0
my-package2 0.2 - 99999.0.0 99999.0
my-package1 0.1.1 0.1.1 99999.0.0 99999.0.0
my-package2 0.2 0.2.3 99999.0.0 99999.0
2 changes: 1 addition & 1 deletion tests/cargo-upgrade/dry_run/stderr.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Checking cargo-list-test-fixture's dependencies
name old req locked latest new req note
==== ======= ====== ====== ======= ====
my-package 0.1.1 - 99999.0.0 99999.0.0
my-package 0.1.1 0.1.1 99999.0.0 99999.0.0
warning: aborting upgrade due to dry run
23 changes: 11 additions & 12 deletions tests/cargo-upgrade/exclude_dep/in/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,40 @@ version = "0.1.0"
path = "dummy.rs"

[dependencies]
docopt = "0.8"
docopt = "0.4"
pad = "0.1"
serde_json = "1.0"
syn = { version = "0.11.10", default-features = false, features = ["parsing"] }
serde_json = "20.0"
syn = { version = "0.1.1", default-features = false }
tar = { version = "0.4", default-features = false }
ftp = "2.2.1"
te = { package = "toml_edit", version = "0.1.5" }
ftp = "20.0.0"
te = { package = "toml_edit", version = "0.1.1" }

[dependencies.semver]
features = ["serde"]
version = "0.7"
version = "0.2"

[dependencies.rn]
package = "renamed"
version = "0.1"

[dev-dependencies]
assert_cli = "0.2.0"
tempdir = "0.3"
tempdir = "0.1"

[build-dependencies]
serde = { version = "1.0", git= "https://github.com/serde-rs/serde.git" }

[target.'cfg(unix)'.dependencies]
openssl = "0.9"
openssl = "0.4"

[target."windows.json"]
# let's make it an inline table
dependencies = { rget = "0.3.0" }
dependencies = { rget = "0.4.0" }

[target.'cfg(target_arch = "x86_64")'.dev-dependencies]
geo = { version = "0.7.0", default-features = false, features = ["postgis-integration"] }
geo = { version = "0.2.0", default-features = false }

[target.foo.build-dependencies]
ftp = "2.2.1"
ftp = "0.2.0"

[features]
default = []
Expand Down
7 changes: 3 additions & 4 deletions tests/cargo-upgrade/exclude_dep/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ version = "0.1.0"
path = "dummy.rs"

[dependencies]
docopt = "0.8"
docopt = "0.4"
pad = "99999.0"
serde_json = "99999.0"
syn = { version = "99999.0.0", default-features = false, features = ["parsing"] }
syn = { version = "99999.0.0", default-features = false }
tar = { version = "99999.0", default-features = false }
ftp = "99999.0.0"
te = { package = "toml_edit", version = "99999.0.0" }

[dependencies.semver]
features = ["serde"]
version = "99999.0"

[dependencies.rn]
Expand All @@ -37,7 +36,7 @@ openssl = "99999.0"
dependencies = { rget = "99999.0.0" }

[target.'cfg(target_arch = "x86_64")'.dev-dependencies]
geo = { version = "99999.0.0", default-features = false, features = ["postgis-integration"] }
geo = { version = "99999.0.0", default-features = false }

[target.foo.build-dependencies]
ftp = "99999.0.0"
Expand Down
34 changes: 17 additions & 17 deletions tests/cargo-upgrade/exclude_dep/stderr.log
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Updating '[ROOTURL]/registry' index
Checking None's dependencies
warning: ignoring docopt, excluded by user
name old req locked latest new req note
==== ======= ====== ====== ======= ====
pad 0.1 - 99999.0.0 99999.0
serde_json 1.0 - 99999.0.0 99999.0
syn 0.11.10 - 99999.0.0 99999.0.0
tar 0.4 - 99999.0.0 99999.0
ftp 2.2.1 - 99999.0.0 99999.0.0
te 0.1.5 - 99999.0.0 99999.0.0
semver 0.7 - 99999.0.0 99999.0
rn 0.1 - 99999.0.0 99999.0
assert_cli 0.2.0 - 99999.0.0 99999.0.0
tempdir 0.3 - 99999.0.0 99999.0
serde 1.0 - - 1.0
openssl 0.9 - 99999.0.0 99999.0
rget 0.3.0 - 99999.0.0 99999.0.0
geo 0.7.0 - 99999.0.0 99999.0.0
ftp 2.2.1 - 99999.0.0 99999.0.0
name old req locked latest new req note
==== ======= ====== ====== ======= ====
pad 0.1 0.1.1 99999.0.0 99999.0
serde_json 20.0 20.0.0 99999.0.0 99999.0
syn 0.1.1 0.1.1 99999.0.0 99999.0.0
tar 0.4 0.4.1 99999.0.0 99999.0
ftp 20.0.0 20.0.0 99999.0.0 99999.0.0
te 0.1.1 0.1.1 99999.0.0 99999.0.0
semver 0.2 0.2.3 99999.0.0 99999.0
rn 0.1 0.1.1 99999.0.0 99999.0
assert_cli 0.2.0 0.2.3 99999.0.0 99999.0.0
tempdir 0.1 0.1.1 99999.0.0 99999.0
serde 1.0 1.0.140 - 1.0
openssl 0.4 0.4.1 99999.0.0 99999.0
rget 0.4.0 0.4.1 99999.0.0 99999.0.0
geo 0.2.0 0.2.3 99999.0.0 99999.0.0
ftp 0.2.0 0.2.3 99999.0.0 99999.0.0
2 changes: 1 addition & 1 deletion tests/cargo-upgrade/invalid_flag/stderr.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ error: Found argument '--flag' which wasn't expected, or isn't valid in this con
If you tried to supply `--flag` as a value rather than a flag, use `-- --flag`

USAGE:
cargo upgrade [OPTIONS] [DEPENDENCY]...
cargo upgrade [OPTIONS] [DEP_ID]...

For more information try --help
1 change: 1 addition & 0 deletions tests/cargo-upgrade/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod invalid_workspace_root_manifest;
mod locked;
mod optional_dep;
mod pinned;
mod preserve_op;
mod preserve_precision_major;
mod preserve_precision_minor;
mod preserve_precision_patch;
Expand Down
2 changes: 1 addition & 1 deletion tests/cargo-upgrade/optional_dep/stderr.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Checking cargo-list-test-fixture's dependencies
name old req locked latest new req note
==== ======= ====== ====== ======= ====
my-package 0.1.1 - 99999.0.0 99999.0.0
my-package 0.1.1 0.1.1 99999.0.0 99999.0.0
18 changes: 9 additions & 9 deletions tests/cargo-upgrade/pinned/in/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
default = "1.0"
exact = "=2.0"
lessthan = "<0.4"
lessorequal = "<=3.0"
caret = "^3.0"
tilde = "~4.1.0"
greaterthan = ">2.0"
greaterorequal = ">=2.1.0"
wildcard = "3.*"
default = "0.2"
exact = "=0.2"
lessthan = "<0.2"
lessorequal = "<=0.2"
caret = "^0.2"
tilde = "~0.2.0"
greaterthan = ">0.2"
greaterorequal = ">=0.2.0"
wildcard = "0.2.*"
12 changes: 6 additions & 6 deletions tests/cargo-upgrade/pinned/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ version = "0.0.0"

[dependencies]
default = "99999.0"
exact = "=2.0"
lessthan = "<0.4"
lessorequal = "<=3.0"
exact = "=0.2"
lessthan = "<0.2"
lessorequal = "<=0.2"
caret = "^99999.0"
tilde = "~99999.0.0"
greaterthan = ">2.0"
greaterorequal = ">=2.1.0"
wildcard = "99999.*"
greaterthan = ">0.2"
greaterorequal = ">=0.2.0"
wildcard = "99999.0.*"
22 changes: 11 additions & 11 deletions tests/cargo-upgrade/pinned/stderr.log
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Updating '[ROOTURL]/registry' index
Checking cargo-list-test-fixture's dependencies
name old req locked latest new req note
==== ======= ====== ====== ======= ====
default 1.0 - 99999.0.0 99999.0
exact =2.0 - 99999.0.0 =2.0 pinned
lessthan <0.4 - 99999.0.0 <0.4 pinned
lessorequal <=3.0 - 99999.0.0 <=3.0 pinned
caret ^3.0 - 99999.0.0 ^99999.0
tilde ~4.1.0 - 99999.0.0 ~99999.0.0
greaterthan >2.0 - 99999.0.0 >2.0 compatible
greaterorequal >=2.1.0 - 99999.0.0 >=2.1.0 compatible
wildcard 3.* - 99999.0.0 99999.*
name old req locked latest new req note
==== ======= ====== ====== ======= ====
default 0.2 0.2.3 99999.0.0 99999.0
exact =0.2 0.2.3 99999.0.0 =0.2 pinned
lessthan <0.2 0.1.1 99999.0.0 <0.2 pinned
lessorequal <=0.2 0.2.3 99999.0.0 <=0.2 pinned
caret ^0.2 0.2.3 99999.0.0 ^99999.0
tilde ~0.2.0 0.2.3 99999.0.0 ~99999.0.0
greaterthan >0.2 99999.0.0 99999.0.0 >0.2 compatible
greaterorequal >=0.2.0 99999.0.0 99999.0.0 >=0.2.0 compatible
wildcard 0.2.* 0.2.3 99999.0.0 99999.0.*
note: Re-run with `--pinned` to upgrade pinned version requirements
note: Re-run with `--to-lockfile` to upgrade compatible version requirements
14 changes: 14 additions & 0 deletions tests/cargo-upgrade/preserve_op/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
default = "0.2"
exact = "=0.2"
lessthan = "<0.2"
lessorequal = "<=0.2"
caret = "^0.2"
tilde = "~0.2.0"
greaterthan = ">0.2"
greaterorequal = ">=0.2.0"
wildcard = "0.2.*"
Loading