Skip to content

Commit

Permalink
pnpm.fetchDeps: multiple workspaces; better pnpmInstallFlags (#350751)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrumplex authored Oct 24, 2024
2 parents 7ce2e38 + 42fb646 commit 98c6c17
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
31 changes: 25 additions & 6 deletions doc/languages-frameworks/javascript.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,26 @@ NOTE: It is highly recommended to use a pinned version of pnpm (i.e. `pnpm_8` or
In case you are patching `package.json` or `pnpm-lock.yaml`, make sure to pass `finalAttrs.patches` to the function as well (i.e. `inherit (finalAttrs) patches`.
`pnpm.configHook` supports adding additional `pnpm install` flags via `pnpmInstallFlags` which can be set to a Nix string array.
`pnpm.configHook` supports adding additional `pnpm install` flags via `pnpmInstallFlags` which can be set to a Nix string array:
```nix
{
pnpm,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "foo";
version = "0-unstable-1980-01-01";
src = ...;
pnpmInstallFlags = [ "--shamefully-hoist" ];
pnpmDeps = pnpm.fetchDeps {
inherit (finalAttrs) pnpmInstallFlags;
};
})
```
#### Dealing with `sourceRoot` {#javascript-pnpm-sourceRoot}
Expand Down Expand Up @@ -459,24 +478,24 @@ Assuming the following directory structure, we can define `sourceRoot` and `pnpm
#### PNPM Workspaces {#javascript-pnpm-workspaces}
If you need to use a PNPM workspace for your project, then set `pnpmWorkspace = "<workspace project name>"` in your `pnpm.fetchDeps` call,
which will make PNPM only install dependencies for that workspace package.
If you need to use a PNPM workspace for your project, then set `pnpmWorkspaces = [ "<workspace project name 1>" "<workspace project name 2>" ]`, etc, in your `pnpm.fetchDeps` call,
which will make PNPM only install dependencies for those workspace packages.
For example:
```nix
...
pnpmWorkspace = "@astrojs/language-server";
pnpmWorkspaces = [ "@astrojs/language-server" ];
pnpmDeps = pnpm.fetchDeps {
inherit (finalAttrs) pnpmWorkspace;
inherit (finalAttrs) pnpmWorkspaces;
...
}
```
The above would make `pnpm.fetchDeps` call only install dependencies for the `@astrojs/language-server` workspace package.
Note that you do not need to set `sourceRoot` to make this work.
Usually in such cases, you'd want to use `pnpm --filter=$pnpmWorkspace build` to build your project, as `npmHooks.npmBuildHook` probably won't work. A `buildPhase` based on the following example will probably fit most workspace projects:
Usually in such cases, you'd want to use `pnpm --filter=<pnpm workspace name> build` to build your project, as `npmHooks.npmBuildHook` probably won't work. A `buildPhase` based on the following example will probably fit most workspace projects:
```nix
buildPhase = ''
Expand Down
4 changes: 2 additions & 2 deletions pkgs/by-name/as/astro-language-server/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ stdenv.mkDerivation (finalAttrs: {
pname
version
src
pnpmWorkspace
pnpmWorkspaces
prePnpmInstall
;
hash = "sha256-/X8ZoWK5kBPm/8clBDP+B9A5ofXnH2svmy4kMc2t5iA=";
Expand All @@ -37,7 +37,7 @@ stdenv.mkDerivation (finalAttrs: {

# Must specify to download "@astrojs/yaml2ts" depencendies
# https://pnpm.io/filtering#--filter-package_name-1
pnpmWorkspace = "@astrojs/language-server...";
pnpmWorkspaces = [ "@astrojs/language-server..." ];
prePnpmInstall = ''
# Warning section for "pnpm@v8"
# https://pnpm.io/cli/install#--filter-package_selector
Expand Down
4 changes: 2 additions & 2 deletions pkgs/by-name/ba/bash-language-server/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ stdenv.mkDerivation (finalAttrs: {
hash = "sha256-yJ81oGd9aNsWQMLvDSgMVVH1//Mw/SVFYFIPsJTQYzE=";
};

pnpmWorkspace = "bash-language-server";
pnpmWorkspaces = [ "bash-language-server" ];
pnpmDeps = pnpm_8.fetchDeps {
inherit (finalAttrs) pname version src pnpmWorkspace;
inherit (finalAttrs) pname version src pnpmWorkspaces;
hash = "sha256-W25xehcxncBs9QgQBt17F5YHK0b+GDEmt27XzTkyYWg=";
};

Expand Down
14 changes: 11 additions & 3 deletions pkgs/development/tools/pnpm/fetch-deps/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
{
hash ? "",
pname,
pnpmWorkspace ? "",
pnpmWorkspaces ? [ ],
prePnpmInstall ? "",
pnpmInstallFlags ? [ ],
...
}@args:
let
Expand All @@ -32,8 +33,14 @@
outputHash = "";
outputHashAlgo = "sha256";
};
installFlags = lib.optionalString (pnpmWorkspace != "") "--filter=${pnpmWorkspace}";

filterFlags = lib.map (package: "--filter=${package}") pnpmWorkspaces;
in
# pnpmWorkspace was deprecated, so throw if it's used.
assert (lib.throwIf (args ? pnpmWorkspace)
"pnpm.fetchDeps: `pnpmWorkspace` is no longer supported, please migrate to `pnpmWorkspaces`."
) true;

stdenvNoCC.mkDerivation (
finalAttrs:
(
Expand Down Expand Up @@ -73,7 +80,8 @@
pnpm install \
--force \
--ignore-scripts \
${installFlags} \
${lib.escapeShellArgs filterFlags} \
${lib.escapeShellArgs pnpmInstallFlags} \
--frozen-lockfile
runHook postInstall
Expand Down
14 changes: 11 additions & 3 deletions pkgs/development/tools/pnpm/fetch-deps/pnpm-config-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ pnpmConfigHook() {

pnpm config set store-dir "$STORE_PATH"

echo "Installing dependencies"

if [[ -n "$pnpmWorkspace" ]]; then
pnpmInstallFlags+=("--filter=$pnpmWorkspace")
echo "'pnpmWorkspace' is deprecated, please migrate to 'pnpmWorkspaces'."
exit 2
fi

echo "Installing dependencies"
if [[ -n "$pnpmWorkspaces" ]]; then
local IFS=" "
for ws in $pnpmWorkspaces; do
pnpmInstallFlags+=("--filter=$ws")
done
fi

runHook prePnpmInstall

pnpm install \
Expand Down

0 comments on commit 98c6c17

Please sign in to comment.