Skip to content

Commit

Permalink
✨ improve(@roots/bud): set process.cwd if --cwd flag is used (#2273)
Browse files Browse the repository at this point in the history
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**
  • Loading branch information
kellymears authored May 18, 2023
1 parent afd35e2 commit a5824ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions sources/@roots/bud-support/src/utilities/filesystem.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/bud-support/src/utilities/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ This is most likely a problem with the internals of bud.js.`,
return paths
}

export {get}
export {get, paths}

0 comments on commit a5824ae

Please sign in to comment.