Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,16 @@ pub fn compile_batch(
scope.spawn(move || compile_to_c_with_lang(&path, &output_dir, lang))
})
.collect();
handles.into_iter().map(|h| h.join().unwrap()).collect()
handles
.into_iter()
.map(|h| {
h.join().unwrap_or_else(|_| {
Err(CompileError::Io(std::io::Error::other(
"compile_batch worker thread panicked",
)))
})
})
.collect()
})
}

Expand Down
24 changes: 23 additions & 1 deletion src/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,15 @@ pub fn scan_directory(dir: &Path) -> Vec<(std::path::PathBuf, Language)> {
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
let file_type = match entry.file_type() {
Ok(ft) => ft,
Err(_) => continue,
};
// Do not follow directory symlinks (avoids cycles and unexpected trees).
if file_type.is_symlink() {
continue;
}
if file_type.is_dir() {
// Skip common non-source directories
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if !matches!(
Expand Down Expand Up @@ -494,4 +502,18 @@ mod tests {
assert_eq!(results.len(), 1);
assert_eq!(results[0].1, Language::Zig);
}

#[cfg(unix)]
#[test]
fn test_scan_directory_skips_symlinked_dirs() {
let dir = tempdir().unwrap();
let real = dir.path().join("real");
std::fs::create_dir(&real).unwrap();
std::fs::write(real.join("lib.c"), "").unwrap();
std::os::unix::fs::symlink(&real, dir.path().join("link")).unwrap();

let results = scan_directory(dir.path());
assert_eq!(results.len(), 1);
assert!(results[0].0.ends_with("lib.c"));
}
}
39 changes: 37 additions & 2 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,33 @@ impl LibraryScanner {
let entry = entry.map_err(|e| format!("Failed to read entry: {}", e))?;
let path = entry.path();

if path.is_file() {
let file_type = match entry.file_type() {
Ok(ft) => ft,
Err(_) => continue,
};
if file_type.is_symlink() {
continue;
}
if file_type.is_file() {
if let Some(ext) = path.extension() {
if ext == "h" && !self.should_exclude(&path) {
headers.push(path);
}
}
} else if path.is_dir() && self.options.recursive {
} else if file_type.is_dir() && self.options.recursive {
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if matches!(
name,
"target"
| "node_modules"
| ".git"
| "build"
| "dist"
| "zig-cache"
| "nimcache"
) {
continue;
}
self.scan_directory(&path, discoveries)?;
}
}
Expand Down Expand Up @@ -354,6 +374,21 @@ mod tests {
assert!(headers[0].ends_with("api.h"));
}

#[test]
fn test_scan_skips_target_and_git_dirs() {
let dir = tempdir().unwrap();
let target = dir.path().join("target");
fs::create_dir(&target).unwrap();
fs::write(target.join("generated.h"), "int skip(void);").unwrap();
fs::write(dir.path().join("api.h"), "int add(int a, int b);").unwrap();

let scanner = LibraryScanner::new(dir.path());
let discoveries = scanner.scan().unwrap();
assert_eq!(discoveries.len(), 1);
assert_eq!(discoveries[0].headers.len(), 1);
assert!(discoveries[0].headers[0].ends_with("api.h"));
}

#[test]
fn test_generate_all_creates_mod_rs() {
let dir = tempdir().unwrap();
Expand Down
Loading