Unverified Commit 44afc376 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Refine error messages (#40661)

As per @sebmarkbage's suggestion, this PR refines the error message to
be more readable and friendly:

```
error - ./components/qux.js

You're importing a component that needs useEffect. It only works in a Client Component but none of its parents are marked with "client", so they're Server Components by default.

   ,----
 5 | import { useEffect } from 'react'
   :          ^^^^^^^^^
   `----

Maybe one of these should be marked as a "client" entry:

./components/qux.js
./components/baz.js
./components/bar.js
./app/dashboard/index/page.js
```

It's more ideal to put error codes inside the SWC transform and format
it in the Next.js logging layer. A future improvement is to tweak the
message a bit more for these specific cases:

- [ ] The error already happens inside an entry point (page or layout),
so itself should have "client" added (or removed), not it parents.
- [ ] When importing `"server-only"` inside a client component, we can
directly hint to the user which module in the import chain is marked
with `"client"`.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 6df87707
...@@ -227,11 +227,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -227,11 +227,7 @@ impl<C: Comments> ReactServerComponents<C> {
handler handler
.struct_span_err( .struct_span_err(
import.source.1, import.source.1,
format!( format!("NEXT_RSC_ERR_SERVER_IMPORT: {}", source).as_str(),
"Disallowed import of `{}` in the Server Components compilation.",
source
)
.as_str(),
) )
.emit() .emit()
}) })
...@@ -243,12 +239,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -243,12 +239,7 @@ impl<C: Comments> ReactServerComponents<C> {
handler handler
.struct_span_err( .struct_span_err(
specifier.1, specifier.1,
format!( format!("NEXT_RSC_ERR_REACT_API: {}", &specifier.0).as_str(),
"Disallowed React API `{}` in the Server Components \
compilation.",
&specifier.0
)
.as_str(),
) )
.emit() .emit()
}) })
...@@ -262,12 +253,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -262,12 +253,7 @@ impl<C: Comments> ReactServerComponents<C> {
handler handler
.struct_span_err( .struct_span_err(
specifier.1, specifier.1,
format!( format!("NEXT_RSC_ERR_REACT_API: {}", &specifier.0).as_str(),
"Disallowed ReactDOM API `{}` in the Server Components \
compilation.",
&specifier.0
)
.as_str(),
) )
.emit() .emit()
}) })
...@@ -285,11 +271,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -285,11 +271,7 @@ impl<C: Comments> ReactServerComponents<C> {
handler handler
.struct_span_err( .struct_span_err(
import.source.1, import.source.1,
format!( format!("NEXT_RSC_ERR_CLIENT_IMPORT: {}", source).as_str(),
"Disallowed import of `{}` in the Client Components compilation.",
source
)
.as_str(),
) )
.emit() .emit()
}) })
......
x Disallowed import of `server-only` in the Client Components compilation. x NEXT_RSC_ERR_CLIENT_IMPORT: server-only
,-[input.js:9:1] ,-[input.js:9:1]
9 | import "server-only" 9 | import "server-only"
: ^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^
......
x Disallowed import of `client-only` in the Server Components compilation. x NEXT_RSC_ERR_SERVER_IMPORT: client-only
,-[input.js:9:1] ,-[input.js:9:1]
9 | import "client-only" 9 | import "client-only"
: ^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^
......
x Disallowed React API `useState` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useState
,-[input.js:1:1] ,-[input.js:1:1]
1 | import { useState } from 'react' 1 | import { useState } from 'react'
: ^^^^^^^^ : ^^^^^^^^
`---- `----
x Disallowed React API `createContext` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: createContext
,-[input.js:3:1] ,-[input.js:3:1]
3 | import { createContext } from 'react' 3 | import { createContext } from 'react'
: ^^^^^^^^^^^^^ : ^^^^^^^^^^^^^
`---- `----
x Disallowed React API `useEffect` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useEffect
,-[input.js:5:1] ,-[input.js:5:1]
5 | import { useEffect, useImperativeHandle } from 'react' 5 | import { useEffect, useImperativeHandle } from 'react'
: ^^^^^^^^^ : ^^^^^^^^^
`---- `----
x Disallowed React API `useImperativeHandle` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useImperativeHandle
,-[input.js:5:1] ,-[input.js:5:1]
5 | import { useEffect, useImperativeHandle } from 'react' 5 | import { useEffect, useImperativeHandle } from 'react'
: ^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^
`---- `----
x Disallowed React API `Component` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: Component
,-[input.js:8:5] ,-[input.js:8:5]
8 | Component, 8 | Component,
: ^^^^^^^^^ : ^^^^^^^^^
`---- `----
x Disallowed React API `createFactory` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: createFactory
,-[input.js:9:5] ,-[input.js:9:5]
9 | createFactory, 9 | createFactory,
: ^^^^^^^^^^^^^ : ^^^^^^^^^^^^^
`---- `----
x Disallowed React API `PureComponent` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: PureComponent
,-[input.js:10:5] ,-[input.js:10:5]
10 | PureComponent, 10 | PureComponent,
: ^^^^^^^^^^^^^ : ^^^^^^^^^^^^^
`---- `----
x Disallowed React API `useDeferredValue` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useDeferredValue
,-[input.js:11:3] ,-[input.js:11:3]
11 | useDeferredValue, 11 | useDeferredValue,
: ^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^
`---- `----
x Disallowed React API `useInsertionEffect` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useInsertionEffect
,-[input.js:12:5] ,-[input.js:12:5]
12 | useInsertionEffect, 12 | useInsertionEffect,
: ^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^
`---- `----
x Disallowed React API `useLayoutEffect` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useLayoutEffect
,-[input.js:13:5] ,-[input.js:13:5]
13 | useLayoutEffect, 13 | useLayoutEffect,
: ^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^
`---- `----
x Disallowed React API `useReducer` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useReducer
,-[input.js:14:5] ,-[input.js:14:5]
14 | useReducer, 14 | useReducer,
: ^^^^^^^^^^ : ^^^^^^^^^^
`---- `----
x Disallowed React API `useRef` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useRef
,-[input.js:15:5] ,-[input.js:15:5]
15 | useRef, 15 | useRef,
: ^^^^^^ : ^^^^^^
`---- `----
x Disallowed React API `useSyncExternalStore` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: useSyncExternalStore
,-[input.js:16:5] ,-[input.js:16:5]
16 | useSyncExternalStore 16 | useSyncExternalStore
: ^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^
......
x Disallowed ReactDOM API `findDOMNode` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: findDOMNode
,-[input.js:2:5] ,-[input.js:2:5]
2 | findDOMNode, 2 | findDOMNode,
: ^^^^^^^^^^^ : ^^^^^^^^^^^
`---- `----
x Disallowed ReactDOM API `flushSync` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: flushSync
,-[input.js:3:3] ,-[input.js:3:3]
3 | flushSync, 3 | flushSync,
: ^^^^^^^^^ : ^^^^^^^^^
`---- `----
x Disallowed ReactDOM API `unstable_batchedUpdates` in the Server Components compilation. x NEXT_RSC_ERR_REACT_API: unstable_batchedUpdates
,-[input.js:4:3] ,-[input.js:4:3]
4 | unstable_batchedUpdates, 4 | unstable_batchedUpdates,
: ^^^^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^^^^
......
x Disallowed import of `react-dom/server` in the Server Components compilation. x NEXT_RSC_ERR_SERVER_IMPORT: react-dom/server
,-[input.js:9:1] ,-[input.js:9:1]
9 | import "react-dom/server" 9 | import "react-dom/server"
: ^^^^^^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^^^^^^
`---- `----
x Disallowed import of `react-dom/client` in the Server Components compilation. x NEXT_RSC_ERR_SERVER_IMPORT: react-dom/client
,-[input.js:11:1] ,-[input.js:11:1]
11 | import "react-dom/client" 11 | import "react-dom/client"
: ^^^^^^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^^^^^^
......
...@@ -1474,24 +1474,22 @@ export default async function getBaseWebpackConfig( ...@@ -1474,24 +1474,22 @@ export default async function getBaseWebpackConfig(
} as any, } as any,
] ]
: []), : []),
...(hasServerComponents ...(hasServerComponents && (isNodeServer || isEdgeServer)
? isNodeServer || isEdgeServer ? [
? [ // RSC server compilation loaders
// RSC server compilation loaders {
{ test: codeCondition.test,
test: codeCondition.test, include: [
include: [ dir,
dir, // To let the internal client components passing through flight loader
// To let the internal client components passing through flight loader /next[\\/]dist/,
/next[\\/]dist/, ],
], issuerLayer: WEBPACK_LAYERS.server,
issuerLayer: WEBPACK_LAYERS.server, use: {
use: { loader: 'next-flight-loader',
loader: 'next-flight-loader',
},
}, },
] },
: [] ]
: []), : []),
...(hasServerComponents && isEdgeServer ...(hasServerComponents && isEdgeServer
? [ ? [
......
...@@ -37,7 +37,7 @@ function isLikelyASyntaxError(message) { ...@@ -37,7 +37,7 @@ function isLikelyASyntaxError(message) {
let hadMissingSassError = false let hadMissingSassError = false
// Cleans up webpack error messages. // Cleans up webpack error messages.
function formatMessage(message, verbose) { function formatMessage(message, verbose, importTraceNote) {
// TODO: Replace this once webpack 5 is stable // TODO: Replace this once webpack 5 is stable
if (typeof message === 'object' && message.message) { if (typeof message === 'object' && message.message) {
const filteredModuleTrace = const filteredModuleTrace =
...@@ -61,7 +61,7 @@ function formatMessage(message, verbose) { ...@@ -61,7 +61,7 @@ function formatMessage(message, verbose) {
body + body +
(message.details && verbose ? '\n' + message.details : '') + (message.details && verbose ? '\n' + message.details : '') +
(filteredModuleTrace && filteredModuleTrace.length && verbose (filteredModuleTrace && filteredModuleTrace.length && verbose
? '\n\nImport trace for requested module:' + ? (importTraceNote || '\n\nImport trace for requested module:') +
filteredModuleTrace.map((trace) => `\n${trace.moduleName}`).join('') filteredModuleTrace.map((trace) => `\n${trace.moduleName}`).join('')
: '') + : '') +
(message.stack && verbose ? '\n' + message.stack : '') (message.stack && verbose ? '\n' + message.stack : '')
...@@ -171,18 +171,44 @@ function formatMessage(message, verbose) { ...@@ -171,18 +171,44 @@ function formatMessage(message, verbose) {
function formatWebpackMessages(json, verbose) { function formatWebpackMessages(json, verbose) {
const formattedErrors = json.errors.map(function (message) { const formattedErrors = json.errors.map(function (message) {
let importTraceNote
// TODO: Shall we use invisible characters in the original error // TODO: Shall we use invisible characters in the original error
// message as meta information? // message as meta information?
if ( if (message && message.message && /NEXT_RSC_ERR_/.test(message.message)) {
message &&
message.message &&
/ (Client|Server) Components compilation\./.test(message.message)
) {
// Comes from the "React Server Components" transform in SWC, always // Comes from the "React Server Components" transform in SWC, always
// attach the module trace. // 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.message)) {
message.message = message.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`
)
importTraceNote =
'\n\nMaybe one of these should be marked as a "client" entry:\n'
} else if (NEXT_RSC_ERR_SERVER_IMPORT.test(message.message)) {
message.message = message.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`
)
importTraceNote =
'\n\nMaybe one of these should be marked as a "client" entry:\n'
} else if (NEXT_RSC_ERR_CLIENT_IMPORT.test(message.message)) {
message.message = message.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`
)
importTraceNote = '\n\nOne of these is marked as a "client" entry:\n'
}
verbose = true verbose = true
} }
return formatMessage(message, verbose) return formatMessage(message, verbose, importTraceNote)
}) })
const formattedWarnings = json.warnings.map(function (message) { const formattedWarnings = json.warnings.map(function (message) {
return formatMessage(message, verbose) return formatMessage(message, verbose)
......
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