-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Flight] Implement captureStackTrace and owner stacks on the Server (#…
…30197) Wire up owner stacks in Flight to the shared internals. This exposes it to `captureOwnerStack()`. In this case we install it permanently as we only allow one RSC renderer which then supports async contexts. Same thing we do for owner. This also ends up adding it to errors logged by React through `consoleWithStackDev`. The plan is to eventually remove that but this is inline with what we do in Fizz and Fiber already. However, at the same time we've instrumented the console so we need to strip them back out before sending to the client. This lets the client control whether to add the stack back in or allowing `console.createTask` to control it. This is another reason we shouldn't append them from React but for now we hack it by removing them after the fact.
- Loading branch information
1 parent
8e9de89
commit 0b5835a
Showing
5 changed files
with
221 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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
52 changes: 52 additions & 0 deletions
52
packages/react-server/src/flight/ReactFlightComponentStack.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {ReactComponentInfo} from 'shared/ReactTypes'; | ||
|
||
import {describeBuiltInComponentFrame} from 'shared/ReactComponentStackFrame'; | ||
|
||
import {enableOwnerStacks} from 'shared/ReactFeatureFlags'; | ||
|
||
export function getOwnerStackByComponentInfoInDev( | ||
componentInfo: ReactComponentInfo, | ||
): string { | ||
if (!enableOwnerStacks || !__DEV__) { | ||
return ''; | ||
} | ||
try { | ||
let info = ''; | ||
|
||
// The owner stack of the current component will be where it was created, i.e. inside its owner. | ||
// There's no actual name of the currently executing component. Instead, that is available | ||
// on the regular stack that's currently executing. However, if there is no owner at all, then | ||
// there's no stack frame so we add the name of the root component to the stack to know which | ||
// component is currently executing. | ||
if (!componentInfo.owner && typeof componentInfo.name === 'string') { | ||
return describeBuiltInComponentFrame(componentInfo.name); | ||
} | ||
|
||
let owner: void | null | ReactComponentInfo = componentInfo; | ||
|
||
while (owner) { | ||
if (typeof owner.stack === 'string') { | ||
// Server Component | ||
const ownerStack: string = owner.stack; | ||
owner = owner.owner; | ||
if (owner && ownerStack !== '') { | ||
info += '\n' + ownerStack; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
return info; | ||
} catch (x) { | ||
return '\nError generating stack: ' + x.message + '\n' + x.stack; | ||
} | ||
} |
This file contains 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