Yarn v4 support

This commit is contained in:
Stéphan Kochen
2023-10-29 13:41:40 +01:00
parent a1ab08efae
commit ee8c72b3a9
8 changed files with 2202 additions and 2696 deletions
+3
View File
@@ -22,6 +22,9 @@ jobs:
with: with:
node-version: 18.x node-version: 18.x
- name: Enable corepack
run: corepack enable
- name: Yarn cache - name: Yarn cache
uses: actions/cache@v3 uses: actions/cache@v3
with: with:
+1 -3
View File
@@ -1,6 +1,4 @@
/.yarn/* /.yarn
!/.yarn/releases
!/.yarn/plugins
/.pnp.* /.pnp.*
/dist/yarn-plugin-nixify.dev.js /dist/yarn-plugin-nixify.dev.js
-823
View File
File diff suppressed because one or more lines are too long
-1
View File
@@ -1 +0,0 @@
yarnPath: .yarn/releases/yarn-3.3.1.cjs
+10 -33
View File
@@ -8,47 +8,24 @@
"build-dev": "./build.js", "build-dev": "./build.js",
"fmt": "prettier --write src" "fmt": "prettier --write src"
}, },
"packageManager": "yarn@3.3.1", "packageManager": "yarn@4.0.1",
"devDependencies": { "devDependencies": {
"@babel/core": "^7.11.4", "@babel/core": "^7.11.4",
"@babel/plugin-proposal-class-properties": "^7.10.4", "@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-proposal-decorators": "^7.10.5", "@babel/plugin-proposal-decorators": "^7.10.5",
"@babel/preset-typescript": "^7.10.4", "@babel/preset-typescript": "^7.10.4",
"@types/node": "10", "@types/node": "10",
"@yarnpkg/cli": "^3.0.0", "@yarnpkg/cli": "^4.0.0",
"@yarnpkg/core": "^3.0.0", "@yarnpkg/core": "^4.0.0",
"@yarnpkg/fslib": "^2.5.1", "@yarnpkg/fslib": "^3.0.0",
"@yarnpkg/plugin-patch": "^3.0.0", "@yarnpkg/plugin-patch": "^4.0.0",
"@yarnpkg/plugin-pnp": "^3.1.0", "@yarnpkg/plugin-pnp": "^4.0.0",
"babel-loader": "^8.1.0", "babel-loader": "^9.1.3",
"clipanion": "^3.0.0", "clipanion": "^4.0.0-rc.2",
"prettier": "3.0.3", "prettier": "3.0.3",
"raw-loader": "^4.0.1", "raw-loader": "^4.0.1",
"typescript": "4.5.4", "typescript": "5.2.2",
"webpack": "^5.35.0", "webpack": "^5.35.0",
"webpack-sources": "^2.2.0" "webpack-sources": "^3.2.3"
},
"resolutions": {
"@yarnpkg/cli": "3.1.0",
"@yarnpkg/core": "3.1.0",
"@yarnpkg/fslib": "2.5.1",
"@yarnpkg/libzip": "2.2.2",
"@yarnpkg/plugin-compat": "3.0.0",
"@yarnpkg/plugin-dlx": "3.0.0",
"@yarnpkg/plugin-essentials": "3.0.0",
"@yarnpkg/plugin-file": "2.2.1",
"@yarnpkg/plugin-git": "2.4.0",
"@yarnpkg/plugin-github": "2.2.0",
"@yarnpkg/plugin-http": "2.1.3",
"@yarnpkg/plugin-init": "3.0.0",
"@yarnpkg/plugin-link": "2.1.2",
"@yarnpkg/plugin-node-modules": "2.3.0",
"@yarnpkg/plugin-npm": "2.5.0",
"@yarnpkg/plugin-npm-cli": "3.0.0",
"@yarnpkg/plugin-pack": "3.0.0",
"@yarnpkg/plugin-patch": "3.0.0",
"@yarnpkg/plugin-pnp": "3.1.0",
"@yarnpkg/plugin-stage": "3.0.0",
"@yarnpkg/shell": "3.0.0"
} }
} }
+44 -15
View File
@@ -1,13 +1,11 @@
import { tmpdir } from "os";
import { Filename, npath, PortablePath, ppath, xfs } from "@yarnpkg/fslib"; import { Filename, npath, PortablePath, ppath, xfs } from "@yarnpkg/fslib";
import { patchUtils } from "@yarnpkg/plugin-patch"; import { patchUtils } from "@yarnpkg/plugin-patch";
import {
computeFixedOutputStorePath,
sanitizeDerivationName,
} from "./nixUtils";
import { json, indent, renderTmpl, upperCamelize } from "./textUtils"; import { json, indent, renderTmpl, upperCamelize } from "./textUtils";
import { import {
Cache, Cache,
YarnVersion,
execUtils, execUtils,
hashUtils, hashUtils,
InstallMode, InstallMode,
@@ -20,7 +18,12 @@ import {
import defaultExprTmpl from "./tmpl/default.nix.in"; import defaultExprTmpl from "./tmpl/default.nix.in";
import projectExprTmpl from "./tmpl/yarn-project.nix.in"; import projectExprTmpl from "./tmpl/yarn-project.nix.in";
import { tmpdir } from "os"; import {
computeFixedOutputStorePath,
sanitizeDerivationName,
} from "./nixUtils";
const isYarn3 = YarnVersion?.startsWith("3.") || false;
// Generator function that runs after `yarn install`. // Generator function that runs after `yarn install`.
export default async ( export default async (
@@ -49,12 +52,24 @@ export default async (
const nixExprPath = configuration.get(`nixExprPath`); const nixExprPath = configuration.get(`nixExprPath`);
const yarnPathAbs = configuration.get(`yarnPath`); const yarnPathAbs = configuration.get(`yarnPath`);
let yarnPathExpr: string; let yarnBinExpr: string;
if (yarnPathAbs.startsWith(cwd)) { if (yarnPathAbs === null) {
yarnPathExpr = // Assume the current running script is the correct Yarn.
const hexHash = await hashUtils.checksumFile(
process.argv[1] as PortablePath,
);
const sriHash = "sha512-" + Buffer.from(hexHash, "hex").toString("base64");
yarnBinExpr = [
"fetchurl {",
` url = "https://repo.yarnpkg.com/${YarnVersion!}/packages/yarnpkg-cli/bin/yarn.js";`,
` hash = "${sriHash}";`,
"}",
].join("\n ");
} else if (yarnPathAbs.startsWith(cwd)) {
yarnBinExpr =
"./" + ppath.relative(ppath.dirname(nixExprPath), yarnPathAbs); "./" + ppath.relative(ppath.dirname(nixExprPath), yarnPathAbs);
} else { } else {
yarnPathExpr = json(yarnPathAbs); yarnBinExpr = json(yarnPathAbs);
report.reportWarning( report.reportWarning(
0, 0,
`The Yarn path ${yarnPathAbs} is outside the project - it may not be reachable by the Nix build`, `The Yarn path ${yarnPathAbs} is outside the project - it may not be reachable by the Nix build`,
@@ -65,6 +80,8 @@ export default async (
let cacheFolderExpr: string; let cacheFolderExpr: string;
if (cacheFolderAbs.startsWith(cwd)) { if (cacheFolderAbs.startsWith(cwd)) {
cacheFolderExpr = json(ppath.relative(cwd, cacheFolderAbs)); cacheFolderExpr = json(ppath.relative(cwd, cacheFolderAbs));
} else if (!isYarn3 && configuration.get(`enableGlobalCache`)) {
cacheFolderExpr = '".yarn/cache"';
} else { } else {
throw Error( throw Error(
`The cache folder ${cacheFolderAbs} is outside the project, this is currently not supported`, `The cache folder ${cacheFolderAbs} is outside the project, this is currently not supported`,
@@ -99,6 +116,7 @@ export default async (
// Build a list of cache entries so Nix can fetch them. // Build a list of cache entries so Nix can fetch them.
// TODO: See if we can use Nix fetchurl for npm: dependencies. // TODO: See if we can use Nix fetchurl for npm: dependencies.
interface CacheEntry { interface CacheEntry {
originalFilename: Filename;
filename: Filename; filename: Filename;
sha512: string; sha512: string;
} }
@@ -109,7 +127,9 @@ export default async (
for (const pkg of project.storedPackages.values()) { for (const pkg of project.storedPackages.values()) {
const { locatorHash } = pkg; const { locatorHash } = pkg;
const checksum = project.storedChecksums.get(locatorHash); const checksum = project.storedChecksums.get(locatorHash);
const cachePath = cache.getLocatorPath(pkg, checksum || null, cacheOptions); const cachePath = isYarn3
? (cache as any).getLocatorPath(pkg, checksum || null, cacheOptions)
: cache.getLocatorPath(pkg, checksum || null);
if (!cachePath) continue; if (!cachePath) continue;
const filename = ppath.basename(cachePath); const filename = ppath.basename(cachePath);
@@ -119,7 +139,13 @@ export default async (
const sha512 = checksum const sha512 = checksum
? checksum.split(`/`).pop()! ? checksum.split(`/`).pop()!
: await hashUtils.checksumFile(cachePath); : await hashUtils.checksumFile(cachePath);
cacheEntries.set(locatorStr, { filename, sha512 }); cacheEntries.set(locatorStr, {
originalFilename: filename,
// Rebuild the filename, because the cache file we're operating on may be
// from the mirror directory, which uses different naming.
filename: cache.getChecksumFilename(pkg, sha512),
sha512,
});
} }
let cacheEntriesCode = `cacheEntries = {\n`; let cacheEntriesCode = `cacheEntries = {\n`;
@@ -290,7 +316,7 @@ export default async (
const projectName = ident ? structUtils.stringifyIdent(ident) : `workspace`; const projectName = ident ? structUtils.stringifyIdent(ident) : `workspace`;
const projectExpr = renderTmpl(projectExprTmpl, { const projectExpr = renderTmpl(projectExprTmpl, {
PROJECT_NAME: json(projectName), PROJECT_NAME: json(projectName),
YARN_PATH: yarnPathExpr, YARN_BIN: yarnBinExpr,
LOCKFILE: lockfileExpr, LOCKFILE: lockfileExpr,
CACHE_FOLDER: cacheFolderExpr, CACHE_FOLDER: cacheFolderExpr,
CACHE_ENTRIES: cacheEntriesCode, CACHE_ENTRIES: cacheEntriesCode,
@@ -321,7 +347,10 @@ export default async (
) { ) {
await xfs.mktempPromise(async (tempDir) => { await xfs.mktempPromise(async (tempDir) => {
const toPreload: PortablePath[] = []; const toPreload: PortablePath[] = [];
for (const [locator, { filename, sha512 }] of cacheEntries.entries()) { for (const [
locator,
{ originalFilename, sha512 },
] of cacheEntries.entries()) {
const name = sanitizeDerivationName(locator); const name = sanitizeDerivationName(locator);
// Check to see if the Nix store entry already exists. // Check to see if the Nix store entry already exists.
const hash = Buffer.from(sha512, "hex"); const hash = Buffer.from(sha512, "hex");
@@ -336,7 +365,7 @@ export default async (
const subdir = ppath.join(tempDir, sha512.slice(0, 7) as Filename); const subdir = ppath.join(tempDir, sha512.slice(0, 7) as Filename);
await xfs.mkdirPromise(subdir); await xfs.mkdirPromise(subdir);
const src = ppath.join(cache.cwd, filename); const src = ppath.join(cache.cwd, originalFilename);
const dst = ppath.join(subdir, name as Filename); const dst = ppath.join(subdir, name as Filename);
await xfs.copyFilePromise(src, dst); await xfs.copyFilePromise(src, dst);
+11 -3
View File
@@ -6,6 +6,8 @@
let let
yarnBin = @@YARN_BIN@@;
cacheFolder = @@CACHE_FOLDER@@; cacheFolder = @@CACHE_FOLDER@@;
lockfile = @@LOCKFILE@@; lockfile = @@LOCKFILE@@;
@@ -15,7 +17,7 @@ let
# Simple stub that provides the global yarn command. # Simple stub that provides the global yarn command.
yarn = writeShellScriptBin "yarn" '' yarn = writeShellScriptBin "yarn" ''
exec '${nodejs}/bin/node' '${@@YARN_PATH@@}' "$@" exec '${nodejs}/bin/node' '${yarnBin}' "$@"
''; '';
# Common attributes between Yarn derivations. # Common attributes between Yarn derivations.
@@ -42,7 +44,7 @@ let
source $stdenv/setup source $stdenv/setup
cd "$src" cd "$src"
${buildVars} ${buildVars}
HOME="$TMP" yarn_cache_folder="$TMP" \ HOME="$TMP" yarn_enable_global_cache=false yarn_cache_folder="$TMP" \
yarn nixify fetch-one $locator yarn nixify fetch-one $locator
# Because we change the cache dir, Yarn may generate a different name. # Because we change the cache dir, Yarn may generate a different name.
mv "$TMP/$(sed 's/-[^-]*\.[^-]*$//' <<< "$outputFilename")"-* $out mv "$TMP/$(sed 's/-[^-]*\.[^-]*$//' <<< "$outputFilename")"-* $out
@@ -89,7 +91,8 @@ let
echo '{ "dependencies": { "${pname}": "${reference}" } }' > package.json echo '{ "dependencies": { "${pname}": "${reference}" } }' > package.json
install -m 0600 ${lockfile} ./yarn.lock install -m 0600 ${lockfile} ./yarn.lock
export yarn_global_folder="$TMP" export yarn_global_folder="$TMP"
export YARN_ENABLE_IMMUTABLE_INSTALLS=false export yarn_enable_global_cache=false
export yarn_enable_immutable_installs=false
yarn --immutable-cache yarn --immutable-cache
''; '';
@@ -123,6 +126,11 @@ let
# Yarn may need a writable home directory. # Yarn may need a writable home directory.
export yarn_global_folder="$TMP" export yarn_global_folder="$TMP"
# Ensure global cache is disabled. Cache must be part of our output.
touch .yarnrc.yml
sed -i -e '/^enableGlobalCache/d' .yarnrc.yml
echo 'enableGlobalCache: false' >> .yarnrc.yml
# Some node-gyp calls may call out to npm, which could fail due to an # Some node-gyp calls may call out to npm, which could fail due to an
# read-only home dir. # read-only home dir.
export HOME="$TMP" export HOME="$TMP"
+2133 -1818
View File
File diff suppressed because it is too large Load Diff