Skip to content

Commit

Permalink
Fix sourcemaps for async Turbopack chunks (#72827)
Browse files Browse the repository at this point in the history
Works around nodejs/node#52102

Webpack will be harder to patch and I don't know yet where the Webpack runtime is using `vm.runInThisContext`.

Best way to observe change is in #71909 and `pnpm test-dev-turbo test/development/app-dir/dynamic-io-dev-errors/ -t "should display error when component accessed data without suspense boundary"`.
Before:
```
Error: [...]
    at Page [Server] (<anonymous>)
    at InnerLayoutRouter (.next/server/chunks/ssr/[root of the server]__088b8c._.js)
```

After:
```
Error: [...]
    at Page [Server] (<anonymous>)
    at InnerLayoutRouter (node_modules/.pnpm/file+..+next-repo-2186278d37ae48e2b1397aa86060054592669ed9909b91e96c5af049d12c04c3+packages+n_vk62popbath6ofshgazcbhzdoe/node_modules/next/dist/src/client/components/layout-router.tsx:324:2)
```

Next step is figuring out why the now sourcemapped frames aren't ignore-listed.
  • Loading branch information
eps1lon authored and wyattjoh committed Nov 28, 2024
1 parent 445e777 commit 6f189e5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import stripAnsi from 'strip-ansi'
import { nextTestSetup } from 'e2e-utils'
import {
assertHasRedbox,
Expand All @@ -13,7 +14,7 @@ import { createSandbox } from 'development-sandbox'
import { outdent } from 'outdent'

describe('Dynamic IO Dev Errors', () => {
const { next } = nextTestSetup({
const { isTurbopack, next } = nextTestSetup({
files: __dirname,
})

Expand Down Expand Up @@ -51,6 +52,7 @@ describe('Dynamic IO Dev Errors', () => {

// NOTE: when update this snapshot, use `pnpm build` in packages/next to avoid next source code get mapped to source.
it('should display error when component accessed data without suspense boundary', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/no-accessed-data')

await retry(async () => {
Expand All @@ -59,6 +61,20 @@ describe('Dynamic IO Dev Errors', () => {
await assertHasRedbox(browser)
})

expect(stripAnsi(next.cliOutput.slice(outputIndex))).toContain(
`\nError: Route "/no-accessed-data": ` +
`A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. ` +
`We don't have the exact line number added to error messages yet but you can see which component in the stack below. ` +
`See more info: https://nextjs.org/docs/messages/next-prerender-missing-suspense` +
'\n at Page [Server] (<anonymous>)' +
// TODO(veil): Should be ignore-listed. Feel free to adjust the component name since it's Next.js internals.
'\n at InnerLayoutRouter (' +
(isTurbopack
? 'node_modules'
: // TODO(veil): Why is this not pointing to n_m in Webpack?
'../')
)

const description = await getRedboxDescription(browser)
const stack = await getRedboxCallStack(browser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type ModuleFactory = (

const url = require("url");
const fs = require("fs/promises");
const vm = require("vm");

const moduleFactories: ModuleFactories = Object.create(null);
const moduleCache: ModuleCache<ModuleWithDirection> = Object.create(null);
Expand Down Expand Up @@ -143,11 +142,13 @@ async function loadChunkAsync(
const module = {
exports: {},
};
vm.runInThisContext(
// TODO: Use vm.runInThisContext once our minimal supported Node.js version includes https://github.com/nodejs/node/pull/52153
// eslint-disable-next-line no-eval -- Can't use vm.runInThisContext due to https://github.com/nodejs/node/issues/52102
(0, eval)(
"(function(module, exports, require, __dirname, __filename) {" +
contents +
"\n})",
resolved
"\n})" +
"\n//# sourceURL=" + url.pathToFileURL(resolved),
)(module, module.exports, localRequire, path.dirname(resolved), resolved);

const chunkModules: ModuleFactories = module.exports;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6f189e5

Please sign in to comment.