Skip to content

Commit bf7f32a

Browse files
committed
util toml targets: infer file only if not a dir
Any directory entry ending with `.rs`, including directories, were previously assumed to be files, and could end up as targets. Now only regular files and symbolic links are inferred.
1 parent 12a26b3 commit bf7f32a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,10 @@ fn infer_from_directory(directory: &Path) -> Vec<(String, PathBuf)> {
624624
}
625625

626626
fn infer_any(entry: &DirEntry) -> Option<(String, PathBuf)> {
627-
if entry.path().extension().and_then(|p| p.to_str()) == Some("rs") {
628-
infer_file(entry)
629-
} else if entry.file_type().map(|t| t.is_dir()).ok() == Some(true) {
627+
if entry.file_type().map_or(false, |t| t.is_dir()) {
630628
infer_subdirectory(entry)
629+
} else if entry.path().extension().and_then(|p| p.to_str()) == Some("rs") {
630+
infer_file(entry)
631631
} else {
632632
None
633633
}

tests/testsuite/build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5022,6 +5022,18 @@ fn inferred_benchmarks() {
50225022
p.cargo("bench --bench=bar --bench=baz").run();
50235023
}
50245024

5025+
#[cargo_test]
5026+
fn no_infer_dirs() {
5027+
let p = project()
5028+
.file("src/lib.rs", "fn main() {}")
5029+
.file("examples/dir.rs/dummy", "")
5030+
.file("benches/dir.rs/dummy", "")
5031+
.file("tests/dir.rs/dummy", "")
5032+
.build();
5033+
5034+
p.cargo("build --examples --benches --tests").run(); // should not fail with "is a directory"
5035+
}
5036+
50255037
#[cargo_test]
50265038
fn target_edition() {
50275039
let p = project()

0 commit comments

Comments
 (0)