From 53e84c571d48d16e1827782a6431eec72d7b75b5 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 16 Mar 2019 15:30:41 +0100 Subject: [PATCH] Only apply default-members when building root manifest --- src/cargo/core/workspace.rs | 10 +++-- src/doc/man/options-packages.adoc | 14 ++++--- tests/testsuite/workspaces.rs | 63 +++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index c36f10d33..d11d00bbc 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -452,11 +452,15 @@ impl<'cfg> Workspace<'cfg> { WorkspaceConfig::Root(ref root_config) => { members_paths = root_config .members_paths(root_config.members.as_ref().unwrap_or(&vec![]))?; - default_members_paths = if let Some(ref default) = root_config.default_members { - Some(root_config.members_paths(default)?) + default_members_paths = if root_manifest_path == self.current_manifest { + if let Some(ref default) = root_config.default_members { + Some(root_config.members_paths(default)?) + } else { + None + } } else { None - } + }; } _ => failure::bail!( "root of a workspace inferred but wasn't a root: {}", diff --git a/src/doc/man/options-packages.adoc b/src/doc/man/options-packages.adoc index 51ab9f0c3..cd671059d 100644 --- a/src/doc/man/options-packages.adoc +++ b/src/doc/man/options-packages.adoc @@ -1,9 +1,13 @@ By default, when no package selection options are given, the packages selected -depend on the current working directory. In the root of a virtual workspace, -all workspace members are selected (`--all` is implied). Otherwise, only the -package in the current directory will be selected. The default packages may be -overridden with the `workspace.default-members` key in the root `Cargo.toml` -manifest. +depend on the selected manifest file (based on the current working directory if +`--manifest-path` is not given). If the manifest is the root of a workspace then +the workspaces default members are selected, otherwise only the package defined +by the manifest will be selected. + +The default members of a workspace can be set explicitly with the +`workspace.default-members` key in the root manifest. If this is not set, a +virtual workspace will include all workspace members (equivalent to passing +`--all`), and a non-virtual workspace will include only the root crate itself. *-p* _SPEC_...:: *--package* _SPEC_...:: diff --git a/tests/testsuite/workspaces.rs b/tests/testsuite/workspaces.rs index 677e09829..e1dba6416 100644 --- a/tests/testsuite/workspaces.rs +++ b/tests/testsuite/workspaces.rs @@ -82,6 +82,44 @@ fn simple_explicit_default_members() { assert!(!p.bin("foo").is_file()); } +#[cargo_test] +fn non_virtual_default_members_build_other_member() { + let p = project() + .file( + "Cargo.toml", + r#" + [project] + name = "foo" + version = "0.1.0" + authors = [] + + [workspace] + members = [".", "bar", "baz"] + default-members = ["baz"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", "pub fn bar() {}") + .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0")) + .file("baz/src/lib.rs", "pub fn baz() {}") + .build(); + + p.cargo("build") + .with_stderr( + "[..] Compiling baz v0.1.0 ([..])\n\ + [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n", + ) + .run(); + + p.cargo("build --manifest-path bar/Cargo.toml") + .with_stderr( + "[..] Compiling bar v0.1.0 ([..])\n\ + [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n", + ) + .run(); +} + #[cargo_test] fn inferred_root() { let p = project() @@ -848,6 +886,31 @@ but is not a member. .run(); } +#[cargo_test] +fn virtual_default_members_build_other_member() { + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["bar", "baz"] + default-members = ["baz"] + "#, + ) + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", "pub fn bar() {}") + .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0")) + .file("baz/src/lib.rs", "pub fn baz() {}") + .build(); + + p.cargo("build --manifest-path bar/Cargo.toml") + .with_stderr( + "[..] Compiling bar v0.1.0 ([..])\n\ + [..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n", + ) + .run(); +} + #[cargo_test] fn virtual_build_no_members() { let p = project().file(