Skip to content

Commit

Permalink
Ignore workspace.default-members when running cargo install on ro…
Browse files Browse the repository at this point in the history
…ot package of a non-virtual workspace
  • Loading branch information
tedinski committed Sep 9, 2022
1 parent 9467f81 commit 08889ee
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,23 @@ impl<'cfg> Workspace<'cfg> {
})
}

/// Reset "default build" behavior for non-virtual workspaces, so that a build in
/// the workspace root would build the root package, not the packages specified
/// in `workspace.default-members`.
pub fn ignore_default_members(&mut self) -> &mut Workspace<'cfg> {
// If we're building a virtual workspace, then there is no root package to build
if self.is_virtual() {
return self;
}
// If we're not building the root package, then the default members do not matter
if self.current_manifest != self.root_manifest() {
return self;
}

self.default_members = vec![self.current_manifest.clone()];
self
}

/// Returns true if the package is a member of the workspace.
pub fn is_member(&self, pkg: &Package) -> bool {
self.member_ids.contains(&pkg.package_id())
Expand Down
5 changes: 5 additions & 0 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ fn make_ws_rustc_target<'cfg>(
ws.set_ignore_lock(config.lock_update_allowed());
ws.set_require_optional_deps(false);

// `cargo install` effectively does `cargo build` in `pkg`. But when this is the root
// of a non-virtual workspace, that would accidentally build `workspace.default-members`
// instead of `pkg` (possibly not even including `pkg` at all!)
ws.ignore_default_members();

let rustc = config.load_global_rustc(Some(&ws))?;
let target = match &opts.build_config.single_requested_kind()? {
CompileKind::Host => rustc.host.as_str().to_owned(),
Expand Down
40 changes: 40 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,46 @@ fn use_path_workspace() {
assert_eq!(lock, lock2, "different lockfiles");
}

#[cargo_test]
fn path_install_workspace_root_despite_default_members() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "ws-root"
version = "0.1.0"
authors = []
[workspace]
members = ["ws-member"]
default-members = ["ws-member"]
"#,
)
.file("src/main.rs", "fn main() {}")
.file(
"ws-member/Cargo.toml",
r#"
[package]
name = "ws-member"
version = "0.1.0"
authors = []
"#,
)
.file("ws-member/src/main.rs", "fn main() {}")
.build();

p.cargo("install --path")
.arg(p.root())
.arg("ws-root")
.with_stderr_contains(
"[INSTALLED] package `ws-root v0.1.0 ([..])` (executable `ws-root[EXE]`)",
)
// Particularly avoid "Installed package `ws-root v0.1.0 ([..]])` (executable `ws-member`)":
.with_stderr_does_not_contain("ws-member")
.run();
}

#[cargo_test]
fn dev_dependencies_no_check() {
Package::new("foo", "1.0.0").publish();
Expand Down

0 comments on commit 08889ee

Please sign in to comment.