Skip to content
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

Merged
merged 2 commits into from
Jul 8, 2024

Conversation

sebmarkbage
Copy link
Collaborator

Before:

Screenshot 2024-07-04 at 3 20 34 PM

After:

Screenshot 2024-07-05 at 6 08 28 PM

Firefox:

Screenshot 2024-07-05 at 6 09 50 PM

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.

@sebmarkbage sebmarkbage requested a review from hoxyq July 5, 2024 22:21
Copy link

vercel bot commented Jul 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 8, 2024 10:34pm
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Jul 5, 2024
@@ -40,6 +42,8 @@ export function describeFiber(
} = workTagMap;

switch (workInProgress.tag) {
case HostHoistable:
case HostSingleton:
Copy link
Collaborator Author

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 + ']' : ''));
Copy link
Collaborator Author

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';
}
Copy link
Collaborator Author

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.

Copy link
Contributor

@hoxyq hoxyq left a 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.

Comment on lines 291 to 298
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
}
Copy link
Contributor

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?

Copy link
Collaborator Author

@sebmarkbage sebmarkbage Jul 8, 2024

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.

Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Comment on lines +76 to +79
// TODO: Should this be feature tested somehow?
__IS_CHROME__: false,
__IS_FIREFOX__: false,
__IS_EDGE__: false,
Copy link
Contributor

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

Copy link
Collaborator Author

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.

Comment on lines 71 to +73
__IS_FIREFOX__: false,
__IS_CHROME__: false,
__IS_EDGE__: false,
Copy link
Contributor

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.

sebmarkbage added a commit that referenced this pull request Jul 8, 2024
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.
github-actions bot pushed a commit that referenced this pull request Jul 8, 2024
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)
github-actions bot pushed a commit that referenced this pull request Jul 8, 2024
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.
@sebmarkbage sebmarkbage merged commit 491a4ea into facebook:main Jul 8, 2024
139 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
3 participants