Skip to content

Commit

Permalink
Auto merge of #11678 - hellux:master, r=weihanglo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Feb 4, 2023
2 parents 12a26b3 + bf7f32a commit 82c3bb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,10 @@ fn infer_from_directory(directory: &Path) -> Vec<(String, PathBuf)> {
}

fn infer_any(entry: &DirEntry) -> Option<(String, PathBuf)> {
if entry.path().extension().and_then(|p| p.to_str()) == Some("rs") {
infer_file(entry)
} else if entry.file_type().map(|t| t.is_dir()).ok() == Some(true) {
if entry.file_type().map_or(false, |t| t.is_dir()) {
infer_subdirectory(entry)
} else if entry.path().extension().and_then(|p| p.to_str()) == Some("rs") {
infer_file(entry)
} else {
None
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5022,6 +5022,18 @@ fn inferred_benchmarks() {
p.cargo("bench --bench=bar --bench=baz").run();
}

#[cargo_test]
fn no_infer_dirs() {
let p = project()
.file("src/lib.rs", "fn main() {}")
.file("examples/dir.rs/dummy", "")
.file("benches/dir.rs/dummy", "")
.file("tests/dir.rs/dummy", "")
.build();

p.cargo("build --examples --benches --tests").run(); // should not fail with "is a directory"
}

#[cargo_test]
fn target_edition() {
let p = project()
Expand Down

0 comments on commit 82c3bb7

Please sign in to comment.