Skip to content

Commit 9399229

Browse files
authored
Auto merge of #3092 - jhbabon:fix/dont-panic-on-workspaces, r=alexcrichton
FIX: Don't try to generate Gargo.lock on empty workspaces. There was a `panic!` when the command `cargo update` was executed in a workspace like this: mkdir ws cd ws echo '[workspace]' > Cargo.toml cargo new p1 cargo new p2 cargo update The problem is that cargo tries to generate the `Cargo.lock` file even if there aren't any members on the workspace. This fix checks the existence of members in the workspace before trying to do anything so at least we report an error instead of throwing a `panic!`. Issue related #3080
2 parents 7f642f5 + 015e797 commit 9399229

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/cargo/core/workspace.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'cfg> Workspace<'cfg> {
101101
Ok(ws)
102102
}
103103

104-
/// Creates a "tempoarary workspace" from one package which only contains
104+
/// Creates a "temporary workspace" from one package which only contains
105105
/// that package.
106106
///
107107
/// This constructor will not touch the filesystem and only creates an
@@ -464,6 +464,12 @@ impl<'cfg> Packages<'cfg> {
464464
}
465465
}
466466

467+
impl<'a, 'cfg> Members<'a, 'cfg> {
468+
pub fn is_empty(self) -> bool {
469+
self.count() == 0
470+
}
471+
}
472+
467473
impl<'a, 'cfg> Iterator for Members<'a, 'cfg> {
468474
type Item = &'a Package;
469475

src/cargo/ops/cargo_generate_lockfile.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ pub fn update_lockfile(ws: &Workspace, opts: &UpdateOptions)
3131
bail!("cannot specify both aggressive and precise simultaneously")
3232
}
3333

34+
if ws.members().is_empty() {
35+
bail!("you can't generate a lockfile for an empty workspace.")
36+
}
37+
3438
let previous_resolve = match try!(ops::load_pkg_lockfile(ws)) {
3539
Some(resolve) => resolve,
3640
None => return generate_lockfile(ws),

tests/workspaces.rs

+22
Original file line numberDiff line numberDiff line change
@@ -940,3 +940,25 @@ fn lockfile_can_specify_nonexistant_members() {
940940

941941
assert_that(p.cargo("build").cwd(p.root().join("a")), execs().with_status(0));
942942
}
943+
944+
#[test]
945+
fn you_cannot_generate_lockfile_for_empty_workspaces() {
946+
let p = project("foo")
947+
.file("Cargo.toml", r#"
948+
[workspace]
949+
"#)
950+
.file("bar/Cargo.toml", r#"
951+
[project]
952+
name = "foo"
953+
version = "0.1.0"
954+
authors = []
955+
"#)
956+
.file("bar/src/main.rs", "fn main() {}");
957+
p.build();
958+
959+
assert_that(p.cargo("update"),
960+
execs().with_status(101)
961+
.with_stderr("\
962+
error: you can't generate a lockfile for an empty workspace.
963+
"));
964+
}

0 commit comments

Comments
 (0)