Unverified Commit 516bc832 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix SWC error wrongly formatted (#46412)

We currently use the `NEXT_RSC_ERR_INVALID_API` error code for metadata API conflicts which is wrong. The format should be `NEXT_RSC_ERR_INVALID_API: foo` and then the formatter transforms it into `foo isn't supported in app/`.

This PR adds a new `NEXT_RSC_ERR_CONFLICT_METADATA_EXPORT` error code and improves the message. Closes #46406.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/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`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) 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`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && 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 2988be2a
...@@ -484,11 +484,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -484,11 +484,7 @@ impl<C: Comments> ReactServerComponents<C> {
if has_gm_export && has_metadata_export { if has_gm_export && has_metadata_export {
HANDLER.with(|handler| { HANDLER.with(|handler| {
handler handler
.struct_span_err( .struct_span_err(span, "NEXT_RSC_ERR_CONFLICT_METADATA_EXPORT")
span,
"NEXT_RSC_ERR_INVALID_API: export generateMetadata and metadata \
together",
)
.emit() .emit()
}) })
} }
......
...@@ -18,6 +18,8 @@ function formatRSCErrorMessage( ...@@ -18,6 +18,8 @@ function formatRSCErrorMessage(
const NEXT_RSC_ERR_CLIENT_IMPORT = /.+NEXT_RSC_ERR_CLIENT_IMPORT: (.*?)\n/s const NEXT_RSC_ERR_CLIENT_IMPORT = /.+NEXT_RSC_ERR_CLIENT_IMPORT: (.*?)\n/s
const NEXT_RSC_ERR_CLIENT_METADATA_EXPORT = const NEXT_RSC_ERR_CLIENT_METADATA_EXPORT =
/.+NEXT_RSC_ERR_CLIENT_METADATA_EXPORT: (.*?)\n/s /.+NEXT_RSC_ERR_CLIENT_METADATA_EXPORT: (.*?)\n/s
const NEXT_RSC_ERR_CONFLICT_METADATA_EXPORT =
/NEXT_RSC_ERR_CONFLICT_METADATA_EXPORT/s
const NEXT_RSC_ERR_CLIENT_DIRECTIVE = /.+NEXT_RSC_ERR_CLIENT_DIRECTIVE\n/s const NEXT_RSC_ERR_CLIENT_DIRECTIVE = /.+NEXT_RSC_ERR_CLIENT_DIRECTIVE\n/s
const NEXT_RSC_ERR_CLIENT_DIRECTIVE_PAREN = const NEXT_RSC_ERR_CLIENT_DIRECTIVE_PAREN =
/.+NEXT_RSC_ERR_CLIENT_DIRECTIVE_PAREN\n/s /.+NEXT_RSC_ERR_CLIENT_DIRECTIVE_PAREN\n/s
...@@ -101,6 +103,13 @@ function formatRSCErrorMessage( ...@@ -101,6 +103,13 @@ function formatRSCErrorMessage(
`\n\nYou are attempting to export "$1" from a component marked with "use client", which is disallowed. Either remove the export, or the "use client" directive. Read more: https://beta.nextjs.org/docs/api-reference/metadata\n\n` `\n\nYou are attempting to export "$1" from a component marked with "use client", which is disallowed. Either remove the export, or the "use client" directive. Read more: https://beta.nextjs.org/docs/api-reference/metadata\n\n`
) )
formattedVerboseMessage = '\n\nFile path:\n'
} else if (NEXT_RSC_ERR_CONFLICT_METADATA_EXPORT.test(message)) {
formattedMessage = message.replace(
NEXT_RSC_ERR_CONFLICT_METADATA_EXPORT,
`\n\n"metadata" and "generateMetadata" cannot be exported at the same time, please keep one of them. Read more: https://beta.nextjs.org/docs/api-reference/metadata\n\n`
)
formattedVerboseMessage = '\n\nFile path:\n' formattedVerboseMessage = '\n\nFile path:\n'
} }
......
...@@ -93,7 +93,7 @@ createNextDescribe( ...@@ -93,7 +93,7 @@ createNextDescribe(
await session.patch(pageFile, uncomment) await session.patch(pageFile, uncomment)
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toInclude( expect(await session.getRedboxSource()).toInclude(
'export generateMetadata and metadata together' '"metadata" and "generateMetadata" cannot be exported at the same time, please keep one of them.'
) )
await cleanup() await cleanup()
......
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