diff --git a/packages/gatsby/src/utils/__tests__/cache.ts b/packages/gatsby/src/utils/__tests__/cache.ts index 586528ad9bcef..2a26d456a05d6 100644 --- a/packages/gatsby/src/utils/__tests__/cache.ts +++ b/packages/gatsby/src/utils/__tests__/cache.ts @@ -1,3 +1,5 @@ +import path from "path" +import os from "os" import Cache from "../cache" import fs from "fs-extra" import manager from "cache-manager" @@ -94,6 +96,17 @@ describe(`cache`, () => { expect(cache.get).toEqual(expect.any(Function)) expect(cache.set).toEqual(expect.any(Function)) }) + + it(`should use root directory`, () => { + const name = `__TEST_CACHE_NAME__` + global.__GATSBY = { root: os.tmpdir() } + getCache({ name }) + delete global.__GATSBY + + expect(fs.ensureDirSync).toHaveBeenCalledWith( + path.join(os.tmpdir(), `.cache`, `caches`, name) + ) + }) }) describe(`get/set`, () => { diff --git a/packages/gatsby/src/utils/cache.ts b/packages/gatsby/src/utils/cache.ts index 442a414374dad..040a340a48fe5 100644 --- a/packages/gatsby/src/utils/cache.ts +++ b/packages/gatsby/src/utils/cache.ts @@ -29,7 +29,12 @@ export default class GatsbyCache { constructor({ name = `db`, store = fsStore }: ICacheProperties = {}) { this.name = name this.store = store - this.directory = path.join(process.cwd(), `.cache/caches/${name}`) + this.directory = path.join( + global.__GATSBY?.root ?? process.cwd(), + `.cache`, + `caches`, + name + ) } init(): GatsbyCache {