Skip to content

Commit c2fa762

Browse files
edgarnansenpieh
authored andcommitted
fix(core): empty cache if delete fails [docker] (#11454)
## Description Attempts to empty the `.cache/` directory if deleting it fails. Deletion might fail if for example the directory is a mountpoint for a docker volume. ## Related Issues This fixes #11097 Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent 07086d1 commit c2fa762

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/gatsby/src/bootstrap/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,12 @@ module.exports = async (args: BootstrapArgs) => {
175175
data
176176
`)
177177
}
178-
178+
const cacheDirectory = `${program.directory}/.cache`
179179
if (!oldPluginsHash || pluginsHash !== oldPluginsHash) {
180180
try {
181-
await fs.remove(`${program.directory}/.cache`)
181+
// Attempt to empty dir if remove fails,
182+
// like when directory is mount point
183+
await fs.remove(cacheDirectory).catch(() => fs.emptyDir(cacheDirectory))
182184
} catch (e) {
183185
report.error(`Failed to remove .cache files.`, e)
184186
}
@@ -197,7 +199,7 @@ module.exports = async (args: BootstrapArgs) => {
197199

198200
// Now that we know the .cache directory is safe, initialize the cache
199201
// directory.
200-
await fs.ensureDir(`${program.directory}/.cache`)
202+
await fs.ensureDir(cacheDirectory)
201203

202204
// Ensure the public/static directory
203205
await fs.ensureDir(`${program.directory}/public/static`)
@@ -213,7 +215,7 @@ module.exports = async (args: BootstrapArgs) => {
213215
parentSpan: bootstrapSpan,
214216
})
215217
activity.start()
216-
const dbSaveFile = `${program.directory}/.cache/loki/loki.db`
218+
const dbSaveFile = `${cacheDirectory}/loki/loki.db`
217219
try {
218220
await loki.start({
219221
saveFile: dbSaveFile,
@@ -236,7 +238,7 @@ module.exports = async (args: BootstrapArgs) => {
236238
})
237239
activity.start()
238240
const srcDir = `${__dirname}/../../cache-dir`
239-
const siteDir = `${program.directory}/.cache`
241+
const siteDir = cacheDirectory
240242
const tryRequire = `${__dirname}/../utils/test-require-error.js`
241243
try {
242244
await fs.copy(srcDir, siteDir, {
@@ -245,12 +247,12 @@ module.exports = async (args: BootstrapArgs) => {
245247
await fs.copy(tryRequire, `${siteDir}/test-require-error.js`, {
246248
clobber: true,
247249
})
248-
await fs.ensureDirSync(`${program.directory}/.cache/json`)
250+
await fs.ensureDirSync(`${cacheDirectory}/json`)
249251

250252
// Ensure .cache/fragments exists and is empty. We want fragments to be
251253
// added on every run in response to data as fragments can only be added if
252254
// the data used to create the schema they're dependent on is available.
253-
await fs.emptyDir(`${program.directory}/.cache/fragments`)
255+
await fs.emptyDir(`${cacheDirectory}/fragments`)
254256
} catch (err) {
255257
report.panic(`Unable to copy site files to .cache`, err)
256258
}

0 commit comments

Comments
 (0)