swc-project/swc · error · anyhow::Error

pretty_diagnostics

Error message

pretty_diagnostics

What it means

This is not a fixed message: TWithDiagnosticArray<anyhow::Error>::to_pretty_error converts collected diagnostics into a single anyhow::Error whose message is the pretty-printed diagnostic text (file/line/snippet). It is the standard way SWC collapses multiple transform diagnostics into one error; an inner error, if present, becomes the anyhow context chain.

Source

Thrown at crates/swc_error_reporters/src/lib.rs:120

        let report_handler = to_pretty_handler(color);

        self.diagnostics
            .iter()
            .map(|d| d.to_pretty_string(&self.cm, skip_filename, &report_handler))
            .collect::<String>()
    }
}

impl TWithDiagnosticArray<anyhow::Error> {
    /// Converts the diagnostics to a pretty error string and wraps it in an
    /// anyhow::Error.
    pub fn to_pretty_error(self) -> anyhow::Error {
        let pretty_diagnostics = self.take_pretty_diagnostics();

        match self.inner {
            Some(inner_error) => inner_error.context(pretty_diagnostics),
            None => {
                anyhow!(pretty_diagnostics)
            }
        }
    }

    /// Converts the diagnostics to a pretty string representation.
    pub fn to_pretty_string(self) -> String {
        let pretty_error = self.to_pretty_error();

        pretty_error.to_string()
    }
}

View on GitHub (pinned to d7d7434666)

Solutions

  1. Read the pretty diagnostic body - it names the file, line, and the actual problem; fix the source or config it points at
  2. If an inner error exists, print with {:#} (alternate formatting) to also see the context chain beneath the diagnostics
  3. Do not string-match the whole message; key on structured diagnostics before conversion if you need programmatic handling
Defensive patterns

Strategy: try-catch

Try / catch

// Rust: this wrapper produces the pretty text as the error message
let out = transform(...);
let result: Result<Module, _> = out.map_err(|t| t.to_pretty_error());
if let Err(pretty) = &result {
    // `pretty`'s Display IS the formatted diagnostics; log it whole
    tracing::error!("transform failed:\n{pretty}");
}

Prevention

When it happens

Trigger: A transform pipeline step returned TWithDiagnosticArray with diagnostics attached (or an inner error plus diagnostics) and the caller invoked to_pretty_error()/to_pretty_string() - typically at the end of swc core transform or in @swc/core error handling.

Common situations: Any failing transform (type errors reported by a checker pass, plugin diagnostics, minifier errors) surfacing through this wrapper; users see the full multi-line diagnostic block as the error message.

Related errors


AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16). Data as JSON: /api/errors/4b46ab4d84d401fb. Report an issue: GitHub.