Skip to content

Commit 53e84c5

Browse files
Nemo157ehuss
authored andcommitted
Only apply default-members when building root manifest
1 parent 93660b0 commit 53e84c5

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

src/cargo/core/workspace.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,15 @@ impl<'cfg> Workspace<'cfg> {
452452
WorkspaceConfig::Root(ref root_config) => {
453453
members_paths = root_config
454454
.members_paths(root_config.members.as_ref().unwrap_or(&vec![]))?;
455-
default_members_paths = if let Some(ref default) = root_config.default_members {
456-
Some(root_config.members_paths(default)?)
455+
default_members_paths = if root_manifest_path == self.current_manifest {
456+
if let Some(ref default) = root_config.default_members {
457+
Some(root_config.members_paths(default)?)
458+
} else {
459+
None
460+
}
457461
} else {
458462
None
459-
}
463+
};
460464
}
461465
_ => failure::bail!(
462466
"root of a workspace inferred but wasn't a root: {}",

src/doc/man/options-packages.adoc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
By default, when no package selection options are given, the packages selected
2-
depend on the current working directory. In the root of a virtual workspace,
3-
all workspace members are selected (`--all` is implied). Otherwise, only the
4-
package in the current directory will be selected. The default packages may be
5-
overridden with the `workspace.default-members` key in the root `Cargo.toml`
6-
manifest.
2+
depend on the selected manifest file (based on the current working directory if
3+
`--manifest-path` is not given). If the manifest is the root of a workspace then
4+
the workspaces default members are selected, otherwise only the package defined
5+
by the manifest will be selected.
6+
7+
The default members of a workspace can be set explicitly with the
8+
`workspace.default-members` key in the root manifest. If this is not set, a
9+
virtual workspace will include all workspace members (equivalent to passing
10+
`--all`), and a non-virtual workspace will include only the root crate itself.
711

812
*-p* _SPEC_...::
913
*--package* _SPEC_...::

tests/testsuite/workspaces.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,44 @@ fn simple_explicit_default_members() {
8282
assert!(!p.bin("foo").is_file());
8383
}
8484

85+
#[cargo_test]
86+
fn non_virtual_default_members_build_other_member() {
87+
let p = project()
88+
.file(
89+
"Cargo.toml",
90+
r#"
91+
[project]
92+
name = "foo"
93+
version = "0.1.0"
94+
authors = []
95+
96+
[workspace]
97+
members = [".", "bar", "baz"]
98+
default-members = ["baz"]
99+
"#,
100+
)
101+
.file("src/main.rs", "fn main() {}")
102+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
103+
.file("bar/src/lib.rs", "pub fn bar() {}")
104+
.file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
105+
.file("baz/src/lib.rs", "pub fn baz() {}")
106+
.build();
107+
108+
p.cargo("build")
109+
.with_stderr(
110+
"[..] Compiling baz v0.1.0 ([..])\n\
111+
[..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
112+
)
113+
.run();
114+
115+
p.cargo("build --manifest-path bar/Cargo.toml")
116+
.with_stderr(
117+
"[..] Compiling bar v0.1.0 ([..])\n\
118+
[..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
119+
)
120+
.run();
121+
}
122+
85123
#[cargo_test]
86124
fn inferred_root() {
87125
let p = project()
@@ -848,6 +886,31 @@ but is not a member.
848886
.run();
849887
}
850888

889+
#[cargo_test]
890+
fn virtual_default_members_build_other_member() {
891+
let p = project()
892+
.file(
893+
"Cargo.toml",
894+
r#"
895+
[workspace]
896+
members = ["bar", "baz"]
897+
default-members = ["baz"]
898+
"#,
899+
)
900+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
901+
.file("bar/src/lib.rs", "pub fn bar() {}")
902+
.file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
903+
.file("baz/src/lib.rs", "pub fn baz() {}")
904+
.build();
905+
906+
p.cargo("build --manifest-path bar/Cargo.toml")
907+
.with_stderr(
908+
"[..] Compiling bar v0.1.0 ([..])\n\
909+
[..] Finished dev [unoptimized + debuginfo] target(s) in [..]\n",
910+
)
911+
.run();
912+
}
913+
851914
#[cargo_test]
852915
fn virtual_build_no_members() {
853916
let p = project().file(

0 commit comments

Comments
 (0)