ast-grep/ast-grep · error

DiagnosticError

DiagnosticError

Error message

DiagnosticError(error_count)

What it means

The `scan` command collects diagnostics (parse errors of scanned files, rule compile errors, etc.) in an atomic counter while printing matches. After scanning completes, if any diagnostics were recorded, scan exits with DiagnosticError(error_count) instead of success, so CI pipelines fail when input files could not be fully analyzed.

Source

Thrown at crates/cli/src/scan.rs:197

      max_item_counter,
    })
  }
}
impl Worker for ScanWithConfig {
  fn consume_items<P: Printer>(
    &self,
    items: Items<P::Processed>,
    mut printer: P,
  ) -> Result<ExitCode> {
    printer.before_print()?;
    for item in items {
      printer.process(item)?;
    }
    printer.after_print()?;
    self.trace.print()?;
    let error_count = self.error_count.load(Ordering::Acquire);
    if error_count > 0 {
      Err(anyhow::anyhow!(EC::DiagnosticError(error_count)))
    } else {
      Ok(ExitCode::SUCCESS)
    }
  }
}

// we should only suggest unused suppression if scan includes all rules
// otherwise, keep silent about unused suppressions because they may used by other rules
// this is a "smart" heuristic but user always can override it
fn default_unused_suppression_rule_severity(arg: &ScanArg) -> Severity {
  if arg.include_all_rules() {
    Severity::Hint
  } else {
    Severity::Off
  }
}

fn no_suppress_all_rule_config(overwrite: &RuleOverwrite) -> RuleConfig<SgLang> {

View on GitHub (pinned to fc2b1530db)

Solutions

  1. Exclude the problematic files/dirs via .gitignore or the `sgconfig.yml` globs so they are not scanned
  2. Fix or skip rules that emit diagnostics on valid input files
  3. Check the printed diagnostics to see which files errored and repair or remove them
  4. If diagnostics are expected, run scan with output inspected rather than fail-on-error gating
Defensive patterns

Strategy: try-catch

Try / catch

const code = sh('sg scan .', { nothrow: true }); if (code.status !== 0 && /DiagnosticError/.test(code.stderr)) { console.warn('scan had diagnostics:', code.stderr); }

Prevention

When it happens

Trigger: Running `sg scan` where any processed file produced a diagnostic — e.g. a scanned file failed to parse under its inferred language, or a rule config emitted a diagnostic — and the worker's consume_items sees error_count > 0 after printing.

Common situations: Scanning directories that contain intentionally-invalid code (minified files, fixtures, snippets in docs); a rule whose transformations error on some files; committing fixture directories without ignore rules.

Related errors


AI-assisted analysis of ast-grep/ast-grep@fc2b1530db (2026-09-05). Data as JSON: /api/errors/59a6849b98382faf. Report an issue: GitHub.