Unverified Commit 77c69a71 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: improve error message for incompatible swc plugins (#86346)

Before:
```
Module not found: Can't resolve 'next/dist/esm/build/templates/helpers'
failed to analyze ecmascript module '[project]/node_modules/.pnpm/next@file+..+next-repo-2bf56e024dbccc85257a8c308492f2375d690b206153b02d3bea2029707d4c38+packa_tacnzof7rtmb5bwqa7bdnfcdbq/node_modules/next/dist/esm/build/templates/helpers.js [ssr] (ecmascript)'

Caused by:
- failed to parse [project]/node_modules/.pnpm/next@file+..+next-repo-2bf56e024dbccc85257a8c308492f2375d690b206153b02d3bea2029707d4c38+packa_tacnzof7rtmb5bwqa7bdnfcdbq/node_modules/next/dist/esm/build/templates/helpers.js
- Transforming and/or parsing of [project]/node_modules/.pnpm/next@file+..+next-repo-2bf56e024dbccc85257a8c308492f2375d690b206153b02d3bea2029707d4c38+packa_tacnzof7rtmb5bwqa7bdnfcdbq/node_modules/next/dist/esm/build/templates/helpers.js failed
- failed to deserialize `swc_common::plugin::diagnostics::PluginCorePkgDiagnostics`
- Mismatch { name: "array", found: 48 }

Debug info:
- Execution of <ModuleAssetContext as AssetContext>::process_resolve_result failed
- Execution of <ModuleAssetContext as AssetContext>::process failed
- Execution of <EcmascriptModuleAsset as Module>::side_effects failed
- Execution of analyze_ecmascript_module failed
- failed to analyze ecmascript module '[project]/node_modules/.pnpm/next@file+..+next-repo-2bf56e024dbccc85257a8c308492f2375d690b206153b02d3bea2029707d4c38+packa_tacnzof7rtmb5bwqa7bdnfcdbq/node_modules/next/dist/esm/build/templates/helpers.js [ssr] (ecmascript)'
- Execution of <EcmascriptModuleAsset as EcmascriptParsable>::failsafe_parse failed
- Execution of parse failed
- failed to parse [project]/node_modules/.pnpm/next@file+..+next-repo-2bf56e024dbccc85257a8c308492f2375d690b206153b02d3bea2029707d4c38+packa_tacnzof7rtmb5bwqa7bdnfcdbq/node_modules/next/dist/esm/build/templates/helpers.js
- Transforming and/or parsing of [project]/node_modules/.pnpm/next@file+..+next-repo-2bf56e024dbccc85257a8c308492f2375d690b206153b02d3bea2029707d4c38+packa_tacnzof7rtmb5bwqa7bdnfcdbq/node_modules/next/dist/esm/build/templates/helpers.js failed
- failed to deserialize `swc_common::plugin::diagnostics::PluginCorePkgDiagnostics`
- Mismatch { name: "array", found: 48 }
Import map: aliased to module 'next' with subpath '/dist/esm/build/templates/helpers' inside of [project]/


https://nextjs.org/docs/messages/module-not-found
```

After:
```
./packages/next/document.js
Failed to execute SWC plugin
An unexpected error occurred when executing an SWC EcmaScript transform plugin.
This might be due to a version mismatch between the plugin and Next.js.

Failed to execute @swc/plugin-react-remove-properties

Caused by:
    0: failed to deserialize `swc_common::plugin::diagnostics::PluginCorePkgDiagnostics`
    1: Mismatch { name: "array", found: 48 }
```
parent 6caeccb1
...@@ -9903,6 +9903,7 @@ dependencies = [ ...@@ -9903,6 +9903,7 @@ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
"bincode 2.0.1", "bincode 2.0.1",
"either",
"indexmap 2.9.0", "indexmap 2.9.0",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"serde", "serde",
......
...@@ -139,7 +139,8 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -139,7 +139,8 @@ pub async fn get_swc_ecma_transform_rule_impl(
}; };
Ok(Some(( Ok(Some((
SwcPluginModule::new(name, file.content().to_bytes().to_vec()).resolved_cell(), SwcPluginModule::new(name.clone(), file.content().to_bytes().to_vec())
.resolved_cell(),
config.clone(), config.clone(),
))) )))
} }
......
...@@ -17,6 +17,44 @@ describe('swcPlugins', () => { ...@@ -17,6 +17,44 @@ describe('swcPlugins', () => {
expect(html).not.toContain('data-custom-attribute') expect(html).not.toContain('data-custom-attribute')
}) })
}) })
;(isNextDev ? describe : describe.skip)('incompatible plugin version', () => {
const { next, skipped, isTurbopack } = nextTestSetup({
files: __dirname,
skipDeployment: true,
dependencies: {
'@swc/plugin-react-remove-properties': '7.0.2',
},
})
if (skipped) return
it('shows a redbox in dev', async () => {
const browser = await next.browser('/')
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"description": "Failed to execute SWC plugin",
"environmentLabel": null,
"label": "Build Error",
"source": "./app/layout.js
Failed to execute SWC plugin
An unexpected error occurred when executing an SWC EcmaScript transform plugin.
This might be due to a version mismatch between the plugin and Next.js. https://plugins.swc.rs/ can help you find the correct plugin version to use.
Failed to execute @swc/plugin-react-remove-properties
Caused by:
0: failed to deserialize \`swc_common::plugin::diagnostics::PluginCorePkgDiagnostics\`
1: Mismatch { name: "array", found: 48 }",
"stack": [],
}
`)
} else {
// TODO missing proper error with Webpack
await expect(browser).toDisplayRedbox(
`"Expected Redbox but found no visible one."`
)
}
})
})
;(isNextDev ? describe : describe.skip)('invalid plugin name', () => { ;(isNextDev ? describe : describe.skip)('invalid plugin name', () => {
const { next, skipped, isTurbopack } = nextTestSetup({ const { next, skipped, isTurbopack } = nextTestSetup({
files: __dirname, files: __dirname,
...@@ -32,6 +70,7 @@ module.exports = { ...@@ -32,6 +70,7 @@ module.exports = {
}) })
if (skipped) return if (skipped) return
// eslint-disable-next-line jest/no-identical-title
it('shows a redbox in dev', async () => { it('shows a redbox in dev', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
......
...@@ -26,6 +26,7 @@ workspace = true ...@@ -26,6 +26,7 @@ workspace = true
anyhow = { workspace = true } anyhow = { workspace = true }
async-trait = { workspace = true } async-trait = { workspace = true }
bincode = { workspace = true } bincode = { workspace = true }
either = { workspace = true }
indexmap = { workspace = true } indexmap = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
......
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