Unverified Commit 261922df authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Show generated code from loaders in parse error messages (#89898)

## What?

When a webpack/turbopack loader produces broken code, error messages now
display **both** the original source and the generated code with source
map information, making it much easier to debug loader issues.

## Why?

Previously, when loaders returned invalid code, error messages only
showed the original source file (after source-map remapping). Users had
no way to see what the loader actually generated, making it hard to
diagnose why the code failed to parse. Showing both sides gives full
context about what went wrong.

## How?

### Turbopack Core (`turbopack-core`)

- **`Source::description()`** — New method on the `Source` trait
providing human-readable descriptions of where code comes from.
Implemented across all source types (`FileSource`, `VirtualSource`,
`WebpackLoadersProcessedAsset`, `PostCssTransformedAsset`, etc.),
producing chains like `"loaders [sass-loader] transform of file content
of ./styles.scss"`.
- **`AdditionalIssueSource`** — New struct to hold a labeled source
location. The `Issue` trait gains an `additional_sources()` method so
issues can expose supplementary code frames.
- **`GeneratedCodeSource`** — A wrapper that strips `GenerateSourceMap`
support from a source, ensuring the *generated* code is displayed as-is
rather than being remapped back to the original.
- **`IssueSource::to_generated_code_source()`** — Helper that detects
sources implementing `GenerateSourceMap` and wraps them in
`GeneratedCodeSource` for display. Used by `AnalyzeIssue` and
`ParsingIssue` to automatically attach generated code frames.

### Error Formatting

- **`turbopack-cli-utils`** — Renders additional sources in CLI issue
output.
- **`format-issue.ts`** — Renders additional sources in the browser
error overlay. Extracted `formatSourceCodeFrame()` helper to deduplicate
code-frame rendering between primary and additional sources.
- Long-line truncation (e.g. minified CSS from SCSS) is handled natively
by the Rust-based `codeFrameColumns` implementation.

### Type Definitions

- Added `SourcePosition`, `IssueSource`, and `AdditionalIssueSource`
interfaces to TypeScript types.
- Updated `PlainSource` (added `file_path`), `PlainIssue` (added
`additional_sources`), and NAPI bindings to pass the data through.

### Test Coverage

- **E2e tests** (`test/e2e/webpack-loader-parse-error/`) with custom
broken JS and CSS loaders, covering all 4 modes:
- **Development (Turbopack)** — Verifies parse errors show both original
and generated code via browser error overlay
- **Development (Webpack)** — Verifies error overlay shows the parse
error (webpack doesn't support additional sources)
- **Production (Turbopack)** — Verifies build failure output with full
error extraction and inline snapshots
- **Production (Webpack)** — Verifies build failure output with inline
snapshots
- Updated `test/development/sass-error/` snapshot to include the new
generated code frame for minified SCSS output.

### Example Output

When a loader produces broken code, users now see:
```
⨯ ./app/data.broken.js:3:1
Parsing ecmascript source code failed
  1 | // This file will be processed by broken-js-loader
  2 | // The loader will return invalid JavaScript with a source map
> 3 | export default function Data() {
    | ^
  4 |   return <div>original source content</div>
  5 | }
  6 |

Expected '</', got '{'

Generated code of loaders [./broken-js-loader.js] transform of file content of app/data.broken.js:
./app/data.broken.js:3:46
  1 | // Generated by broken-js-loader
  2 | export default function Page() {
> 3 |   return <div>this is intentionally broken {{{ invalid jsx
    |                                              ^
  4 | }
  5 |

Import trace:
  Server Component:
    ./app/data.broken.js
    ./app/page.js
```

---------
Co-authored-by: 's avatarClaude <noreply@anthropic.com>
Co-authored-by: 's avatarTobias Koppers <sokra@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <lukeisandberg@gmail.com>
parent 742b1866
use std::io::Write;
use anyhow::{Result, bail};
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileContent, rope::RopeBuilder};
use turbopack_core::{
asset::{Asset, AssetContent},
......@@ -47,6 +47,12 @@ impl Source for StructuredImageFileSource {
.with_modifier(modifier)
.rename_as(rcstr!("*.mjs"))
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let ident = self.image.ident().to_string().await?;
Ok(Vc::cell(format!("structured image of {}", ident).into()))
}
}
#[turbo_tasks::value_impl]
......
......@@ -155,22 +155,19 @@ fn is_internal(file_path: &str) -> bool {
RE.is_match(file_path)
}
/// Renders a code frame for the issue's source location, if available.
/// Renders a code frame for a source location, if available.
///
/// This avoids transferring the full source file content across the NAPI
/// boundary just to call back into Rust for code frame rendering.
///
/// Because this accesses the terminal size, this function call should not be cached (e.g. in
/// turbo-tasks).
fn render_issue_code_frame(issue: &PlainIssue) -> Result<Option<String>> {
let Some(source) = issue.source.as_ref() else {
return Ok(None);
};
fn render_source_code_frame(source: &PlainIssueSource, file_path: &str) -> Result<Option<String>> {
let Some((start, end)) = source.range else {
return Ok(None);
};
if is_internal(&issue.file_path) {
if is_internal(file_path) {
return Ok(None);
}
......@@ -210,6 +207,14 @@ fn render_issue_code_frame(issue: &PlainIssue) -> Result<Option<String>> {
)
}
/// Renders a code frame for the issue's primary source location.
fn render_issue_code_frame(issue: &PlainIssue) -> Result<Option<String>> {
let Some(source) = issue.source.as_ref() else {
return Ok(None);
};
render_source_code_frame(source, &issue.file_path)
}
#[napi(object)]
pub struct NapiIssue {
pub severity: String,
......@@ -219,6 +224,7 @@ pub struct NapiIssue {
pub description: Option<serde_json::Value>,
pub detail: Option<serde_json::Value>,
pub source: Option<NapiIssueSource>,
pub additional_sources: Vec<NapiAdditionalIssueSource>,
pub documentation_link: String,
pub import_traces: serde_json::Value,
/// Pre-rendered code frame for the issue's source location, if available.
......@@ -226,6 +232,14 @@ pub struct NapiIssue {
pub code_frame: Option<String>,
}
#[napi(object)]
pub struct NapiAdditionalIssueSource {
pub description: String,
pub source: NapiIssueSource,
/// Pre-rendered code frame for this additional source location, if available.
pub code_frame: Option<String>,
}
impl From<&PlainIssue> for NapiIssue {
fn from(issue: &PlainIssue) -> Self {
Self {
......@@ -242,6 +256,16 @@ impl From<&PlainIssue> for NapiIssue {
documentation_link: issue.documentation_link.to_string(),
severity: issue.severity.as_str().to_string(),
source: issue.source.as_ref().map(|source| source.into()),
additional_sources: issue
.additional_sources
.iter()
.map(|s| NapiAdditionalIssueSource {
description: s.description.to_string(),
code_frame: render_source_code_frame(&s.source, &s.source.asset.file_path)
.unwrap_or_default(),
source: (&s.source).into(),
})
.collect(),
title: serde_json::to_value(StyledStringSerialize::from(&issue.title)).unwrap(),
import_traces: serde_json::to_value(&issue.import_traces).unwrap(),
code_frame: render_issue_code_frame(issue).unwrap_or_default(),
......@@ -323,12 +347,14 @@ impl From<&(SourcePos, SourcePos)> for NapiIssueSourceRange {
#[napi(object)]
pub struct NapiSource {
pub ident: String,
pub file_path: String,
}
impl From<&PlainSource> for NapiSource {
fn from(source: &PlainSource) -> Self {
Self {
ident: source.ident.to_string(),
file_path: source.file_path.to_string(),
}
}
}
......
......@@ -519,6 +519,7 @@ export interface NapiIssue {
description?: any
detail?: any
source?: NapiIssueSource
additionalSources: Array<NapiAdditionalIssueSource>
documentationLink: string
importTraces: any
/**
......@@ -527,6 +528,12 @@ export interface NapiIssue {
*/
codeFrame?: string
}
export interface NapiAdditionalIssueSource {
description: string
source: NapiIssueSource
/** Pre-rendered code frame for this additional source location, if available. */
codeFrame?: string
}
export interface NapiIssueSource {
source: NapiSource
range?: NapiIssueSourceRange
......@@ -537,6 +544,7 @@ export interface NapiIssueSourceRange {
}
export interface NapiSource {
ident: string
filePath: string
}
export interface NapiSourcePos {
line: number
......
......@@ -112,6 +112,30 @@ export type StyledString =
value: StyledString[]
}
/** 0-indexed line and column position within a source file. */
export interface SourcePosition {
line: number
column: number
}
export interface IssueSource {
source: {
ident: string
filePath: string
}
range?: {
start: SourcePosition
end: SourcePosition
}
}
export interface AdditionalIssueSource {
description: string
source: IssueSource
/** Pre-rendered code frame from the Rust NAPI layer */
codeFrame?: string
}
export interface Issue {
severity: string
stage: string
......@@ -119,25 +143,8 @@ export interface Issue {
title: StyledString
description?: StyledString
detail?: StyledString
source?: {
source: {
ident: string
}
range?: {
start: {
// 0-indexed
line: number
// 0-indexed
column: number
}
end: {
// 0-indexed
line: number
// 0-indexed
column: number
}
}
}
source?: IssueSource
additionalSources?: AdditionalIssueSource[]
documentationLink: string
importTraces?: PlainTraceItem[][]
/** Pre-rendered code frame from the Rust NAPI layer */
......
......@@ -91,6 +91,13 @@ export function processIssues(
}
}
function formatFilePath(filePath: string): string {
return filePath
.replace('[project]/', './')
.replaceAll('/./', '/')
.replace('\\\\?\\', '')
}
export function formatIssue(issue: Issue) {
const { filePath, title, description, detail, source, importTraces } = issue
let { documentationLink } = issue
......@@ -107,10 +114,7 @@ export function formatIssue(issue: Issue) {
documentationLink = 'https://nextjs.org/docs/messages/module-not-found'
}
const formattedFilePath = filePath
.replace('[project]/', './')
.replaceAll('/./', '/')
.replace('\\\\?\\', '')
const formattedFilePath = formatFilePath(filePath)
let message = ''
......@@ -149,6 +153,19 @@ export function formatIssue(issue: Issue) {
message += renderStyledStringToErrorAnsi(detail) + '\n\n'
}
// Render additional sources (e.g., generated code from a loader)
for (const additional of issue.additionalSources ?? []) {
if (additional.codeFrame) {
const additionalFilePath = formatFilePath(
additional.source.source.filePath
)
const loc = additional.source.range
? `:${additional.source.range.start.line + 1}:${additional.source.range.start.column + 1}`
: ''
message += `${additional.description}:\n${additionalFilePath}${loc}\n${additional.codeFrame.trimEnd()}\n\n`
}
}
if (importTraces?.length) {
// This is the same logic as in turbopack/crates/turbopack-cli-utils/src/issue.rs
// We end up with multiple traces when the file with the error is reachable from multiple
......
......@@ -38,6 +38,11 @@ describe('app dir - css', () => {
Pseudo-elements like '::before' or '::after' can't be followed by selectors like 'Ident("path")'
Generated code of PostCSS transform of loaders [next/dist/build/webpack/loaders/resolve-url-loader/index, next/dist/compiled/sass-loader] transform of file content of app/global.scss:
./app/global.scss.css:1:884
> 1 | ...ate(-50%, 0px)}input.defaultCheckbox::before path{fill:currentColor}input:checked.defaul...
| ^
Import trace:
Client Component Browser:
./app/global.scss.css [Client Component Browser]
......
import './styles.broken.css'
export default function CssPage() {
return (
<div>
<p className="page">CSS Page</p>
</div>
)
}
// This file will be processed by broken-js-loader
// The loader will return invalid JavaScript with a source map
export default function Data() {
return <div>original source content</div>
}
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Broken from './data.broken.js'
export default function Page() {
return (
<div>
<p>Hello</p>
<Broken />
</div>
)
}
// A webpack loader that returns invalid CSS with a source map.
// The source map points back to the original file, making it clear that the
// loader is responsible for the broken output (not the original source).
module.exports = function (source) {
// Generate a source map that maps generated lines back to the original source.
// The original file is valid CSS; the generated output is intentionally broken.
//
// Mapping (0-based line numbers):
// generated line 0 ("/* Generated by... */") -> original line 0 (".page {")
// generated line 1 (".page {") -> original line 0 (".page {")
// generated line 2 (" color: red") -> original line 1 (" color: blue;")
// generated line 3 (" @@@ ...") -> original line 2 (" font-size: 16px;")
// generated line 4 (" background: {{{...") -> original line 1 (" color: blue;")
// generated line 5 ("}") -> original line 3 ("}")
const sourceMap = {
version: 3,
sources: [this.resourcePath],
sourcesContent: [source],
mappings: 'AAAA;AAAA;AACE;AACA;AADA;AAEF',
names: [],
}
// Return broken CSS that will fail to parse
const brokenCss = `/* Generated by broken-css-loader */
.page {
color: red
@@@ THIS IS NOT VALID CSS @@@;
background: {{{ invalid
}
`
this.callback(null, brokenCss, sourceMap)
}
// A webpack loader that returns invalid JavaScript with a source map.
// The source map points back to the original file, but the generated
// code is intentionally broken (unparseable).
module.exports = function (source) {
// Generate a source map that maps back to the original source
const sourceMap = {
version: 3,
sources: [this.resourcePath],
sourcesContent: [source],
// identity mapping: each generated line maps to the same original line
mappings: 'AAAA;AACA;AACA;AACA',
names: [],
}
// Return broken JavaScript that will fail to parse
const brokenCode = `// Generated by broken-js-loader
export default function Page() {
return <div>this is intentionally broken {{{ invalid jsx
}
`
this.callback(null, brokenCode, sourceMap)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
turbopack: {
rules: {
'*.broken.js': {
loaders: [
{
loader: require.resolve('./broken-js-loader.js'),
},
],
},
'*.broken.css': {
loaders: [
{
loader: require.resolve('./broken-css-loader.js'),
},
],
},
},
},
webpack: (config) => {
config.module.rules.push(
{
test: /\.broken\.js$/,
use: [require.resolve('./broken-js-loader.js')],
},
{
test: /\.broken\.css$/,
use: [require.resolve('./broken-css-loader.js')],
}
)
return config
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
/**
* Strips the dynamic test directory prefix from paths so snapshots are stable.
* Handles both absolute paths (/full/path/test/tmp/next-test-XXX/) and
* relative paths (./test/tmp/next-test-XXX/).
*/
function normalizePaths(output: string, testDir: string): string {
let result = output.replaceAll(testDir + '/', '')
const relMatch = testDir.match(/(test\/tmp\/next-test-[^/]+)/)
if (relMatch) {
result = result.replaceAll('./' + relMatch[1] + '/', './')
result = result.replaceAll(relMatch[1] + '/', '')
}
return result
}
/**
* Extracts a single error block from CLI output by searching for a title string.
*
* Start boundary: the nearest `⨯` marker (dev) or file-path line (prod)
* before the title.
* End boundary: the first blank line after an "Import trace" section that
* isn't followed by continuation content (indented lines or file paths).
*/
function extractErrorBlock(output: string, errorTitle: string): string {
const titleIdx = output.indexOf(errorTitle)
if (titleIdx === -1) return ''
const beforeTitle = output.substring(0, titleIdx)
// In dev mode, errors start with a `⨯` marker on the same line as the file path.
// In prod mode, errors start with a bare file-path line above the title.
const markerIdx = beforeTitle.lastIndexOf('⨯ ')
let startIdx: number
if (markerIdx !== -1) {
startIdx = markerIdx
} else {
// Go back two lines: title is preceded by a file-path line (e.g. ./file:3:1)
const lines = beforeTitle.split('\n')
startIdx =
lines.length >= 2
? beforeTitle.length -
lines[lines.length - 1].length -
lines[lines.length - 2].length -
1
: 0
}
// Scan forward to find the end of the Import trace section
const block = output.substring(startIdx)
const lines = block.split('\n')
let inImportTrace = false
for (let i = 0; i < lines.length; i++) {
if (/^Import trace/.test(lines[i])) {
inImportTrace = true
}
// A blank line after the import trace ends the block, unless the next
// line is a continuation (indented or a file path)
if (inImportTrace && lines[i].trim() === '' && i > 0) {
const nextLine = lines[i + 1]
if (
!nextLine ||
(!nextLine.startsWith(' ') && !nextLine.startsWith('./'))
) {
return lines.slice(0, i).join('\n')
}
}
}
return block.trimEnd()
}
describe('webpack-loader-parse-error (development)', () => {
const { next, isTurbopack, isNextDev } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: true,
})
if (!isNextDev) {
it('skipped in production mode', () => {})
return
}
beforeAll(() => next.start())
it('should show parse error for JS loader that returns broken code', async () => {
const outputIndex = next.cliOutput.length
// Fetch the page to trigger the error
await next.fetch('/')
// Check that the CLI output contains the parse error
// Turbopack: "Parsing ecmascript source code failed"
// Webpack: "Syntax Error"
await retry(async () => {
expect(next.cliOutput).toMatch(
/Parsing ecmascript source code failed|Syntax Error/
)
})
if (isTurbopack) {
const output = normalizePaths(
stripAnsi(next.cliOutput.slice(outputIndex)),
next.testDir
)
const errorBlock = extractErrorBlock(
output,
'Parsing ecmascript source code failed'
)
expect(errorBlock).toMatchInlineSnapshot(`
"⨯ ./app/data.broken.js:3:1
Expected '</', got '{'
1 | // This file will be processed by broken-js-loader
2 | // The loader will return invalid JavaScript with a source map
> 3 | export default function Data() {
| ^
4 | return <div>original source content</div>
5 | }
6 |
Parsing ecmascript source code failed
Generated code of loaders [broken-js-loader.js] transform of file content of app/data.broken.js:
./app/data.broken.js:3:46
1 | // Generated by broken-js-loader
2 | export default function Page() {
> 3 | return <div>this is intentionally broken {{{ invalid jsx
| ^
4 | }
5 |
Import trace:
Server Component:
./app/data.broken.js
./app/page.js"
`)
}
})
it('should show parse error for CSS loader that returns broken code', async () => {
const outputIndex = next.cliOutput.length
// Fetch the page to trigger the CSS error
await next.fetch('/css-page')
// Check that the CLI output contains the CSS parse error
// Turbopack: "Parsing CSS source code failed"
// Webpack: "Unknown word"
await retry(async () => {
expect(next.cliOutput).toMatch(
/Parsing CSS source code failed|Unknown word/
)
})
if (isTurbopack) {
const output = normalizePaths(
stripAnsi(next.cliOutput.slice(outputIndex)),
next.testDir
)
const errorBlock = extractErrorBlock(
output,
'Parsing CSS source code failed'
)
expect(errorBlock).toMatchInlineSnapshot(`
"⨯ ./app/css-page/styles.broken.css:2:3
Parsing CSS source code failed
1 | .page {
> 2 | color: blue;
| ^
3 | font-size: 16px;
4 | }
5 |
Unexpected token CurlyBracketBlock
Generated code of PostCSS transform of loaders [broken-css-loader.js] transform of file content of app/css-page/styles.broken.css:
./app/css-page/styles.broken.css:5:15
3 | color: red
4 | @@@ THIS IS NOT VALID CSS @@@;
> 5 | background: {{{ invalid
| ^
6 | }
7 |
Import trace:
Client Component Browser:
./app/css-page/styles.broken.css [Client Component Browser]
./app/css-page/page.js [Server Component]"
`)
}
})
})
describe('webpack-loader-parse-error (production)', () => {
const { next, isNextStart, isTurbopack } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: true,
})
if (!isNextStart) {
it('skipped in development mode', () => {})
return
}
it('should fail the build with parse errors from loaders', async () => {
await expect(next.start()).rejects.toThrow(
'next build failed with code/signal 1'
)
const output = normalizePaths(stripAnsi(next.cliOutput), next.testDir)
if (isTurbopack) {
const jsError = extractErrorBlock(output, "Expected '</', got '{'")
expect(jsError).toMatchInlineSnapshot(`
"./app/data.broken.js:3:1
Expected '</', got '{'
1 | // This file will be processed by broken-js-loader
2 | // The loader will return invalid JavaScript with a source map
> 3 | export default function Data() {
| ^
4 | return <div>original source content</div>
5 | }
6 |
Parsing ecmascript source code failed
Generated code of loaders [broken-js-loader.js] transform of file content of app/data.broken.js:
./app/data.broken.js:3:46
1 | // Generated by broken-js-loader
2 | export default function Page() {
> 3 | return <div>this is intentionally broken {{{ invalid jsx
| ^
4 | }
5 |
Import trace:
Server Component:
./app/data.broken.js
./app/page.js"
`)
const cssError = extractErrorBlock(
output,
'Parsing CSS source code failed'
)
expect(cssError).toMatchInlineSnapshot(`
"./app/css-page/styles.broken.css:5:15
Parsing CSS source code failed
3 | color: red
4 | @@@ THIS IS NOT VALID CSS @@@;
> 5 | background: {{{ invalid
| ^
6 | }
7 |
Unexpected token CurlyBracketBlock
Generated code of PostCSS transform of loaders [broken-css-loader.js] transform of file content of app/css-page/styles.broken.css:
./app/css-page/styles.broken.css:5:15
3 | color: red
4 | @@@ THIS IS NOT VALID CSS @@@;
> 5 | background: {{{ invalid
| ^
6 | }
7 |
Import trace:
Client Component Browser:
./app/css-page/styles.broken.css [Client Component Browser]
./app/css-page/page.js [Server Component]"
`)
} else {
// Webpack stops at the first error (JS), showing the generated code
const jsError = extractErrorBlock(output, "Expected '</', got '{'")
expect(jsError).toMatchInlineSnapshot(`
"./app/data.broken.js
Error: x Expected '</', got '{'
,-[3:1]
1 | // Generated by broken-js-loader
2 | export default function Page() {
3 | return <div>this is intentionally broken {{{ invalid jsx
: ^
4 | }
\`----
Caused by:
Syntax Error
Import trace for requested module:
./app/data.broken.js
./app/page.js"
`)
}
})
})
......@@ -93,6 +93,30 @@ pub fn format_issue(
writeln!(styled_issue, "{path}").unwrap();
}
}
// Render additional sources (e.g., generated code from a loader)
for additional in &plain_issue.additional_sources {
let desc = &additional.description;
let source = &additional.source;
match source.range {
Some((start, _)) => {
writeln!(
styled_issue,
"\n{}:\n{}:{}:{}",
desc,
source.asset.ident,
start.line + 1,
start.column + 1
)
.unwrap();
}
None => {
writeln!(styled_issue, "\n{}:\n{}", desc, source.asset.ident).unwrap();
}
}
format_source_content(source, &mut styled_issue);
}
let traces = &*plain_issue.import_traces;
if !traces.is_empty() {
/// Returns the leaf layer name, which is the first present layer name in the trace
......
......@@ -40,6 +40,18 @@ impl DataUriSource {
#[turbo_tasks::value_impl]
impl Source for DataUriSource {
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let data = self.data.await?;
// Include a short prefix of the raw data for identification; data URIs
// can be very long so we cap it at 50 characters.
let prefix: String = data.chars().take(50).collect();
let ellipsis = if data.len() > 50 { "..." } else { "" };
Ok(Vc::cell(
format!("data URI content ({prefix}{ellipsis})").into(),
))
}
#[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> {
let content_type = self.media_type.split(";").next().unwrap().into();
......
......@@ -56,6 +56,11 @@ impl Source for FileSource {
}
ident
}
#[turbo_tasks::function]
fn description(&self) -> Vc<RcStr> {
Vc::cell(format!("file content of {}", self.path).into())
}
}
#[turbo_tasks::value_impl]
......
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use crate::{
asset::{Asset, AssetContent},
ident::AssetIdent,
source::Source,
};
/// A source wrapping another source but stripping source map support.
/// Used to display generated code in error messages without triggering
/// source map remapping (since this type does NOT implement
/// `GenerateSourceMap`).
#[turbo_tasks::value]
pub struct GeneratedCodeSource {
source: ResolvedVc<Box<dyn Source>>,
}
#[turbo_tasks::value_impl]
impl GeneratedCodeSource {
#[turbo_tasks::function]
pub fn new(source: ResolvedVc<Box<dyn Source>>) -> Vc<Self> {
Self { source }.cell()
}
}
#[turbo_tasks::value_impl]
impl Source for GeneratedCodeSource {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
self.source.ident()
}
#[turbo_tasks::function]
fn description(&self) -> Vc<RcStr> {
self.source.description()
}
}
#[turbo_tasks::value_impl]
impl Asset for GeneratedCodeSource {
#[turbo_tasks::function]
fn content(&self) -> Vc<AssetContent> {
self.source.content()
}
}
......@@ -3,7 +3,10 @@ use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use super::{Issue, IssueSeverity, IssueSource, IssueStage, OptionStyledString, StyledString};
use super::{
AdditionalIssueSources, Issue, IssueSeverity, IssueSource, IssueStage, OptionStyledString,
StyledString,
};
use crate::{ident::AssetIdent, issue::OptionIssueSource};
#[turbo_tasks::value(shared)]
......@@ -79,4 +82,14 @@ impl Issue for AnalyzeIssue {
async fn source(&self) -> Vc<OptionIssueSource> {
Vc::cell(self.source)
}
#[turbo_tasks::function]
async fn additional_sources(&self) -> Result<Vc<AdditionalIssueSources>> {
if let Some(issue_source) = &self.source
&& let Some(source) = issue_source.to_generated_code_source().await?
{
return Ok(Vc::cell(vec![source]));
}
Ok(AdditionalIssueSources::empty())
}
}
......@@ -28,6 +28,7 @@ use turbo_tasks_hash::{DeterministicHash, Xxh3Hash64Hasher};
use crate::{
asset::{Asset, AssetContent},
condition::ContextCondition,
generated_code_source::GeneratedCodeSource,
ident::{AssetIdent, Layer},
source::Source,
source_map::{GenerateSourceMap, SourceMap, TokenWithSource},
......@@ -181,6 +182,15 @@ pub trait Issue {
fn source(self: Vc<Self>) -> Vc<OptionIssueSource> {
Vc::cell(None)
}
/// Additional source locations related to this issue (e.g., generated code
/// from a loader). Each source includes a description and location.
/// These are displayed alongside the primary source to give users full
/// context about the error.
#[turbo_tasks::function]
fn additional_sources(self: Vc<Self>) -> Vc<AdditionalIssueSources> {
AdditionalIssueSources::empty()
}
}
// A collectible trait that allows traces to be computed for a given module.
......@@ -620,6 +630,31 @@ impl IssueSource {
pub fn file_path(&self) -> Vc<FileSystemPath> {
self.source.ident().path()
}
/// If this source implements `GenerateSourceMap`, returns an
/// `AdditionalIssueSource` that wraps the source in a `GeneratedCodeSource`
/// (stripping source-map support) so the generated code is shown alongside
/// the original in error messages. Returns `None` otherwise.
pub async fn to_generated_code_source(&self) -> Result<Option<AdditionalIssueSource>> {
if ResolvedVc::try_sidecast::<Box<dyn GenerateSourceMap>>(self.source).is_some() {
let description = self.source.description().await?;
let generated = Vc::upcast::<Box<dyn Source>>(GeneratedCodeSource::new(*self.source))
.to_resolved()
.await?;
return Ok(Some(AdditionalIssueSource {
description: format!("Generated code of {}", description).into(),
source: IssueSource {
source: generated,
// The range is intentionally copied verbatim: the offsets
// are already in generated-source coordinates (they came
// from parsing the loader output), so no remapping is
// needed here.
range: self.range,
},
}));
}
Ok(None)
}
}
impl IssueSource {
......@@ -701,6 +736,26 @@ pub struct OptionIssueSource(Option<IssueSource>);
#[turbo_tasks::value(transparent)]
pub struct OptionStyledString(Option<ResolvedVc<StyledString>>);
/// A labeled issue source used to provide additional context in error messages.
/// For example, when a webpack loader produces broken code, the primary source
/// shows the original file, while an additional source shows the generated code.
#[turbo_tasks::value(shared)]
pub struct AdditionalIssueSource {
pub description: RcStr,
pub source: IssueSource,
}
#[turbo_tasks::value(shared, transparent)]
pub struct AdditionalIssueSources(Vec<AdditionalIssueSource>);
#[turbo_tasks::value_impl]
impl AdditionalIssueSources {
#[turbo_tasks::function]
pub fn empty() -> Vc<Self> {
Vc::cell(Vec::new())
}
}
// A structured reference to a file with module level details for displaying in an import trace
#[derive(
Serialize,
......@@ -880,9 +935,17 @@ pub struct PlainIssue {
pub documentation_link: RcStr,
pub source: Option<PlainIssueSource>,
pub additional_sources: Vec<PlainAdditionalIssueSource>,
pub import_traces: Vec<PlainTrace>,
}
#[turbo_tasks::value(serialization = "none")]
#[derive(Clone, Debug, PartialOrd, Ord)]
pub struct PlainAdditionalIssueSource {
pub description: RcStr,
pub source: PlainIssueSource,
}
fn hash_plain_issue(issue: &PlainIssue, hasher: &mut Xxh3Hash64Hasher, full: bool) {
hasher.write_ref(&issue.severity);
hasher.write_ref(&issue.file_path);
......@@ -901,6 +964,12 @@ fn hash_plain_issue(issue: &PlainIssue, hasher: &mut Xxh3Hash64Hasher, full: boo
hasher.write_value(0_u8);
}
// `additional_sources` is intentionally not hashed: it carries supplementary
// display info (e.g. generated code from a loader) that does not change the
// identity of the underlying problem. Two issues that differ only in their
// generated-code snippet still represent the same root cause and should be
// deduplicated.
if full {
hasher.write_ref(&issue.import_traces);
}
......@@ -958,6 +1027,17 @@ impl PlainIssue {
None
}
},
additional_sources: {
let sources = issue.additional_sources().await?;
let mut result = Vec::new();
for s in sources.iter() {
result.push(PlainAdditionalIssueSource {
description: s.description.clone(),
source: s.source.into_plain().await?,
});
}
result
},
import_traces: match import_tracer {
Some(tracer) => {
into_plain_trace(
......@@ -985,6 +1065,7 @@ pub struct PlainIssueSource {
#[derive(Clone, Debug, PartialOrd, Ord)]
pub struct PlainSource {
pub ident: ReadRef<RcStr>,
pub file_path: ReadRef<RcStr>,
#[turbo_tasks(debug_ignore)]
pub content: ReadRef<FileContent>,
}
......@@ -1001,6 +1082,7 @@ impl PlainSource {
Ok(PlainSource {
ident: asset.ident().to_string().await?,
file_path: asset.ident().path().to_string().await?,
content,
}
.cell())
......
......@@ -19,6 +19,7 @@ pub mod debug_id;
pub mod diagnostics;
pub mod environment;
pub mod file_source;
pub mod generated_code_source;
pub mod ident;
pub mod introspect;
pub mod issue;
......
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use crate::{asset::Asset, ident::AssetIdent};
......@@ -10,6 +11,14 @@ pub trait Source: Asset {
/// all properties of the [Source].
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent>;
/// A human-readable description of this source, explaining where the code
/// comes from. For sources that transform another source, this should
/// include the inner source's description, creating a readable chain
/// like `"loaders [sass-loader] transform of file content of
/// ./styles.scss"`.
#[turbo_tasks::function]
fn description(&self) -> Vc<RcStr>;
}
#[turbo_tasks::value(transparent)]
......
use anyhow::Result;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use crate::{
......@@ -40,6 +41,12 @@ impl Source for VirtualSource {
fn ident(&self) -> Vc<AssetIdent> {
*self.ident
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let ident = self.ident.to_string().await?;
Ok(Vc::cell(format!("virtual source {}", ident).into()))
}
}
#[turbo_tasks::value_impl]
......
......@@ -23,8 +23,8 @@ use turbopack_core::{
chunk::{ChunkingContext, MinifyType},
environment::Environment,
issue::{
Issue, IssueExt, IssueSource, IssueStage, OptionIssueSource, OptionStyledString,
StyledString,
AdditionalIssueSources, Issue, IssueExt, IssueSource, IssueStage, OptionIssueSource,
OptionStyledString, StyledString,
},
reference::ModuleReferences,
reference_type::ImportContext,
......@@ -746,6 +746,14 @@ impl Issue for ParsingIssue {
StyledString::Text(self.msg.clone()).resolved_cell(),
)))
}
#[turbo_tasks::function]
async fn additional_sources(&self) -> Result<Vc<AdditionalIssueSources>> {
if let Some(source) = self.source.to_generated_code_source().await? {
return Ok(Vc::cell(vec![source]));
}
Ok(AdditionalIssueSources::empty())
}
}
#[cfg(test)]
......
......@@ -34,6 +34,12 @@ impl Source for TextContentFileSource {
.with_modifier(rcstr!("text content"))
.rename_as(rcstr!("*.mjs"))
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let inner = self.source.description().await?;
Ok(Vc::cell(format!("text content of {}", inner).into()))
}
}
#[turbo_tasks::value_impl]
......
use std::io::Write;
use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_env::ProcessEnv;
use turbo_tasks_fs::{File, FileContent, FileSystemPath, rope::RopeBuilder};
......@@ -36,6 +37,11 @@ impl Source for ProcessEnvAsset {
fn ident(&self) -> Result<Vc<AssetIdent>> {
Ok(AssetIdent::from_path(self.root.join(".env.js")?))
}
#[turbo_tasks::function]
fn description(&self) -> Vc<RcStr> {
Vc::cell(rcstr!("process environment"))
}
}
#[turbo_tasks::value_impl]
......
......@@ -113,6 +113,12 @@ impl Source for MdxTransformedAsset {
fn ident(&self) -> Vc<AssetIdent> {
self.source.ident().rename_as(rcstr!("*.tsx"))
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let inner = self.source.description().await?;
Ok(Vc::cell(format!("MDX transform of {}", inner).into()))
}
}
#[turbo_tasks::value_impl]
......
......@@ -175,6 +175,12 @@ impl Source for PostCssTransformedAsset {
fn ident(&self) -> Vc<AssetIdent> {
self.source.ident()
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let inner = self.source.description().await?;
Ok(Vc::cell(format!("PostCSS transform of {}", inner).into()))
}
}
#[turbo_tasks::value_impl]
......@@ -284,6 +290,11 @@ impl JsonSource {
#[turbo_tasks::value_impl]
impl Source for JsonSource {
#[turbo_tasks::function]
fn description(&self) -> Vc<RcStr> {
Vc::cell(format!("JSON content of {}", self.path).into())
}
#[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> {
match &*self.key.await? {
......
......@@ -11,8 +11,8 @@ use serde_with::serde_as;
use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
Completion, OperationVc, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, Vc,
trace::TraceRawVcs, turbobail,
Completion, OperationVc, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString, Vc,
trace::TraceRawVcs,
};
use turbo_tasks_env::ProcessEnv;
use turbo_tasks_fs::{
......@@ -154,6 +154,21 @@ impl Source for WebpackLoadersProcessedAsset {
},
)
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let inner = self.source.description().await?;
let loaders = self.transform.await?.loaders.await?;
let loader_names: Vec<&str> = loaders.iter().map(|l| l.loader.as_str()).collect();
Ok(Vc::cell(
format!(
"loaders [{}] transform of {}",
loader_names.join(", "),
inner
)
.into(),
))
}
}
#[turbo_tasks::value_impl]
......@@ -262,9 +277,10 @@ impl WebpackLoadersProcessedAsset {
let resource_fs_path = self.source.ident().path().await?;
let Some(resource_path) = project_path.get_relative_path_to(&resource_fs_path) else {
turbobail!(
"Resource path \"{resource_fs_path}\" needs to be on project filesystem \
\"{project_path}\"",
bail!(
"Resource path \"{}\" needs to be on project filesystem \"{}\"",
resource_fs_path,
project_path
);
};
let config_value = evaluate_webpack_loader(WebpackLoaderContext {
......@@ -610,13 +626,18 @@ impl EvaluateContext for WebpackLoaderContext {
{
Ok(ResponseMessage::Resolve { path })
} else {
turbobail!(
"Resolving {request} in {lookup_path} ends up on a different \
filesystem"
bail!(
"Resolving {} in {} ends up on a different filesystem",
request.to_string().await?,
lookup_path.value_to_string().await?
);
}
} else {
turbobail!("Unable to resolve {request} in {lookup_path}");
bail!(
"Unable to resolve {} in {}",
request.to_string().await?,
lookup_path.value_to_string().await?
);
}
}
RequestMessage::TrackFileRead { file } => {
......
use anyhow::Result;
use bincode::{Decode, Encode};
use turbo_rcstr::RcStr;
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{File, FileContent};
use turbopack_core::{
......@@ -59,6 +60,14 @@ impl Source for WebAssemblySource {
.with_path(self.source.ident().path().await?.append("_.wasm")?),
})
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<RcStr>> {
let inner = self.source.description().await?;
Ok(Vc::cell(
format!("WebAssembly transform of {}", inner).into(),
))
}
}
#[turbo_tasks::value_impl]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment