Skip to content

Commit

Permalink
fetch-yarn-deps: add yarn-berry support
Browse files Browse the repository at this point in the history
A new argument has been added to fetch-yarn-deps:
`yarnVersion` which will default to `1` which uses
the original `yarn`.

For newer `yarn-berry` `yarn.lock`, one can set the
`yarnVersion` to either `3` or `4` which will use
yarn-berry3 or yarn-berry4 respectively.

The difference is the `yarn.lock` file, which follows
a different format, depending on the version.

This also adds the corresponding tests.

The added support for yarn.lock files > version 1 has been
inspired by @szlend from
#254369 (comment)

fixes #254369

Signed-off-by: Florian Brandes <[email protected]>
Co-authored-by: Doron Behar <[email protected]>
  • Loading branch information
gador and doronbehar committed Nov 12, 2024
1 parent 6b69441 commit 379aa01
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 7 deletions.
83 changes: 76 additions & 7 deletions pkgs/build-support/node/fetch-yarn-deps/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
prefetch-yarn-deps,
fixup-yarn-lock,
yarn,
yarn-berry3,
yarn-berry4,
makeSetupHook,
cacert,
callPackage,
Expand Down Expand Up @@ -104,6 +106,7 @@ in
src ? null,
hash ? "",
sha256 ? "",
yarnVersion ? 1,
...
}@args:
let
Expand Down Expand Up @@ -132,22 +135,66 @@ in
dontInstall = true;

nativeBuildInputs = [
prefetch-yarn-deps
(
{
"1" = prefetch-yarn-deps;
"3" = yarn-berry3;
"4" = yarn-berry4;
}
.${builtins.toString yarnVersion}
)
cacert
];
GIT_SSL_CAINFO = "${cacert}/etc/ssl/certs/ca-bundle.crt";
NODE_EXTRA_CA_CERTS = "${cacert}/etc/ssl/certs/ca-bundle.crt";

buildPhase = ''
runHook preBuild
supportedArchitectures = builtins.toJSON {
os = [
"darwin"
"linux"
];
cpu = [
"arm"
"arm64"
"ia32"
"x64"
];
libc = [
"glibc"
"musl"
];
};

yarnLock=''${yarnLock:=$PWD/yarn.lock}
mkdir -p $out
(cd $out; prefetch-yarn-deps --verbose --builder $yarnLock)
configurePhase = lib.optionalString (yarnVersion > 1) ''
runHook preConfigure
runHook postBuild
export HOME="$NIX_BUILD_TOP"
export YARN_ENABLE_TELEMETRY=0
yarn config set enableGlobalCache false
yarn config set cacheFolder $out
yarn config set supportedArchitectures --json "$supportedArchitectures"
runHook postConfigure
'';

buildPhase =
''
runHook preBuild
mkdir -p $out
''
+ lib.optionalString (yarnVersion > 1) ''
yarn install --immutable --mode skip-build
''
+ lib.optionalString (yarnVersion == 1) ''
yarnLock=''${yarnLock:=$PWD/yarn.lock}
(cd $out; prefetch-yarn-deps --verbose --builder $yarnLock)
''
+ ''
runHook postBuild
'';

outputHashMode = "recursive";
}
// hash_
Expand All @@ -174,6 +221,28 @@ in
};
} ./yarn-config-hook.sh;

yarnBerry3ConfigHook = makeSetupHook {
name = "yarn-config-hook";
propagatedBuildInputs = [ yarn-berry3 ];
substitutions = {
yarn = lib.getExe yarn-berry3;
};
meta = {
description = "Install nodejs dependencies from an offline yarn berry cache (version 3)";
};
} ./yarn-berry-config-hook.sh;

yarnBerry4ConfigHook = makeSetupHook {
name = "yarn-config-hook";
propagatedBuildInputs = [ yarn-berry4 ];
substitutions = {
yarn = lib.getExe yarn-berry4;
};
meta = {
description = "Install nodejs dependencies from an offline yarn berry cache (version 4)";
};
} ./yarn-berry-config-hook.sh;

yarnBuildHook = makeSetupHook {
name = "yarn-build-hook";
meta = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "yarn-testing",
"packageManager": "[email protected]",
"dependencies": {
"lit-html": "^3.2.1"
}
}
30 changes: 30 additions & 0 deletions pkgs/build-support/node/fetch-yarn-deps/tests/berry_3/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 6
cacheKey: 10c0

