-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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): try to automatically recover when parcel segfaults #38773
Conversation
let cacheClosePromise = Promise.resolve() | ||
let bundles: RunParcelReturn | ||
try { | ||
// @ts-ignore store is public field on LMDBCache class, but public interface for Cache | ||
// doesn't have it. There doesn't seem to be proper public API for this, so we have to | ||
// resort to reaching into internals. Just in case this is wrapped in try/catch if | ||
// parcel changes internals in future (closing cache is only needed when retrying | ||
// so the if the change happens we shouldn't fail on happy builds) | ||
cacheClosePromise = cache.store.close() |
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.
we are killing child process so this should be done automatically on process exits
jest.mock(`gatsby-worker`, () => { | ||
const gatsbyWorker = jest.requireActual(`gatsby-worker`) | ||
|
||
const { WorkerPool: OriginalWorkerPool } = gatsbyWorker | ||
|
||
class WorkerPoolThatCanUseTS extends OriginalWorkerPool { | ||
constructor(workerPath, options) { | ||
options.env = { | ||
...(options.env ?? {}), | ||
NODE_OPTIONS: `--require ${require.resolve( | ||
`./packages/gatsby/src/utils/worker/__tests__/test-helpers/ts-register.js` | ||
)}`, | ||
} | ||
super(workerPath, options) | ||
} | ||
} | ||
|
||
return { | ||
...gatsbyWorker, | ||
WorkerPool: WorkerPoolThatCanUseTS, | ||
} | ||
}) |
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 is just a trick for unit tests to be able to use source TS (and ESM) files when using gatsby-worker
/ child process setup
In actual usage TS source files become CJS and those will be used (all e2e tests would be using that for example)
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 is added in this PR and as global mock because compile-gatsby-files
is being directly and indrectly used in quite a bit of unit tests, so to avoid hunting for all indirect usage we just allow that to happen any time gatsby-worker
is used within those tests
2ac6db2
to
3e72735
Compare
"filePath": "<PROJECT_ROOT>/gatsby-node.ts", | ||
"generalMessage": "Expected ';', '}' or <eof>", | ||
"hints": null, | ||
"origin": "@parcel/transformer-js", | ||
"specificMessage": "This is the expression part of an expression statement", |
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.
there is improvements to be made here to point out specific location as error we get is like this:
{
diagnostics: [
{
message: "Expected ';', '}' or <eof>",
codeFrames: [
{
filePath: '/Users/misiek/dev/pgs-gatsby/packages/gatsby/src/utils/parcel/__tests__/fixtures/error-in-code-ts/gatsby-node.ts',
codeHighlights: [
{
message: 'This is the expression part of an expression statement',
start: { line: 5, column: 1 },
end: { line: 5, column: 4 }
},
{
message: null,
start: { line: 5, column: 6 },
end: { line: 5, column: 7 }
}
]
}
],
hints: null,
origin: '@parcel/transformer-js'
}
]
}
but it's out of scope of this PR - purpose here is to try to handle segfaults without introducing regression - this added tests is just just a test for case we didn't have (what's the behavior on ts files that are invalid) and that I did break before my most recent commit
3e72735
to
6360181
Compare
Nice to know someone is working on this issue! I'm trying to run your branch as a local version of Gatsby in my project because this issue makes it almost impossible for me to develop.
Any suggestions on how to run your branch locally or should I just wait for it to get merged and published? |
@laurogripa I published canary version with those changes |
import { working } from "../utils/say-what-ts" | ||
import { createPages } from "../utils/create-pages-ts" | ||
|
||
this is wrong syntax that should't compile |
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.
Should this line be a comment?
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.
Oh or is this there to fail the compilation 👍
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.
Yeah, that's test fixture to test behavior when there is error in source file that should result in compilation problem and test itself is here
gatsby/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts
Lines 180 to 210 in 31e7049
it(`handles errors in TS code`, async () => { | |
process.chdir(dir.errorInCode) | |
await remove(`${dir.errorInCode}/.cache`) | |
await compileGatsbyFiles(dir.errorInCode) | |
expect(reporterPanicMock).toMatchInlineSnapshot(` | |
[MockFunction] { | |
"calls": Array [ | |
Array [ | |
Object { | |
"context": Object { | |
"filePath": "<PROJECT_ROOT>/gatsby-node.ts", | |
"generalMessage": "Expected ';', '}' or <eof>", | |
"hints": null, | |
"origin": "@parcel/transformer-js", | |
"specificMessage": "This is the expression part of an expression statement", | |
}, | |
"id": "11901", | |
}, | |
], | |
], | |
"results": Array [ | |
Object { | |
"type": "return", | |
"value": undefined, | |
}, | |
], | |
} | |
`) | |
}) | |
}) |
I did break this behavior when iterating on this code and noticed we didn't have tests for this case so I added it as part of this PR
#38773 (comment) this comment highlight opportunity to improve error message possibly, but this was just out of scope of the fix
* fix(gatsby): try to automatically recover when parcel segfauls * test: make gatsby-worker test adjustment global * fix: handle actual compilation errors * test: bump timeout for windows * init bundles array so TS is happy (cherry picked from commit 0a80cd6)
…) (#38799) * fix(gatsby): try to automatically recover when parcel segfauls * test: make gatsby-worker test adjustment global * fix: handle actual compilation errors * test: bump timeout for windows * init bundles array so TS is happy (cherry picked from commit 0a80cd6) Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Description
Seems like ARM macs parcel (at least version we use) cause quite a bit of problems and results in often seg faults - this tries to run parcel in child process so we can try to handle segfaults by clearing just parcel cache and retrying (few times)
Documentation
Tests
Related Issues
Possibly fixes #38483