Skip to content

Commit 82c3bb7

Browse files
committed
Auto merge of #11678 - hellux:master, r=weihanglo
util toml targets: Do not infer directory as a file Any directory entry that ends with `.rs` is currently inferred to be a file, causing it to be added as a target. This means that directories that end with .rs may also end up as targets. An example where this is apparent, running in an existing project directory; ``` $ mkdir -p tests/some_dir.rs $ cargo fmt Error: `tests/some_dir.rs` is a directory ``` causes cargo-fmt to pass the directory to rustfmt, which in turn makes rustfmt exit with an error.
2 parents 12a26b3 + bf7f32a commit 82c3bb7

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/cargo/util/toml/targets.rs

+3-3
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

+12
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)