| |
| |
| |
| |
| @@ -1,7 +1,9 @@ |
| import type { Socket } from 'net' |
| import { mkdir, writeFile } from 'fs/promises' |
| +import { realpathSync } from 'fs' |
| import * as inspector from 'inspector' |
| -import { join, extname, relative } from 'path' |
| +import { join, extname, relative, isAbsolute } from 'path' |
| +import { fileURLToPath, pathToFileURL } from 'url' |
| |
| import ws from 'next/dist/compiled/ws' |
| |
| @@ -84,6 +86,7 @@ import type { ModernSourceMapPayload } from '../lib/source-maps' |
| import { isDeferredEntry } from '../../build/entries' |
| import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route' |
| import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect' |
| +import { setBundlerFindSourceMapURLImplementation } from '../lib/source-maps' |
| import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware' |
| import { |
| formatIssue, |
| @@ -279,6 +282,47 @@ function getSourceMapFromTurbopack( |
| } |
| } |
| |
| +function getSourceMapURLFromTurbopack( |
| + distDir: string, |
| + scriptNameOrSourceURL: string |
| +): string | null { |
| + // React invokes this with the raw stack-frame filename, which arrives |
| + // either as an absolute filesystem path or as a `file:` URL. Anything else |
| + // (`file:` URLs with a query are eval'd server HMR modules carrying inline |
| + // source maps, `webpack-internal://`, `node:internal/...`, `<anonymous>`, |
| + // ...) is not something we have an emitted source map for. |
| + let scriptPath = scriptNameOrSourceURL |
| + if (scriptNameOrSourceURL.startsWith('file://')) { |
| + if (scriptNameOrSourceURL.includes('?')) { |
| + return null |
| + } |
| + try { |
| + scriptPath = fileURLToPath(scriptNameOrSourceURL) |
| + } catch { |
| + return null |
| + } |
| + } |
| + if (!isAbsolute(scriptPath)) { |
| + return null |
| + } |
| + |
| + // Only chunks emitted into `distDir` have an on-disk source map to point at. |
| + const relativePath = relative(distDir, scriptPath) |
| + if ( |
| + relativePath.startsWith('..') || |
| + // On Windows an absolute path on a different drive is returned unchanged |
| + // rather than as a `..`-prefixed relative path. |
| + isAbsolute(relativePath) |
| + ) { |
| + return null |
| + } |
| + |
| + // The emitted source map lives next to its chunk with a `.map` suffix (see |
| + // `SourceMapAsset::path`). Encode through `pathToFileURL` so any special |
| + // characters in the path are escaped into a well-formed `file:` URL. |
| + return pathToFileURL(scriptPath + '.map').href |
| +} |
| + |
| export async function createHotReloaderTurbopack( |
| opts: SetupOpts & { isSrcDir: boolean }, |
| serverFields: ServerFields, |
| @@ -410,6 +454,14 @@ export async function createHotReloaderTurbopack( |
| getSourceMapFromTurbopack.bind(null, project) |
| ) |
| |
| + let canonicalDistDir = distDir |
| + try { |
| + canonicalDistDir = realpathSync(distDir) |
| + } catch {} |
| + setBundlerFindSourceMapURLImplementation( |
| + getSourceMapURLFromTurbopack.bind(null, canonicalDistDir) |
| + ) |
| + |
| // Set up code frame renderer using native bindings |
| const { installCodeFrameSupport } = |
| require('../lib/install-code-frame') as typeof import('../lib/install-code-frame') |
| @@ -417,6 +469,7 @@ export async function createHotReloaderTurbopack( |
| |
| opts.onDevServerCleanup?.(async () => { |
| setBundlerFindSourceMapImplementation(() => undefined) |
| + setBundlerFindSourceMapURLImplementation(() => null) |
| await project.onExit() |
| await lockfile?.unlock() |
| }) |
| |
| |
| |
| |
| @@ -158,6 +158,36 @@ export function filterStackFrameDEV( |
| } |
| } |
| |
| +// `scriptNameOrSourceURL` is what React forwards from the stack frame: the |
| +// script's `getScriptNameOrSourceURL()`, which for the server chunks we can |
| +// map is an absolute filesystem path, not a URL. The returned value is the |
| +// source map's URL (`file:` or `data:`). |
| +type FindSourceMapURL = (scriptNameOrSourceURL: string) => string | null |
| +// Find the URL of a source map using the bundler's API. |
| +// Shared via `globalThis` because this module is compiled both into the server |
| +// runtime bundles (which call `findSourceMapURLDEV`) and into `next/dist/server` |
| +// (where the dev server registers the implementation), and each copy has its own |
| +// module state. |
| +const bundlerFindSourceMapURLSymbol = Symbol.for( |
| + 'next.server.bundlerFindSourceMapURL' |
| +) |
| + |
| +export function setBundlerFindSourceMapURLImplementation( |
| + findSourceMapURLImplementation: FindSourceMapURL |
| +): void { |
| + ;(globalThis as any)[bundlerFindSourceMapURLSymbol] = |
| + findSourceMapURLImplementation |
| +} |
| + |
| +function bundlerFindSourceMapURL(scriptNameOrSourceURL: string): string | null { |
| + const implementation: FindSourceMapURL | undefined = (globalThis as any)[ |
| + bundlerFindSourceMapURLSymbol |
| + ] |
| + return implementation === undefined |
| + ? null |
| + : implementation(scriptNameOrSourceURL) |
| +} |
| + |
| const invalidSourceMap = Symbol('invalid-source-map') |
| const sourceMapURLs = new LRUCache<string | typeof invalidSourceMap>( |
| 512 * 1024 * 1024, |
| @@ -172,6 +202,19 @@ const sourceMapURLs = new LRUCache<string | typeof invalidSourceMap>( |
| export function findSourceMapURLDEV( |
| scriptNameOrSourceURL: string |
| ): string | null { |
| + try { |
| + const bundlerSourceMapURL = bundlerFindSourceMapURL(scriptNameOrSourceURL) |
| + if (bundlerSourceMapURL !== null) { |
| + return bundlerSourceMapURL |
| + } |
| + } catch (cause) { |
| + console.error( |
| + `${scriptNameOrSourceURL}: Failed to find the source map URL. Cause: ${cause}` |
| + ) |
| + } |
| + |
| + // No bundler implementation (e.g. Webpack): inline the source map Node.js |
| + // knows as a `data:` URL. |
| let sourceMapURL = sourceMapURLs.get(scriptNameOrSourceURL) |
| if (sourceMapURL |
| let sourceMapPayload: ModernSourceMapPayload | undefined |
| |
| |
| |
| |
| @@ -181,7 +181,10 @@ function getSourcemappedFrameIfPossible( |
| let sourceMapConsumer: SyncSourceMapConsumer |
| let sourceMapPayload: ModernSourceMapPayload |
| if (sourceMapCacheEntry |
| - let sourceURL = frame.file |
| + // Fake frame scripts (`about://React/Server/file:///path/to/chunk.js?42`) |
| + // have their positions padded to match the underlying chunk, so they |
| + // resolve via the chunk's source map. |
| + let sourceURL = devirtualizeReactServerURL(frame.file) |
| // e.g. "/Users/foo/APP/.next/server/chunks/ssr/[root-of-the-server]__2934a0._.js" |
| // or "C:\Users\foo\APP\.next\server\chunks\ssr\[root-of-the-server]__2934a0._.js" |
| // will be keyed by Node.js as "file:///APP/.next/server/chunks/ssr/[root-of-the-server]__2934a0._.js". |
| @@ -189,8 +192,8 @@ function getSourcemappedFrameIfPossible( |
| // |
| // But frame.file might also be "webpack-internal:///(rsc)/./app/bad-sourcemap/page.js" or |
| // "<anonymous>" or "node:internal/process/task_queues" here |
| - if (path.isAbsolute(frame.file)) { |
| - sourceURL = url.pathToFileURL(frame.file).toString() |
| + if (path.isAbsolute(sourceURL)) { |
| + sourceURL = url.pathToFileURL(sourceURL).toString() |
| } |
| let maybeSourceMapPayload: ModernSourceMapPayload | undefined |
| try { |
| @@ -228,10 +231,9 @@ function getSourcemappedFrameIfPossible( |
| // is sufficient to compute relative paths but is actually wrong (the |
| // chunk and sourcemap have different content hashes). We are using the |
| // node API to read the sourcemap and it doesn't give us access to the |
| - // URI. Devirtualize `about://React/Server/file:///path/to/chunk.js?4` to |
| - // `file:///path/to/chunk.js` so that relative `sources` in the source map |
| - // resolve against the real chunk URL, not the virtual one. |
| - const sourceMapURL = devirtualizeReactServerURL(sourceURL) + '.map' |
| + // URI. `sourceURL` is already devirtualized so that relative `sources` |
| + // resolve against the real chunk URL, not React's virtual one. |
| + const sourceMapURL = sourceURL + '.map' |
| sourceMapConsumer = new SyncSourceMapConsumer( |
| sourceMapPayload, |
| // @ts-expect-error: our typings don't include this parameter but it is here. |
|
|