swc-project/swc · error · anyhow::Error
Syntax Error
Error message
Syntax Error
What it means
Generic terminal error from swc_compiler_base's parse machinery: the source failed to parse, or recoverable parser errors were emitted, so the parsed program cannot be trusted and is not returned. The real information is in the diagnostics emitted to the handler just before this error is raised; the message itself is only a marker. If SWC_DEBUG=1 is set, the error is additionally annotated with the parser config used.
Source
Thrown at crates/swc_compiler_base/src/lib.rs:113
IsModule::CommonJS => {
parse_file_as_commonjs(&fm, syntax, target, comments, &mut errors)
.map(Program::Script)
}
IsModule::Unknown => parse_file_as_program(&fm, syntax, target, comments, &mut errors),
};
for e in errors {
e.into_diagnostic(handler).emit();
error = true;
}
let program = program_result.map_err(|e| {
e.into_diagnostic(handler).emit();
Error::msg("Syntax Error")
})?;
if error {
return Err(anyhow::anyhow!("Syntax Error"));
}
Ok(program)
})();
if env::var("SWC_DEBUG").unwrap_or_default() == "1" {
res = res.with_context(|| format!("Parser config: {syntax:?}"));
}
res
}
pub struct PrintArgs<'a> {
pub source_root: Option<&'a str>,
pub source_file_name: Option<&'a str>,
pub output_path: Option<PathBuf>,
pub inline_sources_content: bool,
pub source_map: SourceMapsConfig,View on GitHub (pinned to d7d7434666)
Solutions
- Read the diagnostics printed above the error - they carry file, line, and column of the actual syntax problem; fix the input at that location
- Align jsc.parser.syntax with the file type (typescript + tsx flags for .tsx, decorators flag for decorators)
- Set SWC_DEBUG=1 to get 'Parser config: ...' appended and confirm the syntax actually in use
- If input may be non-JS, gate it before calling swc (extension checks) so it never reaches the parser
Example fix
// .swcrc before (tsx file parsed with plain es syntax)
"jsc": { "parser": { "syntax": "ecmascript" }, ... }
// .swcrc after
"jsc": { "parser": { "syntax": "typescript", "tsx": true } } Defensive patterns
Strategy: try-catch
Try / catch
// Capture the emitted diagnostics alongside the error
use swc_common::errors::{Handler, HandlerFlags};
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(source_map.clone()));
let result = swc_compiler_base::parse_js(..., &handler, syntax, target, is_module, comments);
if let Err(e) = &result {
// diagnostics were already emitted to `handler`; fail with both
return Err(anyhow::anyhow!("parse failed: {e}"));
} Prevention
- Drive jsc.parser.syntax from the file extension (.ts/.tsx -> typescript)
- Pre-validate third-party input with a cheap lint pass before handing it to swc
- Keep SWC_DEBUG=1 enabled in dev builds to get the parser config in the error chain
When it happens
Trigger: Calling parse/transform APIs in swc_compiler_base with source text that is invalid under the configured Syntax (e.g. TypeScript decorators or JSX while Syntax is set to plain Es), actual syntax errors in input, or a parser config/es version mismatch with the file contents.
Common situations: Wrong jsc.parser.syntax in .swcrc (tsx code parsed as es), decorator proposals not enabled, using swc to parse non-JS files (JSON/HTML fed as source), or input truncated mid-token.
Related errors
- derive(Merge) does not support an enum
- Failed to parse the input
- Syntax Error
- Failed to resolve plugin path: {resolved_path:?}
- {err:?}
AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16).
Data as JSON: /api/errors/8bd16c31c8508883.
Report an issue: GitHub.