Use a copy of the cache to avoid issues

This commit is contained in:
Stéphan Kochen
2020-08-24 20:31:05 +02:00
parent 68cca6c0c0
commit 111da50bee
2 changed files with 35 additions and 18 deletions
+14 -4
View File
@@ -23,6 +23,7 @@ import projectExprTmpl from "./yarn-project.nix.in";
const generate = async (project: Project, report: Report) => { const generate = async (project: Project, report: Report) => {
const { configuration, cwd } = project; const { configuration, cwd } = project;
const yarnPathAbs = configuration.get(`yarnPath`); const yarnPathAbs = configuration.get(`yarnPath`);
const cacheFolderAbs = configuration.get(`cacheFolder`);
const lockfileFilename = configuration.get(`lockfileFilename`); const lockfileFilename = configuration.get(`lockfileFilename`);
// TODO: Should try to remove this. Our binary wrappers currently do // TODO: Should try to remove this. Our binary wrappers currently do
@@ -40,7 +41,16 @@ const generate = async (project: Project, report: Report) => {
yarnPath = yarnPathAbs; yarnPath = yarnPathAbs;
report.reportWarning( report.reportWarning(
0, 0,
`The Yarn path ${yarnPathAbs} is outside the project directory - it cannot be reached by the Nix build` `The Yarn path ${yarnPathAbs} is outside the project - it may not be reachable by the Nix build`
);
}
let cacheFolder = ppath.relative(cwd, cacheFolderAbs);
if (cacheFolder.startsWith(`../`)) {
cacheFolder = cacheFolderAbs;
report.reportWarning(
0,
`The cache folder ${cacheFolderAbs} is outside the project - it may not be reachable by the Nix build`
); );
} }
@@ -74,7 +84,7 @@ const generate = async (project: Project, report: Report) => {
if (inputPath !== yarnPath) { if (inputPath !== yarnPath) {
report.reportWarning( report.reportWarning(
0, 0,
`The path ${inputPath} is outside the project directory and was ignored - it may not be reachable in the Nix build` `The path ${inputPath} is outside the project and was ignored - it may not be reachable in the Nix build`
); );
} }
continue; continue;
@@ -91,7 +101,6 @@ const generate = async (project: Project, report: Report) => {
// Build the Nix output-hash by hashing the Yarn cache folder. The // Build the Nix output-hash by hashing the Yarn cache folder. The
// derivation should build the exact same. // derivation should build the exact same.
const cacheFolder = configuration.get(`cacheFolder`);
let cacheHash = ``; let cacheHash = ``;
try { try {
const hasherResult = await execUtils.execvp( const hasherResult = await execUtils.execvp(
@@ -116,7 +125,8 @@ const generate = async (project: Project, report: Report) => {
const projectName = ident ? structUtils.stringifyIdent(ident) : `workspace`; const projectName = ident ? structUtils.stringifyIdent(ident) : `workspace`;
const projectExpr = projectExprTmpl const projectExpr = projectExprTmpl
.replace(`@@PROJECT_NAME@@`, JSON.stringify(projectName)) .replace(`@@PROJECT_NAME@@`, JSON.stringify(projectName))
.replace(`@@OFFLINE_CACHE_HASH@@`, JSON.stringify(cacheHash)) .replace(`@@CACHE_FOLDER@@`, JSON.stringify(cacheFolder))
.replace(`@@CACHE_HASH@@`, JSON.stringify(cacheHash))
.replace(`@@YARN_PATH@@`, JSON.stringify(yarnPath)) .replace(`@@YARN_PATH@@`, JSON.stringify(yarnPath))
.replace( .replace(
`@@YARN_CLOSURE_ENTRIES@@`, `@@YARN_CLOSURE_ENTRIES@@`,
+21 -14
View File
@@ -9,7 +9,8 @@ let
# Variables provided by the generator. # Variables provided by the generator.
project-name = @@PROJECT_NAME@@; project-name = @@PROJECT_NAME@@;
offline-cache-hash = @@OFFLINE_CACHE_HASH@@; cache-folder = @@CACHE_FOLDER@@;
cache-hash = @@CACHE_HASH@@;
yarn-path = @@YARN_PATH@@; yarn-path = @@YARN_PATH@@;
yarn-closure-entries = @@YARN_CLOSURE_ENTRIES@@; yarn-closure-entries = @@YARN_CLOSURE_ENTRIES@@;
@@ -30,13 +31,13 @@ let
elem "${type}:${srcRel path}" yarn-closure-entries; elem "${type}:${srcRel path}" yarn-closure-entries;
}; };
# Build just the offline cache for the project. # Build just the cache for the project.
offline-cache = stdenv.mkDerivation { offline-cache = stdenv.mkDerivation {
name = "${project-name}-offline-cache"; name = "${project-name}-offline-cache";
buildInputs = [ nodejs ]; buildInputs = [ nodejs ];
builder = writeText "builder.sh" '' builder = writeText "builder.sh" ''
source $stdenv/setup source $stdenv/setup
cd ${yarn-closure} cd '${yarn-closure}'
# Yarn may need a writable home directory for the global cache mirror. # Yarn may need a writable home directory for the global cache mirror.
# TODO: Can't disable the mirror, because it changes cache filenames. # TODO: Can't disable the mirror, because it changes cache filenames.
@@ -51,7 +52,7 @@ let
''; '';
outputHashMode = "recursive"; outputHashMode = "recursive";
outputHashAlgo = "sha256"; outputHashAlgo = "sha256";
outputHash = offline-cache-hash; outputHash = cache-hash;
}; };
in stdenv.mkDerivation { in stdenv.mkDerivation {
@@ -72,18 +73,15 @@ in stdenv.mkDerivation {
configurePhase = '' configurePhase = ''
runHook preConfigure runHook preConfigure
# Move the entire project to the output directory. # Copy over the Yarn cache.
# TODO: Would rather do this in 'installPhase', # TODO: Can we do without the copy somehow?
# but '.pnp.js' is generated with relative paths. rm -fr '${cache-folder}'
mkdir -p $out/libexec $out/bin mkdir -p "$(dirname '${cache-folder}')"
mv $PWD "$out/libexec/$sourceRoot" cp -r '${offline-cache}' '${cache-folder}'
cd "$out/libexec/$sourceRoot" chmod -R u+w '${cache-folder}'
# Store the absolute path to Yarn for the 'yarn' alias. # Store the absolute path to Yarn for the 'yarn' alias.
export NIX_YARN_PATH="$(readlink -f ${yarn-path})" export NIX_YARN_PATH="$(readlink -f '${yarn-path}')"
# Point Yarn to the offline cache built separately.
yarn config set cacheFolder '${offline-cache}'
# Run normal Yarn install to complete dependency installation. # Run normal Yarn install to complete dependency installation.
yarn install --immutable --immutable-cache yarn install --immutable --immutable-cache
@@ -99,6 +97,15 @@ in stdenv.mkDerivation {
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
mkdir -p $out/libexec $out/bin
# Move the entire project to the output directory.
mv $PWD "$out/libexec/$sourceRoot"
cd "$out/libexec/$sourceRoot"
# Update the path to Yarn.
export NIX_YARN_PATH="$(readlink -f '${yarn-path}')"
# Invoke a plugin internal command to setup binaries. # Invoke a plugin internal command to setup binaries.
yarn nixify install-bin $out/bin yarn nixify install-bin $out/bin