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

Read constructor name more carefully #29954

Merged
merged 4 commits into from
Jun 25, 2024
Merged

Read constructor name more carefully #29954

merged 4 commits into from
Jun 25, 2024

Conversation

LoganDark
Copy link
Contributor

Summary

Sometimes constructor happens to be the name of an unrelated property, or we may be dealing with a Proxy that intercepts every read. Verify the constructor is a function before using its name, and reset the name anyway if it turns out not to be serializable.

Fixes some cases of the devtools crashing and becoming inoperable upon attempting to inspect components whose props are Hookstate States.

How did you test this change?

Installed a patched version of the extension and confirmed that it solves the problem.

Sometimes `constructor` happens to be the name of an unrelated property, or we may be dealing with a `Proxy` that intercepts every read. Verify the constructor is a function before using its name, and reset the name anyway if it turns out not to be serializable.

Fixes some cases of the devtools crashing and becoming inoperable upon attempting to inspect components whose props are Hookstate `State`s.
Copy link

vercel bot commented Jun 19, 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 Jun 25, 2024 6:23pm

@facebook-github-bot
Copy link

Hi @LoganDark!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@hoxyq
Copy link
Contributor

hoxyq commented Jun 19, 2024

Thanks for upstreaming this, I have a few questions:

  • Is it possible to add a test which shows that React DevTools is failing to inspect some of such components?
  • Why do we need to use structuredClone and not just typeof <name> === 'string'?

@LoganDark
Copy link
Contributor Author

LoganDark commented Jun 19, 2024

  • Is it possible to add a test which shows that React DevTools is failing to inspect some of such components?

I'm not sure. I don't really have much time to do that right now, though. I don't imagine it'd be hard, just pass a prop that is {constructor: {name: () => {}}}.

Why do we need to use structuredClone and not just typeof <name> === 'string'?

AFAIK it could be a proxy pretending to be string. It's not really the type that we need to check here, it's whether trying to send it over the bridge would throw exceptions. structuredClone is a decent way to check because it should throw errors in the same situations.

Of course we could probably put a try/catch around each attempt to send a message as well so that unserializable messages don't permanently brick the queue.

@hoxyq
Copy link
Contributor

hoxyq commented Jun 24, 2024

  • Is it possible to add a test which shows that React DevTools is failing to inspect some of such components?

I'm not sure. I don't really have much time to do that right now, though. I don't imagine it'd be hard, just pass a prop that is {constructor: {name: () => {}}}.

Why do we need to use structuredClone and not just typeof <name> === 'string'?

AFAIK it could be a proxy pretending to be string. It's not really the type that we need to check here, it's whether trying to send it over the bridge would throw exceptions. structuredClone is a decent way to check because it should throw errors in the same situations.

Of course we could probably put a try/catch around each attempt to send a message as well so that unserializable messages don't permanently brick the queue.

I think the change makes sense overall, but I would prefer us to be more conservative about the the constructor.name, this should be a string. Bridge is agnostic to the way the data is transferred, it could be anything: browser extension API, websocket message, Chrome DevTools protocol Runtime.bindingCalled event.

AFAIK it could be a proxy pretending to be string.

I am not sure if you can actually implement a proxy that would override typeof in a way that it would return string. Anyways, this seems like an extreme case we shouldn't explicitly cover.

Would you be able to update the PR accordingly?

@LoganDark
Copy link
Contributor Author

Would you be able to update the PR accordingly?

You mean to just check if the type is string instead of trying structuredClone?

@hoxyq
Copy link
Contributor

hoxyq commented Jun 25, 2024

Would you be able to update the PR accordingly?

You mean to just check if the type is string instead of trying structuredClone?

Yes.

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.

LGTM. Thanks for fixing this.

Please fix the linter issues, and then we can merge this.

@LoganDark
Copy link
Contributor Author

LoganDark commented Jun 25, 2024

Ugh, it's another format checker that doesn't provide a diff. (Actually, I think it's always been prettier that consistently does not.)

I suppose I can do that next Monday when I have access to a terminal. I could've done it now if I had a diff, but if I have to clone the repo to my local machine and run a command on it, I can't do that until next Monday since I'm off this week.

I can't even imagine what prettier might be getting upset about anyway.

@LoganDark
Copy link
Contributor Author

LoganDark commented Jun 25, 2024

yeah okay, I truly have no clue what Prettier is getting upset about. I guess I'll properly run the command next Monday. And maybe then I'll open another PR to fix your CI (or open a PR to Prettier to fix their project), as it's really supposed to be providing a diff.

(apologies if this comes off as rude, I mean nothing by it. just frustrated by this having happened multiple times, i.e. having to clone a repo to my computer just to run the stupid formatter because the CI complains that something is wrong without telling me what.)

(and update: looks like it is an issue with Prettier itself, go figure. Not your fault then.)

@hoxyq hoxyq merged commit 133ada7 into facebook:main Jun 25, 2024
160 checks passed
@LoganDark LoganDark deleted the constructor-name branch June 25, 2024 19:41
@LoganDark
Copy link
Contributor Author

thanks!

hoxyq added a commit that referenced this pull request Jul 3, 2024
## Summary

Full list of changes, mostly fixes:
* chore[react-devtools/renderer]: dont show strict mode warning for prod
renderer builds ([hoxyq](https://github.com/hoxyq) in
[#30158](#30158))
* chore[react-devtools/ui]: fix strict mode badge styles
([hoxyq](https://github.com/hoxyq) in
[#30159](#30159))
* fix[react-devtools]: restore original args when recording errors
([hoxyq](https://github.com/hoxyq) in
[#30091](#30091))
* Read constructor name more carefully
([LoganDark](https://github.com/LoganDark) in
[#29954](#29954))
* refactor[react-devtools/extensions]: dont debounce cleanup logic on
navigation ([hoxyq](https://github.com/hoxyq) in
[#30027](#30027))
* lint: enable reportUnusedDisableDirectives and remove unused
suppressions ([kassens](https://github.com/kassens) in
[#28721](#28721))
* fix[react-devtools/extensions]: propagate globals from env
([hoxyq](https://github.com/hoxyq) in
[#29963](#29963))
* refactor[react-devtools/tests]: use registered marks instead of
cleared in tests ([hoxyq](https://github.com/hoxyq) in
[#29929](#29929))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants