diff --git a/sources/@roots/bud-support/src/utilities/filesystem.ts b/sources/@roots/bud-support/src/utilities/filesystem.ts index eb60e840a7..8414c5a82f 100644 --- a/sources/@roots/bud-support/src/utilities/filesystem.ts +++ b/sources/@roots/bud-support/src/utilities/filesystem.ts @@ -1,5 +1,8 @@ +import {isMainThread} from 'node:worker_threads' + import {BudError} from '../errors/errors.js' import {Filesystem} from '../filesystem/index.js' +import {paths} from './paths.js' let filesystem: Filesystem @@ -13,6 +16,16 @@ export const get = (basedir?: string) => { filesystem = new Filesystem(basedir) + /** + * change directory to basedir for process.cwd() to work as expected + * + * @remarks + * - no need to do this if the basedir is the same as the cwd + * - workers don't support process.chdir + */ + const modifiedBaseDirectory = paths.basedir !== process.cwd() + if (isMainThread && modifiedBaseDirectory) process.chdir(basedir) + return filesystem } diff --git a/sources/@roots/bud-support/src/utilities/paths.ts b/sources/@roots/bud-support/src/utilities/paths.ts index 83fbd9a0a3..4462c0627e 100644 --- a/sources/@roots/bud-support/src/utilities/paths.ts +++ b/sources/@roots/bud-support/src/utilities/paths.ts @@ -102,4 +102,4 @@ This is most likely a problem with the internals of bud.js.`, return paths } -export {get} +export {get, paths}