From a5824ae591db7381f2a39d1dc95ab8f13441d4bd Mon Sep 17 00:00:00 2001 From: Kelly Mears Date: Thu, 18 May 2023 11:53:57 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20improve(@roots/bud):=20set=20proces?= =?UTF-8?q?s.cwd=20if=20--cwd=20flag=20is=20used=20(#2273)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tools like `tailwindcss` use `process.cwd()` for pathing. This change sets the `process.cwd()` using `process.chdir` if the `--cwd` flag is used. This causes errors if run in a worker thread so we check that we aren't in one before executing. This matters for us internally because vitest is threaded. Thankfully this change isn't needed for any of our tests to pass. Without this change: ``` ❯ yarn bud build --cwd examples/tailwindcss [@examples/tailwindcss] › ✖ warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration. [@examples/tailwindcss] › ✖ warn - https://tailwindcss.com/docs/content-configuration [stderr] warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration. [stderr] warn - https://tailwindcss.com/docs/content-configuration ╭─ @examples/tailwindcss ./dist [deb12d010346cc2dc6b1] │ ├─ entrypoints │ └─ app │ ├─ css/app.css 5.23 kB │ └─ js/app.js 69 bytes │ ├─ assets │ └─ index.html 389 bytes │ ╰─ compiled 11 modules (0 cached) in 1s 718ms ╭─ HtmlWebpackCompiler ./dist [41b35a5af5068146a6ea] │ ╰─ compiled 5 modules (0 cached) in 46ms ``` With it: ``` ❯ yarn bud build --cwd examples/tailwindcss ╭─ @examples/tailwindcss ./dist [4bfbbebc9bac287cfe34] │ ├─ entrypoints │ └─ app │ ├─ css/app.css 5.53 kB │ └─ js/app.js 69 bytes │ ├─ assets │ └─ index.html 389 bytes │ ╰─ compiled 4 modules (4 cached) in 234ms ╭─ HtmlWebpackCompiler ./dist [41b35a5af5068146a6ea] │ ╰─ compiled 5 modules (5 cached) in 147ms ``` ## Type of change **PATCH: backwards compatible change** --- .../@roots/bud-support/src/utilities/filesystem.ts | 13 +++++++++++++ sources/@roots/bud-support/src/utilities/paths.ts | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) 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}