From ce7dc2be92499f15b4b0315bfca3ee9d61fc3c5e Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Tue, 9 Jul 2024 20:06:08 -0700 Subject: feat(node): Support executing npm package lifecycle scripts (preinstall/install/postinstall) (#24487) Adds support for running npm package lifecycle scripts, opted into via a new `--allow-scripts` flag. With this PR, when running `deno cache` (or `DENO_FUTURE=1 deno install`) you can specify the `--allow-scripts=pkg1,pkg2` flag to run lifecycle scripts attached to the given packages. Note at the moment this only works when `nodeModulesDir` is true (using the local resolver). When a package with un-run lifecycle scripts is encountered, we emit a warning suggesting things may not work and to try running lifecycle scripts. Additionally, if a package script implicitly requires `node-gyp` and it's not found on the system, we emit a warning. Extra things in this PR: - Extracted out bits of `task.rs` into a separate module for reuse - Added a couple fields to `process.config` in order to support `node-gyp` (it relies on a few variables being there) - Drive by fix to downloading new npm packages to test registry --- TODO: - [x] validation for allow-scripts args (make sure it looks like an npm package) - [x] make allow-scripts matching smarter - [ ] figure out what issues this closes --- Review notes: - This adds a bunch of deps to our test registry due to using `node-gyp`, so it's pretty noisy --- tests/specs/npm/lifecycle_scripts/__test__.jsonc | 92 ++++++++++++++++++++++ .../specs/npm/lifecycle_scripts/all_lifecycles.js | 3 + .../specs/npm/lifecycle_scripts/all_lifecycles.out | 14 ++++ .../lifecycle_scripts/all_lifecycles_not_run.out | 12 +++ .../specs/npm/lifecycle_scripts/conflicting_bin.js | 4 + .../npm/lifecycle_scripts/conflicting_bin.out | 13 +++ tests/specs/npm/lifecycle_scripts/deno.json | 3 + .../future_install_all_lifecycles.out | 16 ++++ .../future_install_all_lifecycles_not_run.out | 13 +++ .../npm/lifecycle_scripts/implicit_node_gyp.js | 3 + tests/specs/npm/lifecycle_scripts/main.js | 3 + .../npm/lifecycle_scripts/node_gyp_not_found.out | 8 ++ .../npm/lifecycle_scripts/node_gyp_not_run.out | 9 +++ 13 files changed, 193 insertions(+) create mode 100644 tests/specs/npm/lifecycle_scripts/__test__.jsonc create mode 100644 tests/specs/npm/lifecycle_scripts/all_lifecycles.js create mode 100644 tests/specs/npm/lifecycle_scripts/all_lifecycles.out create mode 100644 tests/specs/npm/lifecycle_scripts/all_lifecycles_not_run.out create mode 100644 tests/specs/npm/lifecycle_scripts/conflicting_bin.js create mode 100644 tests/specs/npm/lifecycle_scripts/conflicting_bin.out create mode 100644 tests/specs/npm/lifecycle_scripts/deno.json create mode 100644 tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles.out create mode 100644 tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles_not_run.out create mode 100644 tests/specs/npm/lifecycle_scripts/implicit_node_gyp.js create mode 100644 tests/specs/npm/lifecycle_scripts/main.js create mode 100644 tests/specs/npm/lifecycle_scripts/node_gyp_not_found.out create mode 100644 tests/specs/npm/lifecycle_scripts/node_gyp_not_run.out (limited to 'tests/specs') diff --git a/tests/specs/npm/lifecycle_scripts/__test__.jsonc b/tests/specs/npm/lifecycle_scripts/__test__.jsonc new file mode 100644 index 000000000..fa60e7963 --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/__test__.jsonc @@ -0,0 +1,92 @@ +{ + "tests": { + "node_gyp": { + "tempDir": true, + "steps": [ + { + "args": "cache --allow-scripts=npm:@denotest/node-addon main.js", + "output": "[WILDCARD]gyp info ok \n" + }, + { + "args": "run -A main.js", + "output": "world\n" + } + ] + }, + "run_without_scripts": { + "tempDir": true, + "steps": [ + { + "args": "run -A main.js", + "output": "node_gyp_not_run.out", + "exitCode": 1 + } + ] + }, + "implicit_node_gyp": { + "tempDir": true, + "steps": [ + { + "envs": { + // I don't think this will work on windows + "PATH": "" + }, + "args": "cache --allow-scripts implicit_node_gyp.js", + "output": "node_gyp_not_found.out", + "exitCode": 1 + } + ] + }, + "lifecycle_scripts": { + "tempDir": true, + "steps": [ + { + // without running scripts (should warn) + "args": "cache all_lifecycles.js", + "output": "all_lifecycles_not_run.out" + }, + { + // now run scripts + "args": "cache --allow-scripts all_lifecycles.js", + // this test package covers running preinstall, install, and postinstall scripts + // it also exercises running bin packages (esbuild in this case), using `node` in a script + // (with and without node-only CLI flags), and using `npx` in a script + "output": "all_lifecycles.out" + } + ] + }, + "future_install_lifecycle_scripts": { + "tempDir": true, + "envs": { + "DENO_FUTURE": "1" + }, + "steps": [ + { + "args": [ + "eval", + "Deno.writeTextFileSync('package.json', '{\"dependencies\":{ \"@denotest/node-lifecycle-scripts\": \"*\" } }')" + ], + "output": "" + }, + { + "args": "install", + "output": "future_install_all_lifecycles_not_run.out" + } + ] + }, + "lifecycle_scripts_conflicting_bin": { + "tempDir": true, + "steps": [ + { + // we import @denotest/says-hello-on-install, which executes `say-hello` from `@denotest/say-hello` in its + // install script. we also import `@denotest/better-say-hello`, which provides a + // bin called `say-hello` as well. `@denotest/says-hello-on-install` wins (gets put into + // node_modules/.bin/say-hello) because it's closer in the dependency tree, but when + // we run the install script, we should use the correct binary (relative to the package) + "args": "cache --allow-scripts conflicting_bin.js", + "output": "conflicting_bin.out" + } + ] + } + } +} diff --git a/tests/specs/npm/lifecycle_scripts/all_lifecycles.js b/tests/specs/npm/lifecycle_scripts/all_lifecycles.js new file mode 100644 index 000000000..e9fb2cf9c --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/all_lifecycles.js @@ -0,0 +1,3 @@ +import { value } from "npm:@denotest/node-lifecycle-scripts"; + +console.log(`value is ${value}`); diff --git a/tests/specs/npm/lifecycle_scripts/all_lifecycles.out b/tests/specs/npm/lifecycle_scripts/all_lifecycles.out new file mode 100644 index 000000000..956c006dd --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/all_lifecycles.out @@ -0,0 +1,14 @@ +preinstall +deno preinstall.js +node preinstall.js +install +hello from install script +postinstall[WILDCARD] + _____________ +< postinstall > + ------------- + \ ^__^ + \ (oo)\_______ + (__)\ )\/\ + ||----w | + || || diff --git a/tests/specs/npm/lifecycle_scripts/all_lifecycles_not_run.out b/tests/specs/npm/lifecycle_scripts/all_lifecycles_not_run.out new file mode 100644 index 000000000..cc42cddee --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/all_lifecycles_not_run.out @@ -0,0 +1,12 @@ +[UNORDERED_START] +Download http://localhost:4260/@denotest/node-lifecycle-scripts +Download http://localhost:4260/@denotest/bin +Download http://localhost:4260/@denotest/node-lifecycle-scripts/1.0.0.tgz +Download http://localhost:4260/@denotest/bin/1.0.0.tgz +Initialize @denotest/node-lifecycle-scripts@1.0.0 +Initialize @denotest/bin@1.0.0 +[UNORDERED_END] +warning: Packages contained npm lifecycle scripts (preinstall/install/postinstall) that were not executed. + This may cause the packages to not work correctly. To run them, use the `--allow-scripts` flag with `deno cache` + (e.g. `deno cache --allow-scripts=pkg1,pkg2 `): + @denotest/node-lifecycle-scripts@1.0.0 diff --git a/tests/specs/npm/lifecycle_scripts/conflicting_bin.js b/tests/specs/npm/lifecycle_scripts/conflicting_bin.js new file mode 100644 index 000000000..fbce3fcdd --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/conflicting_bin.js @@ -0,0 +1,4 @@ +import { sayBetterHello } from "npm:@denotest/better-say-hello@1.0.0"; +import { sayHelloOnInstall } from "npm:@denotest/say-hello-on-install@1.0.0"; +sayBetterHello(); +sayHelloOnInstall(); diff --git a/tests/specs/npm/lifecycle_scripts/conflicting_bin.out b/tests/specs/npm/lifecycle_scripts/conflicting_bin.out new file mode 100644 index 000000000..c70aafdd5 --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/conflicting_bin.out @@ -0,0 +1,13 @@ +[UNORDERED_START] +Download http://localhost:4260/@denotest/better-say-hello +Download http://localhost:4260/@denotest/say-hello-on-install +Download http://localhost:4260/@denotest/say-hello +Download http://localhost:4260/@denotest/better-say-hello/1.0.0.tgz +Download http://localhost:4260/@denotest/say-hello-on-install/1.0.0.tgz +Download http://localhost:4260/@denotest/say-hello/1.0.0.tgz +Initialize @denotest/better-say-hello@1.0.0 +Initialize @denotest/say-hello-on-install@1.0.0 +Initialize @denotest/say-hello@1.0.0 +[UNORDERED_END] +install script +@denotest/say-hello says hello! diff --git a/tests/specs/npm/lifecycle_scripts/deno.json b/tests/specs/npm/lifecycle_scripts/deno.json new file mode 100644 index 000000000..176354f98 --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/deno.json @@ -0,0 +1,3 @@ +{ + "nodeModulesDir": true +} diff --git a/tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles.out b/tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles.out new file mode 100644 index 000000000..059afdfaa --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles.out @@ -0,0 +1,16 @@ +⚠️ `deno install` behavior will change in Deno 2. To preserve the current behavior use the `-g` or `--global` flag. +preinstall +deno preinstall.js +node preinstall.js +install +hello from install script +postinstall +[WILDCARD] + _____________ +< postinstall > + ------------- + \ ^__^ + \ (oo)\_______ + (__)\ )\/\ + ||----w | + || || diff --git a/tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles_not_run.out b/tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles_not_run.out new file mode 100644 index 000000000..e7033229b --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/future_install_all_lifecycles_not_run.out @@ -0,0 +1,13 @@ +⚠️ `deno install` behavior will change in Deno 2. To preserve the current behavior use the `-g` or `--global` flag. +[UNORDERED_START] +Download http://localhost:4260/@denotest/node-lifecycle-scripts +Download http://localhost:4260/@denotest/bin +Download http://localhost:4260/@denotest/node-lifecycle-scripts/1.0.0.tgz +Download http://localhost:4260/@denotest/bin/1.0.0.tgz +Initialize @denotest/node-lifecycle-scripts@1.0.0 +Initialize @denotest/bin@1.0.0 +[UNORDERED_END] +warning: Packages contained npm lifecycle scripts (preinstall/install/postinstall) that were not executed. + This may cause the packages to not work correctly. To run them, use the `--allow-scripts` flag with `deno cache` or `deno install` + (e.g. `deno cache --allow-scripts=pkg1,pkg2 ` or `deno install --allow-scripts=pkg1,pkg2`): + @denotest/node-lifecycle-scripts@1.0.0 diff --git a/tests/specs/npm/lifecycle_scripts/implicit_node_gyp.js b/tests/specs/npm/lifecycle_scripts/implicit_node_gyp.js new file mode 100644 index 000000000..2301aa626 --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/implicit_node_gyp.js @@ -0,0 +1,3 @@ +import { hello } from "npm:@denotest/node-addon-implicit-node-gyp@1.0.0"; + +console.log(hello()); diff --git a/tests/specs/npm/lifecycle_scripts/main.js b/tests/specs/npm/lifecycle_scripts/main.js new file mode 100644 index 000000000..2aaea0543 --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/main.js @@ -0,0 +1,3 @@ +import { hello } from "npm:@denotest/node-addon@1.0.0"; + +console.log(hello()); diff --git a/tests/specs/npm/lifecycle_scripts/node_gyp_not_found.out b/tests/specs/npm/lifecycle_scripts/node_gyp_not_found.out new file mode 100644 index 000000000..65ea53d58 --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/node_gyp_not_found.out @@ -0,0 +1,8 @@ +[UNORDERED_START] +Download http://localhost:4260/@denotest/node-addon-implicit-node-gyp +Download http://localhost:4260/@denotest/node-addon-implicit-node-gyp/1.0.0.tgz +Initialize @denotest/node-addon-implicit-node-gyp@1.0.0 +[UNORDERED_END] +warning: node-gyp was used in a script, but was not listed as a dependency. Either add it as a dependency or install it globally (e.g. `npm install -g node-gyp`) +[WILDCARD] +error: script 'install' in '@denotest/node-addon-implicit-node-gyp@1.0.0' failed with exit code 1 diff --git a/tests/specs/npm/lifecycle_scripts/node_gyp_not_run.out b/tests/specs/npm/lifecycle_scripts/node_gyp_not_run.out new file mode 100644 index 000000000..86bdeaefe --- /dev/null +++ b/tests/specs/npm/lifecycle_scripts/node_gyp_not_run.out @@ -0,0 +1,9 @@ +Download http://localhost:4260/@denotest/node-addon +Download http://localhost:4260/node-gyp +[WILDCARD] +warning: Packages contained npm lifecycle scripts (preinstall/install/postinstall) that were not executed. + This may cause the packages to not work correctly. To run them, use the `--allow-scripts` flag with `deno cache` + (e.g. `deno cache --allow-scripts=pkg1,pkg2 `): + @denotest/node-addon@1.0.0 +error: Uncaught (in promise) Error: Cannot find module './build/Release/node_addon' +[WILDCARD] -- cgit v1.2.3