Unverified Commit 89fcf68c authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

decode magic identifiers (#61658)

### What?

* decode magic identifiers when printing compile errors to the console
* Error Overlay
  * decode magic identifiers in the error message
  * decode magic identifiers in source frame
  * decode magic identifiers in call stack


![image](https://github.com/vercel/next.js/assets/1365881/836ba497-84dd-4958-8341-4207fc6d860f)

![image](https://github.com/vercel/next.js/assets/1365881/f03d5c9e-0b2f-4699-b7c3-56c38b0eba51)

![image](https://github.com/vercel/next.js/assets/1365881/49a65991-92d8-47b4-863c-961536d98d13)


### Why?

### How?

PACK-2008


Closes PACK-2387
parent 55b472b5
...@@ -4,6 +4,7 @@ import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' ...@@ -4,6 +4,7 @@ import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import stripAnsi from 'next/dist/compiled/strip-ansi' import stripAnsi from 'next/dist/compiled/strip-ansi'
import { getFrameSource } from '../../helpers/stack-frame' import { getFrameSource } from '../../helpers/stack-frame'
import { useOpenInEditor } from '../../helpers/use-open-in-editor' import { useOpenInEditor } from '../../helpers/use-open-in-editor'
import { HotlinkedText } from '../hot-linked-text'
export type CodeFrameProps = { stackFrame: StackFrame; codeFrame: string } export type CodeFrameProps = { stackFrame: StackFrame; codeFrame: string }
...@@ -66,7 +67,8 @@ export const CodeFrame: React.FC<CodeFrameProps> = function CodeFrame({ ...@@ -66,7 +67,8 @@ export const CodeFrame: React.FC<CodeFrameProps> = function CodeFrame({
title="Click to open in your editor" title="Click to open in your editor"
> >
<span> <span>
{getFrameSource(stackFrame)} @ {stackFrame.methodName} {getFrameSource(stackFrame)} @{' '}
<HotlinkedText text={stackFrame.methodName} />
</span> </span>
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
......
import { getWordsAndWhitespaces } from './get-words-and-whitespaces'
describe('getWordsAndWhitespaces', () => {
it('should return sequences of words and whitespaces', () => {
const text = ' \n\nhello world https://nextjs.org/\nhttps://nextjs.org/'
expect(getWordsAndWhitespaces(text)).toEqual([
' \n\n',
'hello',
' ',
'world',
' ',
'https://nextjs.org/',
'\n',
'https://nextjs.org/',
])
})
})
// Returns true if the given character is a whitespace character, false otherwise.
function isWhitespace(char: string): boolean {
return char === ' ' || char === '\n' || char === '\t' || char === '\r'
}
/**
* Get sequences of words and whitespaces from a string.
*
* e.g. "Hello world \n\n" -> ["Hello", " ", "world", " \n\n"]
*/
export function getWordsAndWhitespaces(text: string) {
const wordsAndWhitespaces: string[] = []
let current = ''
let currentIsWhitespace = false
for (const char of text) {
if (current.length === 0) {
current += char
currentIsWhitespace = isWhitespace(char)
continue
}
const nextIsWhitespace = isWhitespace(char)
if (currentIsWhitespace === nextIsWhitespace) {
current += char
} else {
wordsAndWhitespaces.push(current)
current = char
currentIsWhitespace = nextIsWhitespace
}
}
if (current.length > 0) {
wordsAndWhitespaces.push(current)
}
return wordsAndWhitespaces
}
import React from 'react' import React from 'react'
import { getWordsAndWhitespaces } from './get-words-and-whitespaces' import {
decodeMagicIdentifier,
MAGIC_IDENTIFIER_REGEX,
} from '../../../../../../shared/lib/magic-identifier'
const linkRegex = /https?:\/\/[^\s/$.?#].[^\s"]*/i const linkRegex = /https?:\/\/[^\s/$.?#].[^\s"]*/i
const splitRegexp = new RegExp(`(${MAGIC_IDENTIFIER_REGEX.source}|\\s+)`)
export const HotlinkedText: React.FC<{ export const HotlinkedText: React.FC<{
text: string text: string
}> = function HotlinkedText(props) { }> = function HotlinkedText(props) {
const { text } = props const { text } = props
const wordsAndWhitespaces = getWordsAndWhitespaces(text) const wordsAndWhitespaces = text.split(splitRegexp)
return ( return (
<> <>
{linkRegex.test(text) {wordsAndWhitespaces.map((word, index) => {
? wordsAndWhitespaces.map((word, index) => {
if (linkRegex.test(word)) { if (linkRegex.test(word)) {
return ( return (
<React.Fragment key={`link-${index}`}> <React.Fragment key={`link-${index}`}>
...@@ -21,9 +25,27 @@ export const HotlinkedText: React.FC<{ ...@@ -21,9 +25,27 @@ export const HotlinkedText: React.FC<{
</React.Fragment> </React.Fragment>
) )
} }
try {
const decodedWord = decodeMagicIdentifier(word)
if (decodedWord !== word) {
return (
<i key={`ident-${index}`}>
{'{'}
{decodedWord}
{'}'}
</i>
)
}
} catch (e) {
return (
<i key={`ident-${index}`}>
{'{'}
{word} (decoding failed: {'' + e}){'}'}
</i>
)
}
return <React.Fragment key={`text-${index}`}>{word}</React.Fragment> return <React.Fragment key={`text-${index}`}>{word}</React.Fragment>
}) })}
: text}
</> </>
) )
} }
...@@ -5,6 +5,7 @@ import { ...@@ -5,6 +5,7 @@ import {
type OriginalStackFrame, type OriginalStackFrame,
} from '../../helpers/stack-frame' } from '../../helpers/stack-frame'
import { useOpenInEditor } from '../../helpers/use-open-in-editor' import { useOpenInEditor } from '../../helpers/use-open-in-editor'
import { HotlinkedText } from '../../components/hot-linked-text'
export const CallStackFrame: React.FC<{ export const CallStackFrame: React.FC<{
frame: OriginalStackFrame frame: OriginalStackFrame
...@@ -27,7 +28,7 @@ export const CallStackFrame: React.FC<{ ...@@ -27,7 +28,7 @@ export const CallStackFrame: React.FC<{
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)}>
{f.methodName} <HotlinkedText text={f.methodName} />
</h3> </h3>
<div <div
data-has-source={hasSource ? 'true' : undefined} data-has-source={hasSource ? 'true' : undefined}
......
import React from 'react' import React from 'react'
import type { ComponentStackFrame } from '../../helpers/parse-component-stack' import type { ComponentStackFrame } from '../../helpers/parse-component-stack'
import { useOpenInEditor } from '../../helpers/use-open-in-editor' import { useOpenInEditor } from '../../helpers/use-open-in-editor'
import { HotlinkedText } from '../../components/hot-linked-text'
function EditorLink({ function EditorLink({
children, children,
...@@ -94,7 +95,9 @@ export function ComponentStackFrameRow({ ...@@ -94,7 +95,9 @@ export function ComponentStackFrameRow({
return ( return (
<div data-nextjs-component-stack-frame> <div data-nextjs-component-stack-frame>
<h3>{component}</h3> <h3>
<HotlinkedText text={component} />
</h3>
<SourceLocation componentStackFrame={componentStackFrame} /> <SourceLocation componentStackFrame={componentStackFrame} />
</div> </div>
) )
......
...@@ -131,12 +131,16 @@ import type { ActionManifest } from '../../../build/webpack/plugins/flight-clien ...@@ -131,12 +131,16 @@ import type { ActionManifest } from '../../../build/webpack/plugins/flight-clien
import { denormalizePagePath } from '../../../shared/lib/page-path/denormalize-page-path' import { denormalizePagePath } from '../../../shared/lib/page-path/denormalize-page-path'
import type { LoadableManifest } from '../../load-components' import type { LoadableManifest } from '../../load-components'
import { generateRandomActionKeyRaw } from '../../app-render/action-encryption-utils' import { generateRandomActionKeyRaw } from '../../app-render/action-encryption-utils'
import { bold, green, red } from '../../../lib/picocolors' import { bold, green, magenta, red } from '../../../lib/picocolors'
import { writeFileAtomic } from '../../../lib/fs/write-atomic' import { writeFileAtomic } from '../../../lib/fs/write-atomic'
import { PAGE_TYPES } from '../../../lib/page-types' import { PAGE_TYPES } from '../../../lib/page-types'
import { trace } from '../../../trace' import { trace } from '../../../trace'
import type { VersionInfo } from '../../dev/parse-version-info' import type { VersionInfo } from '../../dev/parse-version-info'
import type { NextFontManifest } from '../../../build/webpack/plugins/next-font-manifest-plugin' import type { NextFontManifest } from '../../../build/webpack/plugins/next-font-manifest-plugin'
import {
MAGIC_IDENTIFIER_REGEX,
decodeMagicIdentifier,
} from '../../../shared/lib/magic-identifier'
const MILLISECONDS_IN_NANOSECOND = 1_000_000 const MILLISECONDS_IN_NANOSECOND = 1_000_000
const wsServer = new ws.Server({ noServer: true }) const wsServer = new ws.Server({ noServer: true })
...@@ -2679,13 +2683,23 @@ export async function setupDevBundler(opts: SetupOpts) { ...@@ -2679,13 +2683,23 @@ export async function setupDevBundler(opts: SetupOpts) {
export type DevBundler = Awaited<ReturnType<typeof setupDevBundler>> export type DevBundler = Awaited<ReturnType<typeof setupDevBundler>>
function renderStyledStringToErrorAnsi(string: StyledString): string { function renderStyledStringToErrorAnsi(string: StyledString): string {
function decodeMagicIdentifiers(str: string): string {
return str.replaceAll(MAGIC_IDENTIFIER_REGEX, (ident) => {
try {
return magenta(`{${decodeMagicIdentifier(ident)}}`)
} catch (e) {
return magenta(`{${ident} (decoding failed: ${e})}`)
}
})
}
switch (string.type) { switch (string.type) {
case 'text': case 'text':
return string.value return decodeMagicIdentifiers(string.value)
case 'strong': case 'strong':
return bold(red(string.value)) return bold(red(decodeMagicIdentifiers(string.value)))
case 'code': case 'code':
return green(string.value) return green(decodeMagicIdentifiers(string.value))
case 'line': case 'line':
return string.value.map(renderStyledStringToErrorAnsi).join('') return string.value.map(renderStyledStringToErrorAnsi).join('')
case 'stack': case 'stack':
......
function decodeHex(hexStr: string): string {
if (hexStr.trim() === '') {
throw new Error("can't decode empty hex")
}
const num = parseInt(hexStr, 16)
if (isNaN(num)) {
throw new Error(`invalid hex: \`${hexStr}\``)
}
return String.fromCodePoint(num)
}
const enum Mode {
Text,
Underscore,
Hex,
LongHex,
}
const DECODE_REGEX = /^__TURBOPACK__([a-zA-Z0-9_$]+)__$/
export function decodeMagicIdentifier(identifier: string): string {
const matches = identifier.match(DECODE_REGEX)
if (!matches) {
return identifier
}
const inner = matches[1]
let output = ''
let mode: Mode = Mode.Text
let buffer = ''
for (let i = 0; i < inner.length; i++) {
const char = inner[i]
if (mode === Mode.Text) {
if (char === '_') {
mode = Mode.Underscore
} else if (char === '$') {
mode = Mode.Hex
} else {
output += char
}
} else if (mode === Mode.Underscore) {
if (char === '_') {
output += ' '
mode = Mode.Text
} else if (char === '$') {
output += '_'
mode = Mode.Hex
} else {
output += char
mode = Mode.Text
}
} else if (mode === Mode.Hex) {
if (buffer.length === 2) {
output += decodeHex(buffer)
buffer = ''
}
if (char === '_') {
if (buffer !== '') {
throw new Error(`invalid hex: \`${buffer}\``)
}
mode = Mode.LongHex
} else if (char === '$') {
if (buffer !== '') {
throw new Error(`invalid hex: \`${buffer}\``)
}
mode = Mode.Text
} else {
buffer += char
}
} else if (mode === Mode.LongHex) {
if (char === '_') {
throw new Error(`invalid hex: \`${buffer + char}\``)
} else if (char === '$') {
output += decodeHex(buffer)
buffer = ''
mode = Mode.Text
} else {
buffer += char
}
}
}
return output
}
export const MAGIC_IDENTIFIER_REGEX = /__TURBOPACK__[a-zA-Z0-9_$]+__/g
...@@ -118,7 +118,7 @@ https://nextjs.org/docs/messages/module-not-found" ...@@ -118,7 +118,7 @@ https://nextjs.org/docs/messages/module-not-found"
`; `;
exports[`ReactRefreshLogBox app turbo Should not show __webpack_exports__ when exporting anonymous arrow function 1`] = ` exports[`ReactRefreshLogBox app turbo Should not show __webpack_exports__ when exporting anonymous arrow function 1`] = `
"index.js (3:2) @ __TURBOPACK__default__export__ "index.js (3:2) @ {default export}
1 | export default () => { 1 | export default () => {
2 | if (typeof window !== 'undefined') { 2 | if (typeof window !== 'undefined') {
......
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