Skip to content

Commit

Permalink
fix(gatsby): don't rely on process.cwd() in lambda for ssr/dsg, inste…
Browse files Browse the repository at this point in the history
…ad infer root of 'gatsby' through relative path from entry module and set process.cwd() based on that so rest of utils relying on process.cwd() continue to work in monorepo scenarios
  • Loading branch information
pieh committed Jun 4, 2024
1 parent a3aa898 commit 6efd4bb
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/gatsby/src/utils/page-ssr-module/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ const cdnDatastorePath = `%CDN_DATASTORE_PATH%`
const cdnDatastoreOrigin = `%CDN_DATASTORE_ORIGIN%`
const PATH_PREFIX = `%PATH_PREFIX%`

// this file should be in `.cache/page-ssr-module/lambda.js`
// so getting `.cache` location should be one directory above
// directory of this file
const cacheDir = path.join(__dirname, `..`)

// gatsby relies on process.cwd() a lot, so this ensures that CWD is set correctly
// in relation to bundled files for lambda. In some scenarios those files are not in
// expected relative locations to CWD, so here we are forcing setting CWD so the
// relative paths are correct
process.chdir(path.join(cacheDir, `..`))

function setupFsWrapper(): string {
// setup global._fsWrapper
try {
fs.accessSync(__filename, fs.constants.W_OK)
// TODO: this seems funky - not sure if this is correct way to handle this, so just marking TODO to revisit this
return path.join(__dirname, `..`, `data`, `datastore`)
return path.join(cacheDir, `data`, `datastore`)
} catch (e) {
// we are in a read-only filesystem, so we need to use a temp dir

Expand All @@ -31,9 +41,6 @@ function setupFsWrapper(): string {

global.__GATSBY.root = TEMP_DIR

// TODO: don't hardcode this
const cacheDir = `${process.cwd()}/.cache`

// we need to rewrite fs
const rewrites = [
[path.join(cacheDir, `caches`), path.join(TEMP_CACHE_DIR, `caches`)],
Expand Down

0 comments on commit 6efd4bb

Please sign in to comment.