From 039f2cc1b9c0ad4f04e59b37134baa8afb3749d9 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 30 Mar 2022 19:53:43 +0200 Subject: [PATCH] fix(gatsby): use gatsby root instead of process.cwd (#35263) --- packages/gatsby/src/utils/__tests__/cache.ts | 13 +++++++++++++ packages/gatsby/src/utils/cache.ts | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) 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 {