Unverified Commit 5bdf9154 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix: next package resolving in dev overlay (#71632)

parent 4ee184b8
...@@ -14,7 +14,8 @@ import { ...@@ -14,7 +14,8 @@ import {
} from '../lib/constants' } from '../lib/constants'
import type { NextConfigComplete } from '../server/config-shared' import type { NextConfigComplete } from '../server/config-shared'
import { defaultOverrides } from '../server/require-hook' import { defaultOverrides } from '../server/require-hook'
import { NEXT_PROJECT_ROOT, hasExternalOtelApiPackage } from './webpack-config' import { hasExternalOtelApiPackage } from './webpack-config'
import { NEXT_PROJECT_ROOT } from './next-dir-paths'
import { WEBPACK_LAYERS } from '../lib/constants' import { WEBPACK_LAYERS } from '../lib/constants'
import { isWebpackServerOnlyLayer } from './utils' import { isWebpackServerOnlyLayer } from './utils'
......
import path from 'path'
export const NEXT_PROJECT_ROOT = path.join(__dirname, '..', '..')
export const NEXT_PROJECT_ROOT_DIST = path.join(NEXT_PROJECT_ROOT, 'dist')
export const NEXT_PROJECT_ROOT_DIST_CLIENT = path.join(
NEXT_PROJECT_ROOT_DIST,
'client'
)
...@@ -89,6 +89,10 @@ import { ...@@ -89,6 +89,10 @@ import {
getReactCompilerLoader, getReactCompilerLoader,
} from './get-babel-loader-config' } from './get-babel-loader-config'
import type { NextFlightLoaderOptions } from './webpack/loaders/next-flight-loader' import type { NextFlightLoaderOptions } from './webpack/loaders/next-flight-loader'
import {
NEXT_PROJECT_ROOT,
NEXT_PROJECT_ROOT_DIST_CLIENT,
} from './next-dir-paths'
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
type ClientEntries = { type ClientEntries = {
...@@ -101,13 +105,6 @@ const EXTERNAL_PACKAGES = ...@@ -101,13 +105,6 @@ const EXTERNAL_PACKAGES =
const DEFAULT_TRANSPILED_PACKAGES = const DEFAULT_TRANSPILED_PACKAGES =
require('../lib/default-transpiled-packages.json') as string[] require('../lib/default-transpiled-packages.json') as string[]
export const NEXT_PROJECT_ROOT = path.join(__dirname, '..', '..')
export const NEXT_PROJECT_ROOT_DIST = path.join(NEXT_PROJECT_ROOT, 'dist')
const NEXT_PROJECT_ROOT_DIST_CLIENT = path.join(
NEXT_PROJECT_ROOT_DIST,
'client'
)
if (parseInt(React.version) < 18) { if (parseInt(React.version) < 18) {
throw new Error('Next.js requires react >= 18.2.0 to be installed.') throw new Error('Next.js requires react >= 18.2.0 to be installed.')
} }
......
...@@ -24,10 +24,14 @@ export const CallStackFrame: React.FC<{ ...@@ -24,10 +24,14 @@ export const CallStackFrame: React.FC<{
: undefined : undefined
) )
// Format method to strip out the webpack layer prefix.
// e.g. (app-pages-browser)/./app/page.tsx -> ./app/page.tsx
const formattedMethod = f.methodName.replace(/^\([\w-]+\)\//, '')
return ( return (
<div data-nextjs-call-stack-frame> <div data-nextjs-call-stack-frame>
<h3 data-nextjs-frame-expanded={Boolean(frame.expanded)}> <h3 data-nextjs-frame-expanded={Boolean(frame.expanded)}>
<HotlinkedText text={f.methodName} /> <HotlinkedText text={formattedMethod} />
</h3> </h3>
<div <div
data-has-source={hasSource ? 'true' : undefined} data-has-source={hasSource ? 'true' : undefined}
......
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import type { OriginalStackFrameResponse } from '../../server/shared' import type { OriginalStackFrameResponse } from '../../server/shared'
import { isWebpackBundled, formatFrameSourceFile } from './webpack-module-path'
export interface OriginalStackFrame extends OriginalStackFrameResponse { export interface OriginalStackFrame extends OriginalStackFrameResponse {
error: boolean error: boolean
reason: string | null reason: string | null
...@@ -103,42 +103,6 @@ export function getOriginalStackFrames( ...@@ -103,42 +103,6 @@ export function getOriginalStackFrames(
) )
} }
const webpackRegExes = [
/^(rsc:\/\/React\/[^/]+\/)?webpack-internal:\/\/\/(\.)?(\((\w+)\))?/,
/^(webpack:\/\/\/(\.)?|webpack:\/\/(_N_E\/)?)(\((\w+)\))?/,
]
const replacementRegExes = [
/^(rsc:\/\/React\/[^/]+\/)/,
/^webpack-internal:\/\/\/(\.)?(\((\w+)\))?/,
/^(webpack:\/\/\/(\.)?|webpack:\/\/(_N_E\/)?)(\((\w+)\))?/,
/\?\d+$/, // React's fakeFunctionIdx query param
]
function isWebpackBundled(file: string) {
return webpackRegExes.some((regEx) => regEx.test(file))
}
/**
* Format the webpack internal id to original file path
* webpack-internal:///./src/hello.tsx => ./src/hello.tsx
* rsc://React/Server/webpack-internal:///(rsc)/./src/hello.tsx?42 => ./src/hello.tsx
* webpack://_N_E/./src/hello.tsx => ./src/hello.tsx
* webpack://./src/hello.tsx => ./src/hello.tsx
* webpack:///./src/hello.tsx => ./src/hello.tsx
*
* <anonymous> => ''
*/
function formatFrameSourceFile(file: string) {
if (file === '<anonymous>') return ''
for (const regex of replacementRegExes) {
file = file.replace(regex, '')
}
return file
}
export function getFrameSource(frame: StackFrame): string { export function getFrameSource(frame: StackFrame): string {
if (!frame.file) return '' if (!frame.file) return ''
......
import { formatFrameSourceFile, isWebpackBundled } from './webpack-module-path'
describe('webpack-module-path', () => {
describe('isWebpackBundled', () => {
it('should return true for webpack-internal paths', () => {
expect(isWebpackBundled('webpack-internal:///./src/hello.tsx')).toBe(true)
expect(
isWebpackBundled(
'rsc://React/Server/webpack-internal:///(rsc)/./src/hello.tsx?42'
)
).toBe(true)
expect(
isWebpackBundled(
'rsc://React/Server/webpack:///(rsc)/./src/hello.tsx?42'
)
).toBe(true)
expect(
isWebpackBundled(
'rsc://React/Server/webpack:///(app-pages-browser)/./src/hello.tsx?42'
)
).toBe(true)
expect(isWebpackBundled('webpack://_N_E/./src/hello.tsx')).toBe(true)
expect(isWebpackBundled('webpack://./src/hello.tsx')).toBe(true)
expect(isWebpackBundled('webpack:///./src/hello.tsx')).toBe(true)
})
it('should return false for non-webpack-internal paths', () => {
expect(isWebpackBundled('<anonymous>')).toBe(false)
expect(isWebpackBundled('file:///src/hello.tsx')).toBe(false)
})
})
describe('formatFrameSourceFile', () => {
it('should return the original file path', () => {
expect(formatFrameSourceFile('webpack-internal:///./src/hello.tsx')).toBe(
'./src/hello.tsx'
)
expect(
formatFrameSourceFile(
'rsc://React/Server/webpack-internal:///(rsc)/./src/hello.tsx?42'
)
).toBe('./src/hello.tsx')
expect(
formatFrameSourceFile(
'rsc://React/Server/webpack:///(rsc)/./src/hello.tsx?42'
)
).toBe('./src/hello.tsx')
expect(
formatFrameSourceFile(
'rsc://React/Server/webpack:///(app-pages-browser)/./src/hello.tsx?42'
)
).toBe('./src/hello.tsx')
expect(formatFrameSourceFile('webpack://_N_E/./src/hello.tsx')).toBe(
'./src/hello.tsx'
)
expect(formatFrameSourceFile('webpack://./src/hello.tsx')).toBe(
'./src/hello.tsx'
)
expect(formatFrameSourceFile('webpack:///./src/hello.tsx')).toBe(
'./src/hello.tsx'
)
})
it('should return an empty string for <anonymous> file paths', () => {
expect(formatFrameSourceFile('<anonymous>')).toBe('')
})
})
})
const replacementRegExes = [
/^(rsc:\/\/React\/[^/]+\/)/,
/^webpack-internal:\/\/\/(\([\w-]+\)\/)?/,
/^(webpack:\/\/\/|webpack:\/\/(_N_E\/)?)(\([\w-]+\)\/)?/,
/\?\d+$/, // React's fakeFunctionIdx query param
]
export function isWebpackBundled(file: string) {
for (const regex of replacementRegExes) {
if (regex.test(file)) return true
file = file.replace(regex, '')
}
return false
}
/**
* Format the webpack internal id to original file path
* webpack-internal:///./src/hello.tsx => ./src/hello.tsx
* rsc://React/Server/webpack-internal:///(rsc)/./src/hello.tsx?42 => ./src/hello.tsx
* webpack://_N_E/./src/hello.tsx => ./src/hello.tsx
* webpack://./src/hello.tsx => ./src/hello.tsx
* webpack:///./src/hello.tsx => ./src/hello.tsx
*
* <anonymous> => ''
*/
export function formatFrameSourceFile(file: string) {
if (file === '<anonymous>') return ''
for (const regex of replacementRegExes) {
file = file.replace(regex, '')
}
return file
}
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
noContent, noContent,
type OriginalStackFrameResponse, type OriginalStackFrameResponse,
} from './shared' } from './shared'
import { NEXT_PROJECT_ROOT } from '../../../../build/next-dir-paths'
export { getServerError } from '../internal/helpers/node-stack-frames' export { getServerError } from '../internal/helpers/node-stack-frames'
export { parseStack } from '../internal/helpers/parse-stack' export { parseStack } from '../internal/helpers/parse-stack'
export { getSourceMapFromFile } export { getSourceMapFromFile }
...@@ -20,6 +21,7 @@ export { getSourceMapFromFile } ...@@ -20,6 +21,7 @@ export { getSourceMapFromFile }
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type webpack from 'webpack' import type webpack from 'webpack'
import type { RawSourceMap } from 'next/dist/compiled/source-map08' import type { RawSourceMap } from 'next/dist/compiled/source-map08'
import { formatFrameSourceFile } from '../internal/helpers/webpack-module-path'
type Source = type Source =
| { | {
...@@ -149,10 +151,12 @@ export async function createOriginalStackFrame({ ...@@ -149,10 +151,12 @@ export async function createOriginalStackFrame({
) )
) )
const resolvedFilePath = sourceContent
? path.relative(rootDirectory, filePath)
: sourcePosition.source
const traced = { const traced = {
file: sourceContent file: resolvedFilePath,
? path.relative(rootDirectory, filePath)
: sourcePosition.source,
lineNumber: sourcePosition.line, lineNumber: sourcePosition.line,
column: (sourcePosition.column ?? 0) + 1, column: (sourcePosition.column ?? 0) + 1,
methodName: methodName:
...@@ -286,8 +290,17 @@ export function getOverlayMiddleware(options: { ...@@ -286,8 +290,17 @@ export function getOverlayMiddleware(options: {
return badRequest(res) return badRequest(res)
} }
const formattedFilePath = formatFrameSourceFile(frame.file)
const filePath = path.join(rootDirectory, formattedFilePath)
const isNextjsSource = filePath.startsWith(NEXT_PROJECT_ROOT)
let source: Source | undefined let source: Source | undefined
if (isNextjsSource) {
sourcePackage = 'next'
return json(res, { sourcePackage })
}
try { try {
source = await getSource(frame.file, { source = await getSource(frame.file, {
distDirectory, distDirectory,
......
...@@ -1288,20 +1288,18 @@ export default function Home() { ...@@ -1288,20 +1288,18 @@ export default function Home() {
expect(stack).toMatchInlineSnapshot(` expect(stack).toMatchInlineSnapshot(`
"app/utils.ts (1:7) @ eval "app/utils.ts (1:7) @ eval
--- ---
(app-pages-browser)/./app/utils.ts ./app/utils.ts
file://TEST_DIR/.next/static/chunks/app/page.js (39:1) file://TEST_DIR/.next/static/chunks/app/page.js (39:1)
--- ---
Next.js Next.js
--- ---
eval eval
(app-pages-browser)/./app/page.js ./app/page.js
--- ---
(app-pages-browser)/./app/page.js ./app/page.js
file://TEST_DIR/.next/static/chunks/app/page.js (28:1) file://TEST_DIR/.next/static/chunks/app/page.js (28:1)
--- ---
Next.js Next.js"
---
React"
`) `)
} }
......
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