Skip to content

Commit

Permalink
util toml targets: infer file only if not a dir
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hellux committed Feb 4, 2023
1 parent 12a26b3 commit bf7f32a
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 bf7f32a

Please sign in to comment.