swc-project/swc · error

it should not fail without emitting errors to handler

Error message

it should not fail without emitting errors to handler

What it means

Defensive expect in try_with_json_handler: HANDLER.set returned Err even though handler.has_errors() was false, violating the invariant that operations either succeed or report diagnostics to the handler. Reaching it means an error escaped without being emitted — an internal bug, not bad user input.

Source

Thrown at bindings/binding_nodejs_support_wasm/src/lib.rs:147

{
    let wr = JsonErrorWriter {
        errors: Default::default(),
        reporter: None,
        cm,
    };
    let emitter: Box<dyn Emitter> = Box::new(wr.clone());

    let handler = Handler::with_emitter(true, false, emitter);

    let ret = HANDLER.set(&handler, || op(&handler));

    if handler.has_errors() {
        let mut lock = wr.errors.lock().unwrap();
        let error = take(&mut *lock);

        Err(error)
    } else {
        Ok(ret.expect("it should not fail without emitting errors to handler"))
    }
}

impl Emitter for JsonErrorWriter {
    fn emit(&mut self, db: &mut DiagnosticBuilder) {
        let d = &**db;

        let snippet = d.span.primary_span().and_then(|span| {
            let mut snippet = String::new();
            let reporter = self.reporter.get_or_insert_with(json_reporter);
            match reporter.render_report(
                &mut snippet,
                &Snippet {
                    source_code: &to_pretty_source_code(&self.cm, true),
                    span,
                },
            ) {
                Ok(()) => Some(snippet),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Report as an swc internal bug with the input that triggered it
  2. Audit the wrapped op for error paths that return Err without emitting to the Handler
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at bindings/binding_nodejs_support_wasm/src/lib.rs:147 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/ef4c370475b8246a. Report an issue: GitHub.