Skip to content

Commit

Permalink
cargo update can deal with some bad lockfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Jul 30, 2018
1 parent 316622a commit 86feda1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/cargo/core/resolver/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Patch {
pub type Metadata = BTreeMap<String, String>;

impl EncodableResolve {
pub fn into_resolve(self, ws: &Workspace) -> CargoResult<Resolve> {
pub fn into_resolve(self, ws: &Workspace, ignore_errors: bool) -> CargoResult<Resolve> {
let path_deps = build_path_deps(ws);

let packages = {
Expand Down Expand Up @@ -83,6 +83,9 @@ impl EncodableResolve {
// Package is found in the lockfile, but it is
// no longer a member of the workspace.
Ok(None)
} else if ignore_errors {
// We are asked to ignore errors
Ok(None)
} else {
let suggestion = dependent_pkg
.map(|p| format!("\n consider running 'cargo update -p {}'", p.name()))
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub fn update_lockfile(ws: &Workspace, opts: &UpdateOptions) -> CargoResult<()>
bail!("you can't update in the offline mode");
}

let previous_resolve = match ops::load_pkg_lockfile(ws)? {
// `ignore_errors` is set to true, because we are about to clean the errors up.
let previous_resolve = match ops::load_pkg_lockfile(ws, true)? {
Some(resolve) => resolve,
None => return generate_lockfile(ws),
};
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{PackageIdSpec, Workspace};
use util::CargoResult;

pub fn pkgid(ws: &Workspace, spec: Option<&str>) -> CargoResult<PackageIdSpec> {
let resolve = match ops::load_pkg_lockfile(ws)? {
let resolve = match ops::load_pkg_lockfile(ws, false)? {
Some(resolve) => resolve,
None => bail!("a Cargo.lock must exist for this command"),
};
Expand Down
7 changes: 4 additions & 3 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use util::Filesystem;
use util::errors::{CargoResult, CargoResultExt};
use util::toml as cargo_toml;

pub fn load_pkg_lockfile(ws: &Workspace) -> CargoResult<Option<Resolve>> {
pub fn load_pkg_lockfile(ws: &Workspace, ignore_errors: bool) -> CargoResult<Option<Resolve>> {
if !ws.root().join("Cargo.lock").exists() {
return Ok(None);
}
Expand All @@ -24,7 +24,7 @@ pub fn load_pkg_lockfile(ws: &Workspace) -> CargoResult<Option<Resolve>> {
(|| -> CargoResult<Option<Resolve>> {
let resolve: toml::Value = cargo_toml::parse(&s, f.path(), ws.config())?;
let v: resolver::EncodableResolve = resolve.try_into()?;
Ok(Some(v.into_resolve(ws)?))
Ok(Some(v.into_resolve(ws, ignore_errors)?))
})()
.chain_err(|| format!("failed to parse lock file at: {}", f.path().display()))?;
Ok(resolve)
Expand Down Expand Up @@ -115,7 +115,8 @@ fn are_equal_lockfiles(mut orig: String, current: &str, ws: &Workspace) -> bool
let res: CargoResult<bool> = (|| {
let old: resolver::EncodableResolve = toml::from_str(&orig)?;
let new: resolver::EncodableResolve = toml::from_str(current)?;
Ok(old.into_resolve(ws)? == new.into_resolve(ws)?)
// `ignore_errors` is set to true, because we may be about to clean the errors up.
Ok(old.into_resolve(ws, true)? == new.into_resolve(ws, true)?)
})();
if let Ok(true) = res {
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn resolve_ws_with_method<'a>(

Some(resolve)
} else {
ops::load_pkg_lockfile(ws)?
ops::load_pkg_lockfile(ws, false)?
};

let resolved_with_overrides = ops::resolve_with_previous(
Expand All @@ -106,7 +106,7 @@ fn resolve_with_registry<'cfg>(
registry: &mut PackageRegistry<'cfg>,
warn: bool,
) -> CargoResult<Resolve> {
let prev = ops::load_pkg_lockfile(ws)?;
let prev = ops::load_pkg_lockfile(ws, false)?;
let resolve = resolve_with_previous(
registry,
ws,
Expand Down
12 changes: 2 additions & 10 deletions tests/testsuite/bad_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,8 @@ Caused by:
);

assert_that(
p.cargo("build").arg("-v"),
execs().with_status(101).with_stderr(
"\
[ERROR] failed to parse lock file at: [..]
Caused by:
package `bar 0.1.0 ([..])` is specified as a dependency, but is missing from the package list
consider running 'cargo update -p foo'
",
),
p.cargo("update -p foo"),
execs().with_status(0),
);
}

Expand Down

0 comments on commit 86feda1

Please sign in to comment.