Skip to content

Commit 3d5d867

Browse files
authored
Implement support for Cargo.toml's default-members (#4201)
Cargo has resolved rust-lang/cargo#8033. Invoking `cargo kani` now correctly mimics cargo's behavior when there is a `default-members` entry in `Cargo.toml`. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 192911d commit 3d5d867

File tree

8 files changed

+60
-9
lines changed

8 files changed

+60
-9
lines changed

kani-driver/src/call_cargo.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,14 @@ crate-type = ["lib"]
415415

416416
/// Extract the packages that should be verified.
417417
///
418-
/// The result is built following these rules:
418+
/// The result is built following these rules (mimicking cargo, see
419+
/// https://github.com/rust-lang/cargo/blob/master/src/cargo/core/workspace.rs):
419420
/// - If `--package <pkg>` is given, return the list of packages selected.
420421
/// - If `--exclude <pkg>` is given, return the list of packages not excluded.
421422
/// - If `--workspace` is given, return the list of workspace members.
422-
/// - If no argument provided, return the root package if there's one or all members.
423-
/// - I.e.: Do whatever cargo does when there's no `default_members`.
424-
/// - This is because `default_members` is not available in cargo metadata.
425-
/// See <https://github.com/rust-lang/cargo/issues/8033>.
423+
/// - Else obtain the set of packages from cargo's default_workspace_members (i.e., if
424+
/// `default-members` is specified in Cargo.toml, use that list; else if a root package is
425+
/// specified use that; else use all members).
426426
///
427427
/// In addition, if either `--package <pkg>` or `--exclude <pkg>` is given,
428428
/// validate that `<pkg>` is a package name in the workspace, or return an error
@@ -462,11 +462,10 @@ crate-type = ["lib"]
462462
.into_iter()
463463
.filter(|pkg| !pkg_ids.contains_key(&pkg.id))
464464
.collect()
465+
} else if args.cargo.workspace {
466+
metadata.workspace_packages()
465467
} else {
466-
match (args.cargo.workspace, metadata.root_package()) {
467-
(true, _) | (_, None) => metadata.workspace_packages(),
468-
(_, Some(root_pkg)) => vec![root_pkg],
469-
}
468+
metadata.workspace_default_packages()
470469
};
471470
trace!(?packages, "packages_to_verify result");
472471
Ok(packages)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
[package]
4+
name = "empty-main"
5+
version = "0.1.0"
6+
edition = "2018"
7+
8+
[dependencies]
9+
10+
[workspace]
11+
members = [ "builds-ok","dont-build-me"]
12+
13+
# This indicates what package to e.g. build with 'cargo build' without --workspace
14+
default-members = [
15+
".",
16+
"builds-ok",
17+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
[package]
4+
name = "builds-ok"
5+
version = "0.1.0"
6+
edition = "2024"
7+
8+
[dependencies]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
#[kani::proof]
4+
fn main() {
5+
assert!(1 == 2);
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
[package]
4+
name = "dont-build-me"
5+
version = "0.1.0"
6+
edition = "2024"
7+
8+
[dependencies]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
fn main() {
4+
this - is - not - valid - rust;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FAILURE\
2+
assertion failed: 1 == 2
3+
VERIFICATION:- FAILED
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
fn main() {
4+
println!("Hello, world!");
5+
}

0 commit comments

Comments
 (0)