Skip to content

Commit 9d737b6

Browse files
piehherecydevLekoArtstyhopp
authored
fix(gatsby): close parcel cache db before clearing cache and retrying (#36377)
Co-authored-by: Dan Kirkham <[email protected]> Co-authored-by: Lennart <[email protected]> Co-authored-by: Ty Hopp <[email protected]>
1 parent 977a211 commit 9d737b6

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

packages/gatsby/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@graphql-tools/load": "^7.5.10",
3131
"@jridgewell/trace-mapping": "^0.3.13",
3232
"@nodelib/fs.walk": "^1.2.8",
33+
"@parcel/cache": "2.6.2",
3334
"@parcel/core": "2.6.2",
3435
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
3536
"@types/http-proxy": "^1.17.7",

packages/gatsby/src/utils/parcel/__tests__/__snapshots__/compile-gatsby-files.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`gatsby file compilation constructBundler should construct Parcel relative to passed directory 1`] = `
44
Object {
5+
"cache": undefined,
56
"cacheDir": "<PROJECT_ROOT>/packages/gatsby/src/utils/parcel/__tests__/fixtures/js/.cache/.parcel-cache",
67
"defaultConfig": "<PROJECT_ROOT>/packages/gatsby-parcel-config/lib/index.json",
78
"entries": Array [

packages/gatsby/src/utils/parcel/compile-gatsby-files.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Parcel } from "@parcel/core"
2+
import { LMDBCache, Cache } from "@parcel/cache"
23
import path from "path"
34
import type { Diagnostic } from "@parcel/diagnostic"
45
import reporter from "gatsby-cli/lib/reporter"
@@ -27,14 +28,15 @@ function exponentialBackoff(retry: number): Promise<void> {
2728
* Construct Parcel with config.
2829
* @see {@link https://parceljs.org/features/targets/}
2930
*/
30-
export function constructParcel(siteRoot: string): Parcel {
31+
export function constructParcel(siteRoot: string, cache?: Cache): Parcel {
3132
return new Parcel({
3233
entries: [
3334
`${siteRoot}/${gatsbyFileRegex}`,
3435
`${siteRoot}/plugins/**/${gatsbyFileRegex}`,
3536
],
3637
defaultConfig: require.resolve(`gatsby-parcel-config`),
3738
mode: `production`,
39+
cache,
3840
targets: {
3941
root: {
4042
outputFormat: `commonjs`,
@@ -100,8 +102,22 @@ export async function compileGatsbyFiles(
100102

101103
await exponentialBackoff(retry)
102104

103-
const parcel = constructParcel(siteRoot)
105+
// for whatever reason TS thinks LMDBCache is some browser Cache and not actually Parcel's Cache
106+
// so we force type it to Parcel's Cache
107+
const cache = new LMDBCache(getCacheDir(siteRoot)) as unknown as Cache
108+
const parcel = constructParcel(siteRoot, cache)
104109
const { bundleGraph } = await parcel.run()
110+
let cacheClosePromise = Promise.resolve()
111+
try {
112+
// @ts-ignore store is public field on LMDBCache class, but public interface for Cache
113+
// doesn't have it. There doesn't seem to be proper public API for this, so we have to
114+
// resort to reaching into internals. Just in case this is wrapped in try/catch if
115+
// parcel changes internals in future (closing cache is only needed when retrying
116+
// so the if the change happens we shouldn't fail on happy builds)
117+
cacheClosePromise = cache.store.close()
118+
} catch (e) {
119+
reporter.verbose(`Failed to close parcel cache\n${e.toString()}`)
120+
}
105121

106122
await exponentialBackoff(retry)
107123

@@ -138,8 +154,15 @@ export async function compileGatsbyFiles(
138154
)
139155
}
140156

141-
// sometimes parcel cache gets in weird state
142-
await remove(getCacheDir(siteRoot))
157+
// sometimes parcel cache gets in weird state and we need to clear the cache
158+
await cacheClosePromise
159+
160+
try {
161+
await remove(getCacheDir(siteRoot))
162+
} catch {
163+
// in windows we might get "EBUSY" errors if LMDB failed to close, so this try/catch is
164+
// to prevent EBUSY errors from potentially hiding real import errors
165+
}
143166

144167
await compileGatsbyFiles(siteRoot, retry + 1)
145168
return

0 commit comments

Comments
 (0)