Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Capture compilation errors to avoid affecting the entire process #307

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 16 additions & 16 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,23 @@ where
.with_origin(file_path.as_os_str().to_str().unwrap());

if args.get_flag("path-as-namespace") {
compiler
.new_namespace(file_path.to_string_lossy().as_ref());
compiler.new_namespace(file_path.to_string_lossy().as_ref());
}

let _ = compiler.add_source(src);
// Capture compilation errors to avoid affecting the entire process
match compiler.add_source(src) {
Ok(_) => {
state.num_compiled_files =
state.num_compiled_files.saturating_add(1);
}
Err(err) => {
eprintln!("Failed to compile `{}`: {}", file_path.display(), err);
}
}

state.file_in_progress = None;

state.num_compiled_files =
state.num_compiled_files.saturating_add(1);

Ok(())
},
// Any error occurred during walk is aborts the walk.
Err,
) {
if let Some(console) = console {
Expand All @@ -271,21 +274,18 @@ where
console.finalize(&state)?;
}

for error in compiler.errors() {
eprintln!("{}", error);
}

for warning in compiler.warnings() {
eprintln!("{}", warning);
}

if !compiler.errors().is_empty() {
bail!("{} errors found", compiler.errors().len());
}

let rules = compiler.build();

if state.num_compiled_files == 0 {
bail!("No valid YARA rules compiled.");
}

Ok(rules)

}

struct CompileState {
Expand Down
Loading