Skip to content

Commit

Permalink
fix(gatsby): close parcel cache db before clearing cache and retrying (
Browse files Browse the repository at this point in the history
…#36377)

Co-authored-by: Dan Kirkham <[email protected]>
Co-authored-by: Lennart <[email protected]>
Co-authored-by: Ty Hopp <[email protected]>
  • Loading branch information
4 people authored Aug 18, 2022
1 parent 977a211 commit 9d737b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@graphql-tools/load": "^7.5.10",
"@jridgewell/trace-mapping": "^0.3.13",
"@nodelib/fs.walk": "^1.2.8",
"@parcel/cache": "2.6.2",
"@parcel/core": "2.6.2",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
"@types/http-proxy": "^1.17.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`gatsby file compilation constructBundler should construct Parcel relative to passed directory 1`] = `
Object {
"cache": undefined,
"cacheDir": "<PROJECT_ROOT>/packages/gatsby/src/utils/parcel/__tests__/fixtures/js/.cache/.parcel-cache",
"defaultConfig": "<PROJECT_ROOT>/packages/gatsby-parcel-config/lib/index.json",
"entries": Array [
Expand Down
31 changes: 27 additions & 4 deletions packages/gatsby/src/utils/parcel/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Parcel } from "@parcel/core"
import { LMDBCache, Cache } from "@parcel/cache"
import path from "path"
import type { Diagnostic } from "@parcel/diagnostic"
import reporter from "gatsby-cli/lib/reporter"
Expand Down Expand Up @@ -27,14 +28,15 @@ function exponentialBackoff(retry: number): Promise<void> {
* Construct Parcel with config.
* @see {@link https://parceljs.org/features/targets/}
*/
export function constructParcel(siteRoot: string): Parcel {
export function constructParcel(siteRoot: string, cache?: Cache): Parcel {
return new Parcel({
entries: [
`${siteRoot}/${gatsbyFileRegex}`,
`${siteRoot}/plugins/**/${gatsbyFileRegex}`,
],
defaultConfig: require.resolve(`gatsby-parcel-config`),
mode: `production`,
cache,
targets: {
root: {
outputFormat: `commonjs`,
Expand Down Expand Up @@ -100,8 +102,22 @@ export async function compileGatsbyFiles(

await exponentialBackoff(retry)

const parcel = constructParcel(siteRoot)
// for whatever reason TS thinks LMDBCache is some browser Cache and not actually Parcel's Cache
// so we force type it to Parcel's Cache
const cache = new LMDBCache(getCacheDir(siteRoot)) as unknown as Cache
const parcel = constructParcel(siteRoot, cache)
const { bundleGraph } = await parcel.run()
let cacheClosePromise = Promise.resolve()
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()
} catch (e) {
reporter.verbose(`Failed to close parcel cache\n${e.toString()}`)
}

await exponentialBackoff(retry)

Expand Down Expand Up @@ -138,8 +154,15 @@ export async function compileGatsbyFiles(
)
}

// sometimes parcel cache gets in weird state
await remove(getCacheDir(siteRoot))
// sometimes parcel cache gets in weird state and we need to clear the cache
await cacheClosePromise

try {
await remove(getCacheDir(siteRoot))
} catch {
// in windows we might get "EBUSY" errors if LMDB failed to close, so this try/catch is
// to prevent EBUSY errors from potentially hiding real import errors
}

await compileGatsbyFiles(siteRoot, retry + 1)
return
Expand Down

0 comments on commit 9d737b6

Please sign in to comment.