Fix arguments when using -e with Node.js or Bun#4
Fix arguments when using -e with Node.js or Bun#4
-e with Node.js or Bun#4Conversation
src/argv_ffi.mjs
Outdated
|
|
||
| if (globalThis.process) { | ||
| const specialArgs = ["-e", "--eval", "-p", "--print"]; | ||
| // In Node.js and Bun, when special arguments are specified, `process.argv[1]` is not a script path, |
| const specialArgs = ["-e", "--eval", "-p", "--print"]; | ||
| // In Node.js and Bun, when special arguments are specified, `process.argv[1]` is not a script path, | ||
| // and command line arguments are stored starting from `process.argv[1]`. | ||
| if (process.execArgv.some((x) => specialArgs.includes(x))) { |
There was a problem hiding this comment.
I think this is a bit over-eager? There could be --eval in argv and it not be a call to that flag, such as if it was a value for another flag or if it came after --?
Would we need to handle --eval=something?
How can we test this to be sure it works?
There was a problem hiding this comment.
process.execArgv is Node.js (or Bun) specific arguments.
Arguments after -- are not included in process.execArgv.
They are included in process.argv.
See https://nodejs.org/api/process.html#processexecargv
I have no good ideas how to test this with automated testing. However, I tested it by manually executing the following commands.
$ node -e "import {load} from './build/dev/javascript/argv/argv.mjs';console.log(JSON.stringify(load(),null,2))" -- --eval arg1
{
"runtime": "/home/kit494way/.local/share/mise/installs/node/24.2.0/bin/node",
"program": "",
"arguments": {
"head": "--eval",
"tail": {
"head": "arg1",
"tail": {}
}
}
}
$ bun -e "import {load} from './build/dev/javascript/argv/argv.mjs';console.log(JSON.stringify(load(),null,2))" -- --eval arg1
{
"runtime": "/home/kit494way/.local/share/mise/installs/bun/1.2.21/bin/bun",
"program": "",
"arguments": {
"head": "--eval",
"tail": {
"head": "arg1",
"tail": {}
}
}
}There was a problem hiding this comment.
What about the --eval=something syntax?
I have no good ideas how to test this with automated testing.
Could use the shellout package to call various runtimes with various arguments, running a Gleam function that prints argv.load(). JSON.stringify couldn't be used as its behaviour is unspecified with Gleam data.
|
Thank you!! |
Fix arguments when using special arguments (i.e.
-e,--eval,-p,--print) with Node.js or Bun.When special arguments are specified,
process.argv[1]is not a script path, and command line arguments are stored starting fromprocess.argv[1].In Node.js,
util.parseArgs(), which parsesprocess.argv, extracts from index 1 ofprocess.argvwhen a special argument is specified.https://github.com/nodejs/node/blob/7079041e0a73738185ff5b7480d4775158b527c3/lib/internal/util/parse_args/parse_args.js#L58-L72
Deno also implements
process.argv, but it behaves differently from Node.js and Bun.Therefore, change the processing order so that
Deno.argstakes precedence overprocess.argv.Before this change
Node.js
When executing as Node.js script file, an argument
arg1is included inarguments.Output:
When evaluating string as Node.js script, an arguments
arg1is treated asprogramand not included inarguments.node -e "import {load} from './build/dev/javascript/argv/argv.mjs'; console.log(load())" arg1Output:
Bun
When executing as Bun script file, an argument
arg1is included inarguments.Output:
When evaluating string as Bun script, an arguments
arg1is treated asprogramand not included inarguments.bun -e "import {load} from './build/dev/javascript/argv/argv.mjs'; console.log(load())" arg1Output:
After this change
Node.js
Regardless of whether you execute it as a script file or evaluate a string as a script, the argument
arg1is correctly included inarguments.Execute as Node.js script file:
Output:
Evaluate string as Node.js script:
node -e "import {load} from './build/dev/javascript/argv/argv.mjs'; console.log(load())" arg1Output:
Bun
Regardless of whether you execute it as a script file or evaluate a string as a script, the argument
arg1is correctly included inarguments.Execute as Bun script file:
Output:
Evaluate string as Bun script:
bun -e "import {load} from './build/dev/javascript/argv/argv.mjs'; console.log(load())" arg1Output: