Skip to content

Commit

Permalink
Add corepack plugin
Browse files Browse the repository at this point in the history
The motivation is listed in the README.
  • Loading branch information
jasononeil committed Feb 23, 2024
1 parent c82cfc2 commit 4e1ba77
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
55 changes: 55 additions & 0 deletions plugins/corepack/README.md
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.
14 changes: 14 additions & 0 deletions plugins/corepack/plugin.json
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/\""
]
}
}

0 comments on commit 4e1ba77

Please sign in to comment.