From 3fd4c14d3683baac8d1f94286ae14fe160888b51 Mon Sep 17 00:00:00 2001 From: DavHau Date: Thu, 1 Aug 2024 22:40:22 +0700 Subject: [PATCH] nodejsLockUtils: fix computing parent path for workspace packages --- lib/internal/nodejsLockUtils.nix | 19 +++++++----- .../test_nodejs_lockutils/default.nix | 29 +++++++++---------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/internal/nodejsLockUtils.nix b/lib/internal/nodejsLockUtils.nix index a82cdfea6e..0f14260a39 100644 --- a/lib/internal/nodejsLockUtils.nix +++ b/lib/internal/nodejsLockUtils.nix @@ -1,11 +1,15 @@ {lib, ...}: let # path = node_modules/@org/lib/node_modules/bar - stripPath = path: let - split = lib.splitString "node_modules/" path; # = [ "@org/lib" "bar" ] - suffix = "node_modules/${lib.last split}"; # = "node_modules/bar" - nextPath = lib.removeSuffix suffix path; # = "node_modules/@org/lib/node_modules/bar"; + parentPath = path: let + # noPackages = lib.removePrefix "packages/" path; + packages = lib.splitString "node_modules/" path; # = [ "@org/lib" "bar" ] + nextPath = lib.concatStringsSep "node_modules/" (lib.init packages); in - lib.removeSuffix "/" nextPath; + lib.removeSuffix "/" ( + if path == nextPath + then "" + else nextPath + ); findEntry = # = "attrs" @@ -22,7 +26,8 @@ searchPath else if currentPath == "" then throw "${search} not found in package-lock.json." - else findEntry packageLock (stripPath currentPath) search; + # if the package cannot be found as a sub-dependency, check the parent + else findEntry packageLock (parentPath currentPath) search; in { - inherit findEntry stripPath; + inherit findEntry parentPath; } diff --git a/tests/nix-unit/test_nodejs_lockutils/default.nix b/tests/nix-unit/test_nodejs_lockutils/default.nix index 9c5ce37ba8..7fe338b66a 100644 --- a/tests/nix-unit/test_nodejs_lockutils/default.nix +++ b/tests/nix-unit/test_nodejs_lockutils/default.nix @@ -4,35 +4,32 @@ nodejsLockUtils ? import ../../../lib/internal/nodejsLockUtils.nix {inherit lib;}, }: { # test the path strip function - test_nodejsLockUtils_stripPath_simple = let - nextPath = nodejsLockUtils.stripPath "node_modules/@org/lib/node_modules/bar"; - in { - expr = nextPath; + test_nodejsLockUtils_parentPath_simple = { + expr = nodejsLockUtils.parentPath "node_modules/@org/lib/node_modules/bar"; expected = "node_modules/@org/lib"; }; - test_nodejsLockUtils_stripPath_root = let - nextPath = nodejsLockUtils.stripPath "node_modules/bar"; - in { - expr = nextPath; + test_nodejsLockUtils_parentPath_root = { + expr = nodejsLockUtils.parentPath "node_modules/bar"; # The root expected = ""; }; - test_nodejsLockUtils_stripPath_empty = let - nextPath = nodejsLockUtils.stripPath ""; - in { - expr = nextPath; + test_nodejsLockUtils_parentPath_empty = { + expr = nodejsLockUtils.parentPath ""; expected = ""; }; - test_nodejsLockUtils_stripPath_complex = let - nextPath = nodejsLockUtils.stripPath "node_modules/@org/lib/node_modules/bar/node_modules/foo"; - in { - expr = nextPath; + test_nodejsLockUtils_parentPath_complex = { + expr = nodejsLockUtils.parentPath "node_modules/@org/lib/node_modules/bar/node_modules/foo"; expected = "node_modules/@org/lib/node_modules/bar"; }; + test_nodejsLockUtils_parentPath_local_package = { + expr = nodejsLockUtils.parentPath "packages/foo"; + expected = ""; + }; + # test the resolve function test_nodejsLockUtils_findEntry_argparse = let plock = builtins.fromJSON (builtins.readFile ./package-lock.json);