From 8bc16ac940ae0407bbece3df9c2f5342244eddaf Mon Sep 17 00:00:00 2001 From: elipeter Date: Mon, 23 Jun 2025 17:45:54 +0200 Subject: [PATCH] Refactor and enhance debugging in `walk.rs` and `scan.rs` - Removed unused commented-out code in `walk.rs` for improved readability. - Added more `tracing::debug` logs for clearer traceability during file scanning and rule processing. - Improved condition handling and formatting consistency in `scan.rs`. - Simplified error management and removed redundant comments in database-related functions. --- src/commands/scan.rs | 25 +++++++++++++------------ src/walk.rs | 29 ++--------------------------- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/src/commands/scan.rs b/src/commands/scan.rs index 3450e1a4..afcc58b6 100644 --- a/src/commands/scan.rs +++ b/src/commands/scan.rs @@ -49,11 +49,11 @@ pub fn handle( diags = scan_with_index_parallel(&project_name, pool, config)?; } - if format == "console" || format == "" && config.output.default_format == "console" { + tracing::debug!("Found {:?} issues.", diags.len()); + + if format == "console" || (format == "" && config.output.default_format == "console") { + tracing::debug!("Printing to console"); for d in &diags { - if d.severity != Severity::High { - continue; - } let sev_str = match d.severity { Severity::High => style("HIGH").red().bold(), Severity::Medium => style("MEDIUM").yellow().bold(), @@ -85,14 +85,14 @@ fn scan_filesystem( rx.into_iter() .flatten() - .par_bridge() // rayon hand-off - .try_for_each(|path| { // stable API - let mut local = run_rules_on_file(&path, cfg).unwrap(); // <- same as before + .par_bridge() + .try_for_each(|path| { + let mut local = run_rules_on_file(&path, cfg).unwrap(); let mut guard = acc.lock().unwrap(); guard.append(&mut local); - Ok::<(), DynError>(()) // explicit error type - })?; // propagate first error, if any - + Ok::<(), DynError>(()) + })?; + Ok(acc.into_inner().unwrap()) } @@ -101,8 +101,6 @@ fn scan_with_index_parallel( pool: Arc>, cfg: &Config, ) -> Result, Box> { - - // Get the file list once (single connection, no contention) let files = { let idx = Indexer::from_pool(project, &pool)?; idx.get_files(project)? @@ -153,6 +151,7 @@ pub(crate) fn run_rules_on_file( path: &Path, cfg: &Config, ) -> Result, Box> { + tracing::debug!("Running rules on {}", path.to_string_lossy()); let bytes = std::fs::read(path)?; let mut parser = Parser::new(); @@ -187,12 +186,14 @@ pub(crate) fn run_rules_on_file( for cq in &compiled { if cfg.scanner.min_severity > cq.meta.severity { + tracing::debug!("Skipping rule {} because it's below the minimum severity", cq.meta.id); continue; } let mut matches = cursor.matches(&cq.query, root, &*bytes); while let Some(m) = matches.next() { for cap in m.captures.iter().filter(|c| c.index == 0) { let point = cap.node.start_position(); + tracing::debug!("Found match for rule {}", cq.meta.id); out.push(Diag { path: path.to_string_lossy().to_string(), line: point.row + 1, diff --git a/src/walk.rs b/src/walk.rs index b0b9b737..b0600029 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -72,38 +72,13 @@ pub fn spawn_senders( .threads(worker_thrs) .overrides(overrides) .build_parallel(); - - - /* - walker.run(move || { - let tx = tx.clone(); - let mut batch = Vec::::with_capacity(256); - - Box::new(move |entry| { - tracing::debug!("walking: {:?}", entry); - - let mut b = Batcher { tx: tx.clone(), batch: Vec::with_capacity(BATCH_SIZE) }; - match entry { - Ok(e) if e.file_type().is_some_and(|ft| ft.is_file()) => { - b.push(e.into_path()); - tracing::debug!("scanning file: {:?}", b); - if batch.len() == BATCH_SIZE { - let _ = tx.send(std::mem::take(&mut batch)); - } - } - Err(err) => tracing::error!("walk error: {err}"), - _ => {} - } - WalkState::Continue - }) - }); - */ + walker.run(move || { let mut batcher = Batcher { tx: tx.clone(), batch: Vec::with_capacity(BATCH_SIZE), }; - + Box::new(move |entry| { tracing::debug!("walking: {:?}", entry); let e = match entry {