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 785be33
Show file tree
Hide file tree
Showing 2 changed files with 18 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
15 changes: 15 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5022,6 +5022,21 @@ 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/file.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"
assert!(p.bin("examples/file").is_file());
assert!(!p.bin("examples/dir").exists());
}

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

0 comments on commit 785be33

Please sign in to comment.