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