swc-project/swc · error

Failed to parse the input

Error message

Failed to parse the input

What it means

The ast-viewer wasm binding (bindings/binding_es_ast_viewer) parses user-submitted code with swc's parser (parse_program / parse_module / parse_commonjs depending on the is_esm/is_cjs flags). When the parser returns an Err, the handler first pretty-prints the real diagnostic (with file/line/column) and then this generic anyhow 'Failed to parse the input' is attached so the JS side receives a rejected promise. The useful information is the emitted diagnostic above this message, not the message itself.

Solutions

  1. Read the pretty-printed diagnostic that accompanies the error — it contains the exact line/column and reason; fix the code there
  2. Set the correct syntax flags for the input: enable is_ts for TypeScript, is_jsx for JSX, and pick esm vs cjs to match the module goal
  3. If the snippet is TS-only syntax, convert or strip type annotations before parsing as plain JS

Example fix

// before: TS code parsed as plain ESM
parse(code /* `const x: number = 1` */, { isTs: false, isEsm: true });

// after
parse(code, { isTs: true, isJsx: true, isEsm: true });
Defensive patterns

Strategy: validation

Validate before calling

// Choose flags from the file before calling the viewer's parse
function flagsFor(file, code) {
  return {
    isTs: /\.(ts|mts|cts|tsx)$/.test(file),
    isJsx: /\.tsx$/.test(file) || /<[A-Za-z][^>]*>/.test(code),
    isEsm: !/\.cjs$/.test(file) && /^\s*(import|export)\s/m.test(code),
    isCjs: /\.cjs$/.test(file),
  };
}

Try / catch

try {
  parse(code, flags);
} catch (e) {
  // the pretty diagnostic above 'Failed to parse the input' has line/column; surface both
  showDiagnostic(e.message);
}

Prevention

When it happens

Trigger: Calling the ast-viewer's parse function (or the ast-viewer website) with code that fails to parse under the chosen syntax flags: TypeScript syntax fed with is_ts=false, JSX fed with is_jsx=false, ESM syntax with is_cjs=true, or genuinely malformed code.

Common situations: Pasting TS/TSX into the viewer without the TS/JSX toggles enabled; a `target`/syntax flag combination that forbids decorators or early TS features; truncated code snippets cut off mid-token.

Understand the failure class

Related errors


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

Appendix: source

Thrown at bindings/binding_es_ast_viewer/src/lib.rs:97

        parser.parse_commonjs().map(Program::Script)
    } else {
        parser.parse_program()
    };

    let mut ast = try_with_handler(
        cm,
        HandlerOpts {
            color: ColorConfig::Never,
            ..Default::default()
        },
        |handler| {
            for err in parser.take_errors() {
                err.into_diagnostic(handler).emit();
            }

            program.map_err(|err| {
                err.into_diagnostic(handler).emit();
                anyhow::anyhow!("Failed to parse the input")
            })
        },
    )
    .map_err(|err| err.to_pretty_string())?;

    let tokens = parser.input_mut().iter_mut().take();

    GLOBALS.set(&Globals::default(), || {
        let unresolved_mark = Mark::new();
        let top_level_mark = Mark::new();
        ast.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, is_ts));

        Ok(vec![format!("{ast:#?}"), format!("{tokens:#?}")])
    })
}

#[wasm_bindgen]
pub fn version() -> String {

View on GitHub (pinned to 5176682b65)