"@types/trusted-types@npm:^2.0.2":
version: 2.0.7
resolution: "@types/trusted-types@npm:2.0.7"
checksum: 4c4855f10de7c6c135e0d32ce462419d8abbbc33713b31d294596c0cc34ae1fa6112a2f9da729c8f7a20707782b0d69da3b1f8df6645b0366d08825ca1522e0c
languageName: node
linkType: hard

"lit-html@npm:^3.2.1":
version: 3.2.1
resolution: "lit-html@npm:3.2.1"
dependencies:
"@types/trusted-types": "npm:^2.0.2"
checksum: 31c02df2148bf9a73545570cbe57aae01c4de1d9b44060b6ff13641837d38e39e6b1abcf92e13882cc84f5fee37605ed79602b91ad479728549014462808118e
languageName: node
linkType: hard

"yarn-testing@workspace:.":
version: 0.0.0-use.local
resolution: "yarn-testing@workspace:."
dependencies:
lit-html: ^3.2.1
languageName: unknown
linkType: soft
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "yarn-testing",
"packageManager": "[email protected]",
"dependencies": {
"lit-html": "^3.2.1"
}
}
30 changes: 30 additions & 0 deletions pkgs/build-support/node/fetch-yarn-deps/tests/berry_4/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10c0

"@types/trusted-types@npm:^2.0.2":
version: 2.0.7
resolution: "@types/trusted-types@npm:2.0.7"
checksum: 10c0/4c4855f10de7c6c135e0d32ce462419d8abbbc33713b31d294596c0cc34ae1fa6112a2f9da729c8f7a20707782b0d69da3b1f8df6645b0366d08825ca1522e0c
languageName: node
linkType: hard

"lit-html@npm:^3.2.1":
version: 3.2.1
resolution: "lit-html@npm:3.2.1"
dependencies:
"@types/trusted-types": "npm:^2.0.2"
checksum: 10c0/31c02df2148bf9a73545570cbe57aae01c4de1d9b44060b6ff13641837d38e39e6b1abcf92e13882cc84f5fee37605ed79602b91ad479728549014462808118e
languageName: node
linkType: hard

"yarn-testing@workspace:.":
version: 0.0.0-use.local
resolution: "yarn-testing@workspace:."
dependencies:
lit-html: "npm:^3.2.1"
languageName: unknown
linkType: soft
10 changes: 10 additions & 0 deletions pkgs/build-support/node/fetch-yarn-deps/tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@
yarnLock = ./giturl.lock;
sha256 = "sha256-VPnyqN6lePQZGXwR7VhbFnP7/0/LB621RZwT1F+KzVQ=";
};
berry_3 = testers.invalidateFetcherByDrvHash fetchYarnDeps {
src = ./berry_3;
yarnVersion = 3;
sha256 = "sha256-eiVOpRHuAS7zb6JpCzS3TIHbqQU+wCrMItzZGrE6Fbo=";
};
berry_4 = testers.invalidateFetcherByDrvHash fetchYarnDeps {
src = ./berry_4;
yarnVersion = 4;
sha256 = "sha256-Eq37qKn5P7rkufJBhME0OG/3BsWlCsFYVnWHP5uVhAQ=";
};
}
30 changes: 30 additions & 0 deletions pkgs/build-support/node/fetch-yarn-deps/yarn-berry-config-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
yarnBerryConfigHook(){
echo "Executing yarnBerryConfigHook"

# Use a constant HOME directory
mkdir -p /tmp/home
export HOME=/tmp/home
if [[ -n "$yarnOfflineCache" ]]; then
offlineCache="$yarnOfflineCache"
fi
if [[ -z "$offlineCache" ]]; then
echo yarnConfigHook: No yarnOfflineCache or offlineCache were defined\! >&2
exit 2
fi

export YARN_ENABLE_TELEMETRY=0
yarn config set enableGlobalCache false
yarn config set enableScripts false
yarn config set cacheFolder "$offlineCache"

yarn install --immutable --immutable-cache

# TODO: Check if this is really needed
patchShebangs node_modules

echo "finished yarnConfigHook"
}

if [[ -z "${dontYarnInstallDeps-}" ]]; then
postConfigureHooks+=(yarnBerryConfigHook)
fi
1 change: 1 addition & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ with pkgs;
prefetch-yarn-deps
yarnConfigHook
yarnBerry3ConfigHook
yarnBerry4ConfigHook
yarnBuildHook
yarnInstallHook
fetchYarnDeps;
Expand Down

0 comments on commit 379aa01

Please sign in to comment.