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

fix(gatsby): Improve error message when EnsureResources fails to ensure a resource #21429

Merged
merged 9 commits into from
Feb 29, 2020

Conversation

blainekasten
Copy link
Contributor

Description

Documentation

It turns out that in rare cases, EnsureResources can fail. It's not currently known what can cause it. But the outcome of when it fails is that we hit an error accessing properties on an undefined object. https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/root.js#L75

So this fix at least throws an error in render and doesn't try to render the app causing misleading errors.

Related Issues

Addresses #19959

@@ -12,7 +12,8 @@
"strictPropertyInitialization": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"esModuleInterop": true
"esModuleInterop": true,
"jsx": "preserve"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this flag is so we can do react stuff. it was throwing an error and breaking builds

@pieh
Copy link
Contributor

pieh commented Feb 21, 2020

I have reservation about this - this will hide actual error in the runtime so it's more difficult to try to debug things. This is particularly problematic when the service worker is involved (because state of service worker caches etc is not sharable, and actual errors is only thing we can use to try to get to the bottom of things - it's still not easy, and sometimes even impossible to do, but at least there might be some clues).

So I don't know if hiding error here is good idea?

@blainekasten
Copy link
Contributor Author

@pieh I may be wrong. But I don't think this is really making any functional difference from what is happening currently besides that the error it reports is more helpful.

Currently without our change, we call the render this.props.children(this.state). And if this.state has a blank pageResources, then we just crash here instead: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/root.js#L75

Since this code is assuming pageResources absolutely exists it crashes. And then the error is more obtuse. This change at least lets the user have a bit more context. We could add a detail to the log that says to wipe the .cache dir and rebuild to fix it

`This typically means that an issue occurred building components for that path`
)
throw new Error(
`EnsureResources was not able to find resources for path: "${this.props.location.pathname}"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not duplicate this line in warning and error message - I would vote to get rid of warn completely and just put this whole text in thrown error - sounds reasonable?

Copy link
Contributor

@pieh pieh Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I was mostly thinking about production builds here - maybe we should have conditional logic for gatsby develop (using process.env.NODE_ENV === `production` to show short(ish) message, but for gatsby develop case we can show cleaning up .cache (but it probably should be both .cache and public given stale page-data being on of potential reasons for this)

@pieh
Copy link
Contributor

pieh commented Feb 24, 2020

Hmmm, ok - I can agree with that.

I'm not sure if we should mention wiping .cache - this is browser error that end users of site can see and not just site developers (tho react will show stuff like "Error: Minified React error #130" (my favourite react error ;P) so maybe this is fine thing to do.

Tho with recent discovery that error like this is caused by stale page-data.json file - we should be able to just fix this (and maybe remove that error - it's always few bytes that might not need to be there :P)

Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added small comment, after that I think it's good to go

packages/gatsby/cache-dir/ensure-resources.js Outdated Show resolved Hide resolved
@blainekasten blainekasten merged commit 7527993 into master Feb 29, 2020
@blainekasten blainekasten deleted the fix/ensure-resources branch February 29, 2020 16:45
@wardpeet
Copy link
Contributor

wardpeet commented Mar 2, 2020

Fixed in [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants