-
Notifications
You must be signed in to change notification settings - Fork 45.9k
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
[DevTools] Print component stacks as error objects to get source mapping #30289
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -40,6 +42,8 @@ export function describeFiber( | |||
} = workTagMap; | |||
|
|||
switch (workInProgress.tag) { | |||
case HostHoistable: | |||
case HostSingleton: |
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.
This was missing previously leading to missing frames for html/body which lead to mismatches.
} | ||
|
||
export function describeDebugInfoFrame(name: string, env: ?string): string { | ||
return describeBuiltInComponentFrame(name + (env ? ' (' + env + ')' : '')); | ||
return describeBuiltInComponentFrame(name + (env ? ' [' + env + ']' : '')); |
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.
Because parenthesis are parsed as part of the location in Chrome we need to replace those with brackets. Not sure why I used parenthesis in the first place really.
suffix = ' (<anonymous>)'; | ||
} else if (__IS_FIREFOX__) { | ||
suffix = '@unknown:0:0'; | ||
} |
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.
Chrome parses the name as the location if it doesn't have one. <anonymous>
is the only special case that works I think.
Firefox skips lines that don't have location so we need to add a fake one which can be anything really. I don't think there's any special case that doesn't linkify it.
Safari is fine without it.
2b01aac
to
b12be26
Compare
b12be26
to
5297db2
Compare
5297db2
to
31c9db8
Compare
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.
Awesome. Left 1 clarifying question regarding filtering out %s
from the end of the first arg.
const firstArg = args[0]; | ||
if ( | ||
args.length > 1 && | ||
typeof firstArg === 'string' && | ||
firstArg.endsWith('%s') | ||
) { | ||
args[0] = firstArg.slice(0, firstArg.length - 2); // Strip the %s param | ||
} |
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.
Is this the logic for backwards-compatibility with previous versions of React? To filter out %s
at the end of the message.
If so, why is it gated with !isStrictModeOverride(...)
? Shouldn't it be independent of when error was emitted?
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.
If we're in the strict mode override we end up converting all parameterized arguments into a flat string. I'm not sure we really need to do that. E.g. for badging I tried to avoid doing that. One benefit of doing this is that we are guaranteed to be able to format the styling of all the content where as if you print them as object it might not listen to our color override.
So the formatting no longer has a trailing %s
after it has been rewritten.
I think my intension was to just leave it as a string so that the color formatting consistently can override it. However, I was going to try it with %o
. I think I messed up refactoring though so this doesn't quite make sense atm because it has already been flattened.
It's tricky to test because React should always dedupe so it doesn't get logged again during replaying.
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.
If we're in the strict mode override we end up converting all parameterized arguments into a flat string.
Oh, true. Well, we can't really guarantee that it is going to be a single flat string, because we omit other objects and user-space console stylings and just bail out.
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.
I'll just leave it intact in that case since it's non-essential and sometimes hidden anyway. It'll also go away with owner stacks since we'll never append then.
// TODO: Should this be feature tested somehow? | ||
__IS_CHROME__: false, | ||
__IS_FIREFOX__: false, | ||
__IS_EDGE__: false, |
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.
Ugh, react-devtools-inline
is shipped as an npm package, so these variables don't make much sense for it. Instead, it should be checked at runtime, not build time.
Will update this later
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.
I'm not quite sure why more of these weren't reached and subsequently errored before. Not sure why just the ones I added mattered.
__IS_FIREFOX__: false, | ||
__IS_CHROME__: false, | ||
__IS_EDGE__: false, |
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.
Will probably add __IS_NATIVE__
here as well.
This is the same change as in #30289 but for the main runtime - e.g. parent stacks in errorInfo.componentStack, appended stacks to console.error coming from React itself and when we add virtual frames to owner stacks. Since we don't add location information these frames look weird to some stack parsers - such as the native one. This is an existing issue when you want to use some off-the-shelf parsers to parse production component stacks for example. While we won't add Error objects to logs ourselves necessarily, some third party could want to do the same thing we do in DevTools and so we should provide the same capability to just take this trace and print it using an Error object.
This is the same change as in #30289 but for the main runtime - e.g. parent stacks in errorInfo.componentStack, appended stacks to console.error coming from React itself and when we add virtual frames to owner stacks. Since we don't add location information these frames look weird to some stack parsers - such as the native one. This is an existing issue when you want to use some off-the-shelf parsers to parse production component stacks for example. While we won't add Error objects to logs ourselves necessarily, some third party could want to do the same thing we do in DevTools and so we should provide the same capability to just take this trace and print it using an Error object. DiffTrain build for [df783f9](df783f9)
This is the same change as in #30289 but for the main runtime - e.g. parent stacks in errorInfo.componentStack, appended stacks to console.error coming from React itself and when we add virtual frames to owner stacks. Since we don't add location information these frames look weird to some stack parsers - such as the native one. This is an existing issue when you want to use some off-the-shelf parsers to parse production component stacks for example. While we won't add Error objects to logs ourselves necessarily, some third party could want to do the same thing we do in DevTools and so we should provide the same capability to just take this trace and print it using an Error object. DiffTrain build for commit df783f9.
…t mode override We've already merged the stack with the message at this point so would need further refactoring to handle. This is non-essential and often just fully hidden.
31c9db8
to
5b3ebf0
Compare
Before:
After:
Firefox:
The first log doesn't get a stack because it's logged before DevTools boots up and connects which is unfortunate.
The second log already has a stack printed by React (this is on stable) it gets replaced by our object now.
The third and following logs don't have a stack and get one appended.
I only turn the stack into an error object if it matches what we would emit from DevTools anyway. Otherwise we assume it's not React. Since I had to change the format slightly to make this work, I first normalize the stack slightly before doing a comparison since it won't be 1:1.