Unverified Commit d12693e7 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Handle top level errors coming from Turbopack entrypoints subscription (#62528)

## What?

Adds handling for issues that are emitted during the entrypoints
subscription, before the changes in this PR any issue (i.e. compiler
errors) that was emitted during the collection of which entrypoints
exist in the application were ignored.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2591
parent 7593039b
...@@ -839,19 +839,24 @@ async fn directory_tree_to_loader_tree( ...@@ -839,19 +839,24 @@ async fn directory_tree_to_loader_tree(
} else if *current_tree.has_only_catchall().await? { } else if *current_tree.has_only_catchall().await? {
tree.parallel_routes.insert("children".to_string(), subtree); tree.parallel_routes.insert("children".to_string(), subtree);
} else { } else {
// TODO: improve error message to have the full paths // TODO: Investigate if this is still needed. Emitting the
DirectoryTreeIssue { // error causes the test "should
app_dir, // gracefully handle when two page
message: StyledString::Text(format!( // segments match the `children`
"You cannot have two parallel pages that resolve to the same path. \ // parallel slot" to fail
Route {} has multiple matches in {}", // DirectoryTreeIssue {
for_app_path, app_page // app_dir,
)) // message: StyledString::Text(format!(
.cell(), // "You cannot have two parallel pages that resolve
severity: IssueSeverity::Error.cell(), // to the same path. \ Route {}
} // has multiple matches in {}",
.cell() // for_app_path, app_page
.emit(); // ))
// .cell(),
// severity: IssueSeverity::Error.cell(),
// }
// .cell()
// .emit();
} }
} else { } else {
tree.parallel_routes.insert("children".to_string(), subtree); tree.parallel_routes.insert("children".to_string(), subtree);
......
...@@ -664,6 +664,8 @@ async function getBuildId( ...@@ -664,6 +664,8 @@ async function getBuildId(
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid)) .traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid))
} }
const IS_TURBOPACK_BUILD = process.env.TURBOPACK && process.env.TURBOPACK_BUILD
export default async function build( export default async function build(
dir: string, dir: string,
reactProductionProfiling = false, reactProductionProfiling = false,
...@@ -1329,7 +1331,7 @@ export default async function build( ...@@ -1329,7 +1331,7 @@ export default async function build(
duration: number duration: number
buildTraceContext: undefined buildTraceContext: undefined
}> { }> {
if (!(process.env.TURBOPACK && process.env.TURBOPACK_BUILD)) { if (!IS_TURBOPACK_BUILD) {
throw new Error("next build doesn't support turbopack yet") throw new Error("next build doesn't support turbopack yet")
} }
...@@ -1411,6 +1413,23 @@ export default async function build( ...@@ -1411,6 +1413,23 @@ export default async function build(
const entrypoints = entrypointsResult.value const entrypoints = entrypointsResult.value
const topLevelErrors: {
message: string
}[] = []
for (const issue of entrypoints.issues) {
topLevelErrors.push({
message: formatIssue(issue),
})
}
if (topLevelErrors.length > 0) {
throw new Error(
`Turbopack build failed with ${
topLevelErrors.length
} issues:\n${topLevelErrors.map((e) => e.message).join('\n')}`
)
}
await handleEntrypoints({ await handleEntrypoints({
entrypoints, entrypoints,
currentEntrypoints, currentEntrypoints,
......
...@@ -59,6 +59,8 @@ import { ...@@ -59,6 +59,8 @@ import {
renderStyledStringToErrorAnsi, renderStyledStringToErrorAnsi,
type SendHmr, type SendHmr,
type StartBuilding, type StartBuilding,
processTopLevelIssues,
type TopLevelIssuesMap,
} from './turbopack-utils' } from './turbopack-utils'
import { import {
propagateServerField, propagateServerField,
...@@ -154,6 +156,7 @@ export async function createHotReloaderTurbopack( ...@@ -154,6 +156,7 @@ export async function createHotReloaderTurbopack(
app: new Map(), app: new Map(),
} }
const currentTopLevelIssues: TopLevelIssuesMap = new Map()
const currentEntryIssues: EntryIssuesMap = new Map() const currentEntryIssues: EntryIssuesMap = new Map()
const manifestLoader = new TurbopackManifestLoader({ buildId, distDir }) const manifestLoader = new TurbopackManifestLoader({ buildId, distDir })
...@@ -425,12 +428,14 @@ export async function createHotReloaderTurbopack( ...@@ -425,12 +428,14 @@ export async function createHotReloaderTurbopack(
) )
} }
processTopLevelIssues(currentTopLevelIssues, entrypoints)
await handleEntrypoints({ await handleEntrypoints({
entrypoints, entrypoints,
currentEntrypoints, currentEntrypoints,
currentEntryIssues: currentEntryIssues, currentEntryIssues,
manifestLoader, manifestLoader,
nextConfig: opts.nextConfig, nextConfig: opts.nextConfig,
rewrites: opts.fsChecker.rewrites, rewrites: opts.fsChecker.rewrites,
...@@ -654,18 +659,23 @@ export async function createHotReloaderTurbopack( ...@@ -654,18 +659,23 @@ export async function createHotReloaderTurbopack(
const appEntryKey = getEntryKey('app', 'server', page) const appEntryKey = getEntryKey('app', 'server', page)
const pagesEntryKey = getEntryKey('pages', 'server', page) const pagesEntryKey = getEntryKey('pages', 'server', page)
const topLevelIssues = currentTopLevelIssues.values()
const thisEntryIssues = const thisEntryIssues =
currentEntryIssues.get(appEntryKey) ?? currentEntryIssues.get(appEntryKey) ??
currentEntryIssues.get(pagesEntryKey) currentEntryIssues.get(pagesEntryKey)
if (thisEntryIssues !== undefined && thisEntryIssues.size > 0) { if (thisEntryIssues !== undefined && thisEntryIssues.size > 0) {
// If there is an error related to the requesting page we display it instead of the first error // If there is an error related to the requesting page we display it instead of the first error
return [...thisEntryIssues.values()].map( return [...topLevelIssues, ...thisEntryIssues.values()].map(
(issue) => new Error(formatIssue(issue)) (issue) => new Error(formatIssue(issue))
) )
} }
// Otherwise, return all errors across pages // Otherwise, return all errors across pages
const errors = [] const errors = []
for (const issue of topLevelIssues) {
errors.push(new Error(formatIssue(issue)))
}
for (const entryIssues of currentEntryIssues.values()) { for (const entryIssues of currentEntryIssues.values()) {
for (const issue of entryIssues.values()) { for (const issue of entryIssues.values()) {
errors.push(new Error(formatIssue(issue))) errors.push(new Error(formatIssue(issue)))
...@@ -768,7 +778,7 @@ export async function createHotReloaderTurbopack( ...@@ -768,7 +778,7 @@ export async function createHotReloaderTurbopack(
page, page,
pathname, pathname,
route, route,
currentEntryIssues: currentEntryIssues, currentEntryIssues,
entrypoints: currentEntrypoints, entrypoints: currentEntrypoints,
manifestLoader, manifestLoader,
readyIds, readyIds,
......
...@@ -118,8 +118,9 @@ export function formatIssue(issue: Issue) { ...@@ -118,8 +118,9 @@ export function formatIssue(issue: Issue) {
} }
type IssueKey = `${Issue['severity']}-${Issue['filePath']}-${string}-${string}` type IssueKey = `${Issue['severity']}-${Issue['filePath']}-${string}-${string}`
type IssuesMap = Map<IssueKey, Issue> export type IssuesMap = Map<IssueKey, Issue>
export type EntryIssuesMap = Map<EntryKey, IssuesMap> export type EntryIssuesMap = Map<EntryKey, IssuesMap>
export type TopLevelIssuesMap = IssuesMap
function getIssueKey(issue: Issue): IssueKey { function getIssueKey(issue: Issue): IssueKey {
return `${issue.severity}-${issue.filePath}-${JSON.stringify( return `${issue.severity}-${issue.filePath}-${JSON.stringify(
...@@ -127,6 +128,18 @@ function getIssueKey(issue: Issue): IssueKey { ...@@ -127,6 +128,18 @@ function getIssueKey(issue: Issue): IssueKey {
)}-${JSON.stringify(issue.description)}` )}-${JSON.stringify(issue.description)}`
} }
export function processTopLevelIssues(
currentTopLevelIssues: TopLevelIssuesMap,
result: TurbopackResult
) {
currentTopLevelIssues.clear()
for (const issue of result.issues) {
const issueKey = getIssueKey(issue)
currentTopLevelIssues.set(issueKey, issue)
}
}
export function processIssues( export function processIssues(
currentEntryIssues: EntryIssuesMap, currentEntryIssues: EntryIssuesMap,
key: EntryKey, key: EntryKey,
......
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