From 10a8f5a83b61644db344cc38e9441cf71ccb79fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Mon, 23 Jan 2023 14:55:05 +0100 Subject: [PATCH] More relative path fixes --- src/generate.ts | 57 +++++++++++++++++++++++------------- src/tmpl/yarn-project.nix.in | 8 ++--- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/generate.ts b/src/generate.ts index 614357a..bf5b088 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -45,14 +45,16 @@ export default async ( return; } - // Sanity checks. - const yarnPathAbs = configuration.get(`yarnPath`); + // Determine relative paths for Nix path literals. const nixExprPath = configuration.get(`nixExprPath`); - let yarnPath: PortablePath; + + const yarnPathAbs = configuration.get(`yarnPath`); + let yarnPathExpr: string; if (yarnPathAbs.startsWith(cwd)) { - yarnPath = ppath.relative(ppath.dirname(nixExprPath), yarnPathAbs); + yarnPathExpr = + "./" + ppath.relative(ppath.dirname(nixExprPath), yarnPathAbs); } else { - yarnPath = yarnPathAbs; + yarnPathExpr = json(yarnPathAbs); report.reportWarning( 0, `The Yarn path ${yarnPathAbs} is outside the project - it may not be reachable by the Nix build` @@ -60,29 +62,42 @@ export default async ( } const cacheFolderAbs = configuration.get(`cacheFolder`); - let cacheFolder = ppath.relative(cwd, cacheFolderAbs); - if (cacheFolder.startsWith(`../`)) { - cacheFolder = cacheFolderAbs; + let cacheFolderExpr: string; + if (cacheFolderAbs.startsWith(cwd)) { + cacheFolderExpr = + "./" + ppath.relative(ppath.dirname(nixExprPath), cacheFolderAbs); + } else { + cacheFolderExpr = json(cacheFolderAbs); report.reportWarning( 0, `The cache folder ${cacheFolderAbs} is outside the project - it may not be reachable by the Nix build` ); } - for (const source of configuration.sources.values()) { - if (!source.startsWith(`<`)) { - const relativeSource = ppath.relative(cwd, source as PortablePath); - if (relativeSource.startsWith(`../`)) { - report.reportWarning( - 0, - `The config file ${source} is outside the project - it may not be reachable by the Nix build` - ); + const configSources = new Set(); + for (const sourceList of configuration.sources.values()) { + for (const source of sourceList.split(", ")) { + if (!source.startsWith(`<`)) { + configSources.add(source); } } } + for (const source of configSources) { + const relativeSource = ppath.resolve(cwd, source as PortablePath); + if (!relativeSource.startsWith(cwd)) { + report.reportWarning( + 0, + `The config file ${source} is outside the project - it may not be reachable by the Nix build` + ); + } + } - // Determine relative paths for Nix path literals. - const lockfileFilename = configuration.get(`lockfileFilename`); + const lockfileExpr = + "./" + + ppath.relative( + ppath.dirname(nixExprPath), + ppath.resolve(cwd, configuration.get(`lockfileFilename`)) + ); // Build a list of cache entries so Nix can fetch them. // TODO: See if we can use Nix fetchurl for npm: dependencies. @@ -278,9 +293,9 @@ export default async ( const projectName = ident ? structUtils.stringifyIdent(ident) : `workspace`; const projectExpr = renderTmpl(projectExprTmpl, { PROJECT_NAME: json(projectName), - YARN_PATH: yarnPath, - LOCKFILE: lockfileFilename, - CACHE_FOLDER: json(cacheFolder), + YARN_PATH: yarnPathExpr, + LOCKFILE: lockfileExpr, + CACHE_FOLDER: cacheFolderExpr, CACHE_ENTRIES: cacheEntriesCode, ISOLATED: isolatedCode.join("\n"), ISOLATED_INTEGRATION: indent(" ", isolatedIntegration.join("\n")), diff --git a/src/tmpl/yarn-project.nix.in b/src/tmpl/yarn-project.nix.in index 97cb349..c1d54d3 100644 --- a/src/tmpl/yarn-project.nix.in +++ b/src/tmpl/yarn-project.nix.in @@ -7,9 +7,7 @@ let cacheFolder = @@CACHE_FOLDER@@; -#@@ IF NEED_ISOLATED_BUILD_SUPPRORT - lockfile = ./@@LOCKFILE@@; -#@@ ENDIF NEED_ISOLATED_BUILD_SUPPRORT + lockfile = @@LOCKFILE@@; # Call overrideAttrs on a derivation if a function is provided. optionalOverride = fn: drv: @@ -17,7 +15,7 @@ let # Simple stub that provides the global yarn command. yarn = writeShellScriptBin "yarn" '' - exec '${nodejs}/bin/node' '${./@@YARN_PATH@@}' "$@" + exec '${nodejs}/bin/node' '${@@YARN_PATH@@}' "$@" ''; # Common attributes between Yarn derivations. @@ -164,7 +162,7 @@ let mkdir -p "$out/libexec/$name" tar xzvf package.tgz --directory "$out/libexec/$name" --strip-components=1 - cp .yarnrc* '@@LOCKFILE@@' "$out/libexec/$name" + cp .yarnrc* ${lockfile} "$out/libexec/$name" cp --recursive .yarn "$out/libexec/$name" # If the project uses the node-modules linker, then