Skip to content

Commit

Permalink
[Flight] Add react-stack-bottom-frame to console replaying (#30926)
Browse files Browse the repository at this point in the history
Any time we're creating a stack trace we should have a
react-stack-bottom-frame so we know what to filter out.

This is the same thing we already do for createFakeJSXCallStackInDEV but
we should do that when replaying logs too.
  • Loading branch information
sebmarkbage authored Sep 9, 2024
1 parent 0dbacf2 commit 49825c0
Showing 1 changed file with 69 additions and 36 deletions.
105 changes: 69 additions & 36 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,61 @@ function getCurrentStackInDEV(): string {
return '';
}
const replayConsoleWithCallStack = {
'react-stack-bottom-frame': function (
response: Response,
methodName: string,
stackTrace: ReactStackTrace,
owner: null | ReactComponentInfo,
env: string,
args: Array<mixed>,
): void {
// There really shouldn't be anything else on the stack atm.
const prevStack = ReactSharedInternals.getCurrentStack;
ReactSharedInternals.getCurrentStack = getCurrentStackInDEV;
currentOwnerInDEV = owner;

try {
const callStack = buildFakeCallStack(
response,
stackTrace,
env,
bindToConsole(methodName, args, env),
);
if (owner != null) {
const task = initializeFakeTask(response, owner, env);
initializeFakeStack(response, owner);
if (task !== null) {
task.run(callStack);
return;
}
}
const rootTask = getRootTask(response, env);
if (rootTask != null) {
rootTask.run(callStack);
return;
}
callStack();
} finally {
ReactSharedInternals.getCurrentStack = prevStack;
}
},
};

const replayConsoleWithCallStackInDEV: (
response: Response,
methodName: string,
stackTrace: ReactStackTrace,
owner: null | ReactComponentInfo,
env: string,
args: Array<mixed>,
) => void = __DEV__
? // We use this technique to trick minifiers to preserve the function name.
(replayConsoleWithCallStack['react-stack-bottom-frame'].bind(
replayConsoleWithCallStack,
): any)
: (null: any);

function resolveConsoleEntry(
response: Response,
value: UninitializedModel,
Expand Down Expand Up @@ -2361,43 +2416,21 @@ function resolveConsoleEntry(
const env = payload[3];
const args = payload.slice(4);

// There really shouldn't be anything else on the stack atm.
const prevStack = ReactSharedInternals.getCurrentStack;
ReactSharedInternals.getCurrentStack = getCurrentStackInDEV;
currentOwnerInDEV = owner;

try {
if (!enableOwnerStacks) {
// Printing with stack isn't really limited to owner stacks but
// we gate it behind the same flag for now while iterating.
bindToConsole(methodName, args, env)();
return;
}
const callStack = buildFakeCallStack(
response,
stackTrace,
env,
bindToConsole(methodName, args, env),
);
if (owner != null) {
const task = initializeFakeTask(response, owner, env);
initializeFakeStack(response, owner);
if (task !== null) {
task.run(callStack);
return;
}
// TODO: Set the current owner so that captureOwnerStack() adds the component
// stack during the replay - if needed.
}
const rootTask = getRootTask(response, env);
if (rootTask != null) {
rootTask.run(callStack);
return;
}
callStack();
} finally {
ReactSharedInternals.getCurrentStack = prevStack;
if (!enableOwnerStacks) {
// Printing with stack isn't really limited to owner stacks but
// we gate it behind the same flag for now while iterating.
bindToConsole(methodName, args, env)();
return;
}

replayConsoleWithCallStackInDEV(
response,
methodName,
stackTrace,
owner,
env,
args,
);
}

function mergeBuffer(
Expand Down

0 comments on commit 49825c0

Please sign in to comment.