-
Notifications
You must be signed in to change notification settings - Fork 27.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error messages of server compilation #41136
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/next/build/webpack/plugins/wellknown-errors-plugin/parseRSC.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import type { webpack } from 'next/dist/compiled/webpack/webpack' | ||
|
||
import { relative } from 'path' | ||
import { SimpleWebpackError } from './simpleWebpackError' | ||
|
||
export function formatRSCErrorMessage( | ||
message: string | ||
): null | [string, string] { | ||
if (message && /NEXT_RSC_ERR_/.test(message)) { | ||
let formattedMessage = message | ||
let formattedVerboseMessage = '' | ||
|
||
// Comes from the "React Server Components" transform in SWC, always | ||
// attach the module trace. | ||
const NEXT_RSC_ERR_REACT_API = /.+NEXT_RSC_ERR_REACT_API: (.*?)\n/s | ||
const NEXT_RSC_ERR_SERVER_IMPORT = /.+NEXT_RSC_ERR_SERVER_IMPORT: (.*?)\n/s | ||
const NEXT_RSC_ERR_CLIENT_IMPORT = /.+NEXT_RSC_ERR_CLIENT_IMPORT: (.*?)\n/s | ||
|
||
if (NEXT_RSC_ERR_REACT_API.test(message)) { | ||
formattedMessage = message.replace( | ||
NEXT_RSC_ERR_REACT_API, | ||
`\n\nYou're importing a component that needs $1. It only works in a Client Component but none of its parents are marked with "client", so they're Server Components by default.\n\n` | ||
) | ||
formattedVerboseMessage = | ||
'\n\nMaybe one of these should be marked as a "client" entry:\n' | ||
} else if (NEXT_RSC_ERR_SERVER_IMPORT.test(message)) { | ||
formattedMessage = message.replace( | ||
NEXT_RSC_ERR_SERVER_IMPORT, | ||
`\n\nYou're importing a component that imports $1. It only works in a Client Component but none of its parents are marked with "client", so they're Server Components by default.\n\n` | ||
) | ||
formattedVerboseMessage = | ||
'\n\nMaybe one of these should be marked as a "client" entry:\n' | ||
} else if (NEXT_RSC_ERR_CLIENT_IMPORT.test(message)) { | ||
formattedMessage = message.replace( | ||
NEXT_RSC_ERR_CLIENT_IMPORT, | ||
`\n\nYou're importing a component that needs $1. That only works in a Server Component but one of its parents is marked with "client", so it's a Client Component.\n\n` | ||
) | ||
formattedVerboseMessage = | ||
'\n\nOne of these is marked as a "client" entry:\n' | ||
} | ||
|
||
return [formattedMessage, formattedVerboseMessage] | ||
} | ||
|
||
return null | ||
} | ||
|
||
// Check if the error is specifically related to React Server Components. | ||
// If so, we'll format the error message to be more helpful. | ||
export function getRscError( | ||
fileName: string, | ||
err: Error, | ||
module: any, | ||
compilation: webpack.Compilation, | ||
compiler: webpack.Compiler | ||
): SimpleWebpackError | false { | ||
const formattedError = formatRSCErrorMessage(err.message) | ||
if (!formattedError) return false | ||
shuding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Get the module trace: | ||
// https://cs.github.com/webpack/webpack/blob/9fcaa243573005d6fdece9a3f8d89a0e8b399613/lib/stats/DefaultStatsFactoryPlugin.js#L414 | ||
const visitedModules = new Set() | ||
const moduleTrace = [] | ||
let current = module | ||
while (current) { | ||
if (visitedModules.has(current)) break | ||
visitedModules.add(current) | ||
moduleTrace.push(current) | ||
const origin = compilation.moduleGraph.getIssuer(current) | ||
if (!origin) break | ||
current = origin | ||
} | ||
|
||
const error = new SimpleWebpackError( | ||
fileName, | ||
formattedError[0] + | ||
formattedError[1] + | ||
moduleTrace | ||
.map((m) => | ||
m.resource ? ' ' + relative(compiler.context, m.resource) : '' | ||
) | ||
.filter(Boolean) | ||
.join('\n') | ||
) | ||
|
||
// Delete the stack because it's created here. | ||
error.stack = '' | ||
|
||
return error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add
error
docs (nextjs.org/docs/messages) for these so that we can show examples of this better?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
err.sh
support custom query parameters (like React's error) to render, or 100% static? Because the$1
here is dynamic (there are many possible APIs that will cause an error here, and we might extend).I can add some static content there as well to explain it better (or maybe linking to the RSC docs?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh it doesn't need to be dynamic just meant something static like https://github.com/vercel/next.js/blob/canary/errors/no-head-element.md where we can add related docs links and such