Skip to content

Commit 24b9619

Browse files
committed
[v2][bugfix] Patching shebangs of the bundled dependencies
Some npm dependencies are bundled, meaning their node_modules directory is contained as part of their parent tgz distribution. Like a regular package, a bundled package can contain some executable scripts. That being said, these exec scripts won't be listed in the `bin` section of their parent's package-lock.json file. This is to be expected, those are transitive dependencies, they're not meant to be listed there. When patching a package through a source_override, we look whether or not a node_modules directory is present. If it's there, it means we potentially have some unpatched shebangs in these transitive dependencies. We recurse in them using "patchShebangs".
1 parent 991dba3 commit 24b9619

File tree

5 files changed

+939
-0
lines changed

5 files changed

+939
-0
lines changed

internal-v2.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ rec {
173173
patchShebangs $bin
174174
fi
175175
done
176+
if [[ -d node_modules ]]; then
177+
# Patching shebangs of the bundled dependencies
178+
patchShebangs node_modules
179+
fi
176180
}
177181
178182
prepare_links_for_npm_preinstall

tests/tests-v2/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
build = callPackage ./build.nix { };
44
build-tests = callPackage ./build-tests.nix { };
5+
bundle-tests = callPackage ./examples-projects/bundled-dep-require-patch-shebang { };
56
integration-tests = callPackage ./integration-tests { };
67
patch-package = callPackage ./patch-package.nix { };
78
make-url-source = callPackage ./make-url-source.nix { };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{ npmlock2nix, testLib, runCommand, nodejs-16_x, nodejs-17_x, python3 }:
2+
npmlock2nix.v2.node_modules {
3+
src = ./.;
4+
sourceOverrides = with npmlock2nix.v2.node_modules; {
5+
"ganache" = packageRequirePatchShebangs;
6+
};
7+
postCheck = ''
8+
echo "[+] Checking if the bundled binary shebang has been correctly patched."
9+
if cat "node_modules/ganache/node_modules/node-gyp-build/bin.js" | head -1 | grep -c "/usr/bin/env"
10+
then
11+
echo "ERROR: the bundled node-gyp-build/bin.js file's shebang has not been patched."
12+
exit 1
13+
else
14+
echo "[+] Ok"
15+
fi
16+
'';
17+
}

0 commit comments

Comments
 (0)