Skip to content

Commit 7bce509

Browse files
committed
Auto merge of #8511 - eonil:check-member-path-existence, r=ehuss
Check workspace member existence as dir. Cargo command fails if workspace members are set to something like `crates/*` and if there's any non project file in `crates` directory. This PR makes member discovery logic to exclude non-directory paths. In my case, `.DS_Store` (which is made automatically by Finder on macOS) file triggered this issue.
2 parents 8475310 + c5e13da commit 7bce509

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/cargo/core/workspace.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,16 @@ impl WorkspaceRootConfig {
12061206
if expanded_paths.is_empty() {
12071207
expanded_list.push(pathbuf);
12081208
} else {
1209-
expanded_list.extend(expanded_paths);
1209+
// Some OS can create system support files anywhere.
1210+
// (e.g. macOS creates `.DS_Store` file if you visit a directory using Finder.)
1211+
// Such files can be reported as a member path unexpectedly.
1212+
// Check and filter out non-directory paths to prevent pushing such accidental unwanted path
1213+
// as a member.
1214+
for expanded_path in expanded_paths {
1215+
if expanded_path.is_dir() {
1216+
expanded_list.push(expanded_path);
1217+
}
1218+
}
12101219
}
12111220
}
12121221

tests/testsuite/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ mod locate_project;
6262
mod lockfile_compat;
6363
mod login;
6464
mod lto;
65+
mod member_discovery;
6566
mod member_errors;
6667
mod message_format;
6768
mod metabuild;

tests/testsuite/member_discovery.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Tests for workspace member discovery.
2+
3+
use cargo::core::{Shell, Workspace};
4+
use cargo::util::config::Config;
5+
6+
use cargo_test_support::install::cargo_home;
7+
use cargo_test_support::project;
8+
use cargo_test_support::registry;
9+
10+
/// Tests exclusion of non-directory files from workspace member discovery using glob `*`.
11+
#[cargo_test]
12+
fn bad_file_member_exclusion() {
13+
let p = project()
14+
.file(
15+
"Cargo.toml",
16+
r#"
17+
[workspace]
18+
members = [ "crates/*" ]
19+
"#,
20+
)
21+
.file("crates/.DS_Store", "PLACEHOLDER")
22+
.file(
23+
"crates/bar/Cargo.toml",
24+
r#"
25+
[project]
26+
name = "bar"
27+
version = "0.1.0"
28+
authors = []
29+
"#,
30+
)
31+
.file("crates/bar/src/main.rs", "fn main() {}")
32+
.build();
33+
34+
// Prevent this test from accessing the network by setting up .cargo/config.
35+
registry::init();
36+
let config = Config::new(
37+
Shell::from_write(Box::new(Vec::new())),
38+
cargo_home(),
39+
cargo_home(),
40+
);
41+
let ws = Workspace::new(&p.root().join("Cargo.toml"), &config).unwrap();
42+
assert_eq!(ws.members().count(), 1);
43+
assert_eq!(ws.members().next().unwrap().name(), "bar");
44+
}

0 commit comments

Comments
 (0)