-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Fix console.trace including unexpected lines #88319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
timneutkens
wants to merge
2
commits into
canary
Choose a base branch
from
01-09-fix_console.trace_including_unexpected_lines
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−7
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,28 @@ function convertToDimmedArgs( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * For console.trace, we need special handling because it captures the stack trace | ||
| * at the point where it's called. When wrapped by multiple patching functions, | ||
| * the stack trace includes all the wrapper frames which pollutes the output. | ||
| * | ||
| * This function captures a clean stack trace, filters out internal Next.js frames, | ||
| * and formats it like console.trace would. | ||
| */ | ||
| function getCleanStackTrace(): string { | ||
| const err = new Error() | ||
| const stack = err.stack || '' | ||
| const lines = stack.split('\n') | ||
|
|
||
| // Filter out internal wrapper frames from node-environment-extensions | ||
| const filteredLines = lines.filter((line) => { | ||
| return !line.includes('/node-environment-extensions/') | ||
| }) | ||
|
|
||
| // Remove the "Error" header line and join | ||
| return filteredLines.slice(1).join('\n') | ||
| } | ||
|
|
||
| // Based on https://github.com/facebook/react/blob/28dc0776be2e1370fe217549d32aee2519f0cf05/packages/react-server/src/ReactFlightServer.js#L248 | ||
| function patchConsoleMethod(methodName: InterceptableConsoleMethod): void { | ||
| const descriptor = Object.getOwnPropertyDescriptor(console, methodName) | ||
|
|
@@ -181,6 +203,13 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void { | |
| const wrapperMethod = function (this: typeof console, ...args: any[]) { | ||
| const consoleStore = consoleAsyncStorage.getStore() | ||
|
|
||
| // Special handling for console.trace: capture the stack trace early | ||
| // before the wrapper chain pollutes it, then use console.log to output | ||
| let traceStack: string | undefined | ||
| if (methodName === 'trace') { | ||
| traceStack = getCleanStackTrace() | ||
| } | ||
|
|
||
| // First we see if there is a cache signal for our current scope. If we're in a client render it'll | ||
| // come from the client React cacheSignal implementation. If we are in a server render it'll come from | ||
| // the server React cacheSignal implementation. Any particular console call will be in one, the other, or neither | ||
|
|
@@ -200,18 +229,26 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void { | |
| consoleStore, | ||
| originalMethod, | ||
| methodName, | ||
| args | ||
| args, | ||
| traceStack | ||
| ) | ||
| } else if (consoleStore?.dim === true) { | ||
| return applyWithDimming.call( | ||
| this, | ||
| consoleStore, | ||
| originalMethod, | ||
| methodName, | ||
| args | ||
| args, | ||
| traceStack | ||
| ) | ||
| } else { | ||
| return originalMethod.apply(this, args) | ||
| return applyMethod.call( | ||
| this, | ||
| originalMethod, | ||
| methodName, | ||
| args, | ||
| traceStack | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -239,7 +276,8 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void { | |
| consoleStore, | ||
| originalMethod, | ||
| methodName, | ||
| args | ||
| args, | ||
| traceStack | ||
| ) | ||
| } | ||
| // intentional fallthrough | ||
|
|
@@ -255,10 +293,17 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void { | |
| consoleStore, | ||
| originalMethod, | ||
| methodName, | ||
| args | ||
| args, | ||
| traceStack | ||
| ) | ||
| } else { | ||
| return originalMethod.apply(this, args) | ||
| return applyMethod.call( | ||
| this, | ||
| originalMethod, | ||
| methodName, | ||
| args, | ||
| traceStack | ||
| ) | ||
| } | ||
| default: | ||
| workUnitStore satisfies never | ||
|
|
@@ -273,13 +318,48 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper to apply the console method, with special handling for console.trace | ||
| */ | ||
| function applyMethod<F extends (this: Console, ...args: any[]) => any>( | ||
| this: Console, | ||
| method: F, | ||
| methodName: InterceptableConsoleMethod, | ||
| args: Parameters<F>, | ||
| traceStack?: string | ||
| ): ReturnType<F> { | ||
| if (methodName === 'trace' && traceStack !== undefined) { | ||
| // For console.trace, output the label + clean stack using console.log | ||
| // This goes through the wrapper chain for proper file logging | ||
| const label = args.length > 0 ? `Trace: ${args.join(' ')}` : 'Trace' | ||
| return console.log(`${label}\n${traceStack}`) as ReturnType<F> | ||
| } | ||
| return method.apply(this, args) | ||
| } | ||
|
|
||
| function applyWithDimming<F extends (this: Console, ...args: any[]) => any>( | ||
| this: Console, | ||
| consoleStore: undefined | ConsoleStore, | ||
| method: F, | ||
| methodName: InterceptableConsoleMethod, | ||
| args: Parameters<F> | ||
| args: Parameters<F>, | ||
| traceStack?: string | ||
| ): ReturnType<F> { | ||
| // Special handling for console.trace with clean stack | ||
| if (methodName === 'trace' && traceStack !== undefined) { | ||
| const label = args.length > 0 ? `Trace: ${args.join(' ')}` : 'Trace' | ||
| const traceOutput = `${label}\n${traceStack}` | ||
| // Use console.log with the dimmed trace output | ||
|
||
| const dimmedArgs = convertToDimmedArgs('log', [traceOutput]) | ||
| if (consoleStore?.dim === true) { | ||
| return console.log(...dimmedArgs) as ReturnType<F> | ||
| } else { | ||
| return consoleAsyncStorage.run(DIMMED_STORE, () => | ||
| console.log(...dimmedArgs) | ||
| ) as ReturnType<F> | ||
| } | ||
| } | ||
|
|
||
| if (consoleStore?.dim === true) { | ||
| return method.apply(this, convertToDimmedArgs(methodName, args)) | ||
| } else { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Console.trace output loses async storage context when calling console.log(), breaking proper file logging and dimming behavior