Unverified Commit d457e63e authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

[devtools] Force ANSI colors on overlay code frames regardless of TTY (#93550)

### What?

The dev overlay's code frame loses syntax highlighting whenever `next
dev` runs under a non-TTY parent (VS Code / Cursor's integrated
terminal, Docker without `-t`, `concurrently`, the JavaScript Debug
Terminal, CI logs, etc.). Tokens render as plain monochrome text instead
of the usual colored output.

### Why?

The "syntax highlighting" in the overlay isn't real highlighting. The
server embeds ANSI escape codes into the code frame string, and the
browser overlay parses them via Anser to render colored `<span>`s.
Whether the dev server's stdout is a TTY is irrelevant on this path —
the consumer is the browser.

Since #71860, `getOriginalCodeFrame` defaults `colors` to
`process.stdout.isTTY`, and the overlay middlewares relied on that
default. The moment stdout was a pipe, the overlay had nothing to color.

### How?

`createOriginalStackFrame` and `getOriginalStackFrames` are shared by
five consumers (overlay HTTP endpoint, webpack `Module not found`
plugin, browser-log forwarding, MCP error formatter, hot-reloader
resolver). Hardcoding `colors: true` would leak escape sequences into
the four non-overlay paths.

Thread a `codeFrameOptions: { colors?, maxWidth? }` parameter through
both functions. The overlay HTTP middlewares opt in to `colors: true`
and `maxWidth: DEVTOOLS_CODE_FRAME_MAX_WIDTH`; everyone else keeps the
existing TTY-gated colors and terminal-width wrapping. (The 1000-column
override from #92621 was suffering from the same leak — also scoped to
the overlay now.)

Tested from Cursor's integrated terminal with/without fix:

Before:
<img width="1174" height="1070" alt="CleanShot 2026-05-06 at 22 34
45@2x"
src="https://github.com/user-attachments/assets/16ef1ac2-749b-443d-9e76-42f1318e9cd1"
/>


After:
<img width="1254" height="1066" alt="CleanShot 2026-05-06 at 22 34
30@2x"
src="https://github.com/user-attachments/assets/577ef3c2-3f02-4bb5-9825-63aa5564c593"
/>
parent 401ce380
...@@ -283,10 +283,22 @@ async function nativeTraceSource( ...@@ -283,10 +283,22 @@ async function nativeTraceSource(
return undefined return undefined
} }
/**
* Code frame rendering options. The defaults match terminal consumers; only
* the overlay HTTP path opts in to always-on colors and the wide max width.
*/
type CodeFrameOptions = {
/** Defaults to `process.stdout.isTTY`. */
colors?: boolean
/** Defaults to the dev server's terminal width. */
maxWidth?: number
}
async function createOriginalStackFrame( async function createOriginalStackFrame(
project: Project, project: Project,
projectPath: string, projectPath: string,
frame: TurbopackStackFrame frame: TurbopackStackFrame,
codeFrameOptions?: CodeFrameOptions
): Promise<OriginalStackFrameResponse | null> { ): Promise<OriginalStackFrameResponse | null> {
const traced = const traced =
(await nativeTraceSource(frame)) ?? (await nativeTraceSource(frame)) ??
...@@ -324,9 +336,8 @@ async function createOriginalStackFrame( ...@@ -324,9 +336,8 @@ async function createOriginalStackFrame(
get originalCodeFrame() { get originalCodeFrame() {
if (originalCodeFrame === undefined) { if (originalCodeFrame === undefined) {
originalCodeFrame = getOriginalCodeFrame(tracedFrame, traced.source, { originalCodeFrame = getOriginalCodeFrame(tracedFrame, traced.source, {
// The overlay renders in a browser with horizontal scrolling, colors: codeFrameOptions?.colors,
// so don't truncate lines to the server's terminal width. maxWidth: codeFrameOptions?.maxWidth,
maxWidth: DEVTOOLS_CODE_FRAME_MAX_WIDTH,
}) })
} }
return originalCodeFrame return originalCodeFrame
...@@ -372,6 +383,13 @@ export function getOverlayMiddleware({ ...@@ -372,6 +383,13 @@ export function getOverlayMiddleware({
isServer: request.isServer, isServer: request.isServer,
isEdgeServer: request.isEdgeServer, isEdgeServer: request.isEdgeServer,
isAppDirectory: request.isAppDirectory, isAppDirectory: request.isAppDirectory,
codeFrameOptions: {
// Overlay parses ANSI in JS and renders in a scrollable
// `<pre>`, so colors are always wanted and terminal width is
// irrelevant.
colors: true,
maxWidth: DEVTOOLS_CODE_FRAME_MAX_WIDTH,
},
}) })
ignoreListAnonymousStackFramesIfSandwiched(result) ignoreListAnonymousStackFramesIfSandwiched(result)
...@@ -492,6 +510,7 @@ export async function getOriginalStackFrames({ ...@@ -492,6 +510,7 @@ export async function getOriginalStackFrames({
isServer, isServer,
isEdgeServer, isEdgeServer,
isAppDirectory, isAppDirectory,
codeFrameOptions,
}: { }: {
project: Project project: Project
projectPath: string projectPath: string
...@@ -499,6 +518,7 @@ export async function getOriginalStackFrames({ ...@@ -499,6 +518,7 @@ export async function getOriginalStackFrames({
isServer: boolean isServer: boolean
isEdgeServer: boolean isEdgeServer: boolean
isAppDirectory: boolean isAppDirectory: boolean
codeFrameOptions?: CodeFrameOptions
}): Promise<OriginalStackFramesResponse> { }): Promise<OriginalStackFramesResponse> {
const stackFrames = createStackFrames({ const stackFrames = createStackFrames({
frames, frames,
...@@ -513,7 +533,8 @@ export async function getOriginalStackFrames({ ...@@ -513,7 +533,8 @@ export async function getOriginalStackFrames({
const stackFrame = await createOriginalStackFrame( const stackFrame = await createOriginalStackFrame(
project, project,
projectPath, projectPath,
frame frame,
codeFrameOptions
) )
if (stackFrame === null) { if (stackFrame === null) {
return { return {
......
...@@ -182,12 +182,24 @@ function findOriginalSourcePositionAndContentFromCompilation( ...@@ -182,12 +182,24 @@ function findOriginalSourcePositionAndContentFromCompilation(
return module?.buildInfo?.importLocByPath?.get(importedModule) ?? null return module?.buildInfo?.importLocByPath?.get(importedModule) ?? null
} }
/**
* Code frame rendering options. The defaults match terminal consumers; only
* the overlay HTTP path opts in to always-on colors and the wide max width.
*/
type CodeFrameOptions = {
/** Defaults to `process.stdout.isTTY`. */
colors?: boolean
/** Defaults to the dev server's terminal width. */
maxWidth?: number
}
export async function createOriginalStackFrame({ export async function createOriginalStackFrame({
ignoredByDefault, ignoredByDefault,
source, source,
rootDirectory, rootDirectory,
frame, frame,
errorMessage, errorMessage,
codeFrameOptions,
}: { }: {
/** setting this to true will not consult ignoreList */ /** setting this to true will not consult ignoreList */
ignoredByDefault: boolean ignoredByDefault: boolean
...@@ -195,6 +207,7 @@ export async function createOriginalStackFrame({ ...@@ -195,6 +207,7 @@ export async function createOriginalStackFrame({
rootDirectory: string rootDirectory: string
frame: StackFrame frame: StackFrame
errorMessage?: string errorMessage?: string
codeFrameOptions?: CodeFrameOptions
}): Promise<OriginalStackFrameResponse | null> { }): Promise<OriginalStackFrameResponse | null> {
const moduleNotFound = findModuleNotFoundFromError(errorMessage) const moduleNotFound = findModuleNotFoundFromError(errorMessage)
const result = await (() => { const result = await (() => {
...@@ -263,9 +276,8 @@ export async function createOriginalStackFrame({ ...@@ -263,9 +276,8 @@ export async function createOriginalStackFrame({
get originalCodeFrame() { get originalCodeFrame() {
if (originalCodeFrame === undefined) { if (originalCodeFrame === undefined) {
originalCodeFrame = getOriginalCodeFrame(traced, sourceContent, { originalCodeFrame = getOriginalCodeFrame(traced, sourceContent, {
// The overlay renders in a browser with horizontal scrolling, colors: codeFrameOptions?.colors,
// so don't truncate lines to the server's terminal width. maxWidth: codeFrameOptions?.maxWidth,
maxWidth: DEVTOOLS_CODE_FRAME_MAX_WIDTH,
}) })
} }
return originalCodeFrame return originalCodeFrame
...@@ -393,6 +405,7 @@ export async function getOriginalStackFrames({ ...@@ -393,6 +405,7 @@ export async function getOriginalStackFrames({
serverStats, serverStats,
edgeServerStats, edgeServerStats,
rootDirectory, rootDirectory,
codeFrameOptions,
}: { }: {
isServer: boolean isServer: boolean
isEdgeServer: boolean isEdgeServer: boolean
...@@ -402,6 +415,7 @@ export async function getOriginalStackFrames({ ...@@ -402,6 +415,7 @@ export async function getOriginalStackFrames({
serverStats: () => webpack.Stats | null serverStats: () => webpack.Stats | null
edgeServerStats: () => webpack.Stats | null edgeServerStats: () => webpack.Stats | null
rootDirectory: string rootDirectory: string
codeFrameOptions?: CodeFrameOptions
}): Promise<OriginalStackFramesResponse> { }): Promise<OriginalStackFramesResponse> {
const frameResponses = await Promise.all( const frameResponses = await Promise.all(
frames.map( frames.map(
...@@ -415,6 +429,7 @@ export async function getOriginalStackFrames({ ...@@ -415,6 +429,7 @@ export async function getOriginalStackFrames({
serverStats, serverStats,
edgeServerStats, edgeServerStats,
rootDirectory, rootDirectory,
codeFrameOptions,
}).then( }).then(
(value) => { (value) => {
return { return {
...@@ -446,6 +461,7 @@ async function getOriginalStackFrame({ ...@@ -446,6 +461,7 @@ async function getOriginalStackFrame({
serverStats, serverStats,
edgeServerStats, edgeServerStats,
rootDirectory, rootDirectory,
codeFrameOptions,
}: { }: {
isServer: boolean isServer: boolean
isEdgeServer: boolean isEdgeServer: boolean
...@@ -455,6 +471,7 @@ async function getOriginalStackFrame({ ...@@ -455,6 +471,7 @@ async function getOriginalStackFrame({
serverStats: () => webpack.Stats | null serverStats: () => webpack.Stats | null
edgeServerStats: () => webpack.Stats | null edgeServerStats: () => webpack.Stats | null
rootDirectory: string rootDirectory: string
codeFrameOptions?: CodeFrameOptions
}): Promise<OriginalStackFrameResponse> { }): Promise<OriginalStackFrameResponse> {
const filename = frame.file ?? '' const filename = frame.file ?? ''
const source = await getSource(frame, { const source = await getSource(frame, {
...@@ -534,6 +551,7 @@ async function getOriginalStackFrame({ ...@@ -534,6 +551,7 @@ async function getOriginalStackFrame({
frame, frame,
source, source,
rootDirectory, rootDirectory,
codeFrameOptions,
}) })
if (!originalStackFrameResponse) { if (!originalStackFrameResponse) {
...@@ -601,6 +619,13 @@ export function getOverlayMiddleware(options: { ...@@ -601,6 +619,13 @@ export function getOverlayMiddleware(options: {
serverStats, serverStats,
edgeServerStats, edgeServerStats,
rootDirectory, rootDirectory,
codeFrameOptions: {
// Overlay parses ANSI in JS and renders in a scrollable
// `<pre>`, so colors are always wanted and terminal width
// is irrelevant.
colors: true,
maxWidth: DEVTOOLS_CODE_FRAME_MAX_WIDTH,
},
}) })
) )
} catch (err) { } catch (err) {
......
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