Skip to content

fix(node_shim): forward --require/--import to translated deno args#34637

Open
bartlomieju wants to merge 2 commits into
mainfrom
fix/node-shim-forward-require-import
Open

fix(node_shim): forward --require/--import to translated deno args#34637
bartlomieju wants to merge 2 commits into
mainfrom
fix/node-shim-forward-require-import

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

node_shim parses Node's --require/-r and --import flags into
preload_cjs_modules and preload_esm_modules, but translate_to_deno_args
never read those vectors back out. The result was that a translated node
invocation silently dropped its preload modules: node --require ./setup.js script.js became deno run ... script.js with the --require gone.

deno run, deno eval, and deno test all accept --require (a CommonJS
preload) and --import (an alias of --preload, an ES module preload) through
runtime_misc_args, so the parsed preload modules can be forwarded directly on
every translated path. This adds an add_preload_flags helper that emits a
--require for each CommonJS preload and an --import for each ESM preload,
wired into add_common_flags (covering the run and test paths) and into the
eval path (which builds its flags inline). The preload flags are emitted before
the script or eval code so they preload ahead of the main module, matching
Node.

The REPL path is intentionally left unchanged; preload-into-REPL is a separate
case from the script and eval translations this targets.

New unit tests cover forwarding for --require, -r, and --import on the run
path, multiple preloads preserving order, and --require combined with -e
(asserting the flag lands on deno eval and precedes the eval code).

This came out of reviewing #34605, which hand-rolled a partial node argument
parser in cli/task_runner.rs partly because node_shim dropped these flags.
Fixing it here lets that path delegate to node_shim instead of reimplementing
the parsing.

node_shim parsed Node's --require/-r and --import flags into
preload_cjs_modules/preload_esm_modules but translate_to_deno_args never
emitted them, so the preload modules were silently dropped from the
translated `deno run`/`deno eval`/`deno test` invocation.

deno run, eval, and test all accept --require (CommonJS preload) and
--import (alias of --preload, ESM preload) via runtime_misc_args, so
forward each parsed preload module on every translated path. Add an
add_preload_flags helper called from add_common_flags (run/test) and the
eval path, and cover run, eval, short -r, and ordering with unit tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant