-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Corepack plugin | ||
|
||
This plugin sets up [Corepack](https://github.com/nodejs/corepack/), a NodeJS feature to automatically install the correct version of Yarn or PNPM. | ||
|
||
When using corepack, running `pnpm` or `yarn` will always use the correct version as defined in the `packageManager` field of the project's `package.json` file. | ||
|
||
## Use and activation | ||
|
||
1. Add this plugin to the `include` section of your `devbox.json`: | ||
|
||
```json | ||
{ | ||
"include": ["github:cultureamp/devbox-extras?dir=plugins/corepack"] | ||
} | ||
``` | ||
|
||
2. Ensure your `package.json` sets the `packageManager` field: | ||
|
||
```json | ||
{ | ||
"packageManager": "[email protected]" | ||
} | ||
``` | ||
|
||
or | ||
|
||
```json | ||
{ | ||
"packageManager": "[email protected]" | ||
} | ||
``` | ||
|
||
## Motivation | ||
|
||
There’s a subtle but messy issue that can arise using Devbox, Node.js, and Yarn/PNPM. If you install node and yarn as documented you will end up with two different versions of node: | ||
|
||
```sh | ||
$ devbox add [email protected] yarn | ||
... | ||
$ devbox run node --version | ||
v20.8.0 | ||
$ devbox run yarn node --version | ||
yarn node v1.22.19 | ||
v18.18.0 | ||
``` | ||
|
||
This happens because nix bundles all dependencies into every package, yarn is getting it’s own copy of Node.js, which may be more recent than the Node.js version the project is using. | ||
|
||
Corepack by default tries to install shim scripts for both `yarn` and `pnpm` in the same directory that `node` is found in. Unfortunately when using devbox, the `node` binary is in the Nix store, which is read only, so Corepack fails to set up correctly. | ||
|
||
This plugin works by: | ||
|
||
- creating a folder in `.devbox/virtenv/pnpm_plugin/bin/` | ||
- running `corepack enable --install-directory "./devbox/virtenv/pnpm_plugin/bin/"`, which adds the `pnpm` and `yarn` shim scripts | ||
- and then adding `./devbox/virtenv/pnpm_plugin/bin/` to the `PATH` environment variable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "corepack", | ||
"version": "0.0.1", | ||
"readme": "README.md", | ||
"env": { | ||
"PATH": "{{ .Virtenv }}/bin/:$PATH" | ||
}, | ||
"shell": { | ||
"init_hook": [ | ||
"mkdir -p {{ .Virtenv }}/bin", | ||
"corepack enable --install-directory \"{{ .Virtenv }}/bin/\"" | ||
] | ||
} | ||
} |