vitest
vitest copied to clipboard
Option `--config` of `vite-node` not working
Describe the bug
The --config
option is not taken into account and vite-node
continues to use the vite.config.js
configuration file.
Reproduction
https://stackblitz.com/edit/vitejs-vite-yn6aom?file=package.json
npx vite-node --script ./scripts/test.ts
# return `REAL_KEY`
npx vite-node --config vite-node.config.js --script ./scripts/test.ts
# BUG: return `REAL_KEY`, but should return DUMMY_KEY
System Info
System:
OS: Linux 5.15 Arch Linux
CPU: (32) x64 AMD Ryzen 9 7950X 16-Core Processor
Memory: 9.92 GB / 15.19 GB
Container: Yes
Shell: 5.9 - /usr/sbin/zsh
Binaries:
Node: 18.16.1 - /mnt/wslg/runtime-dir/fnm_multishells/20564_1688209265198/bin/node
npm: 9.7.2 - /mnt/wslg/runtime-dir/fnm_multishells/20564_1688209265198/bin/npm
pnpm: 8.6.5 - /mnt/wslg/runtime-dir/fnm_multishells/20564_1688209265198/bin/pnpm
npmPackages:
@vitest/coverage-c8: ^0.32.2 => 0.32.2
vitest: ^0.32.2 => 0.32.2
Used Package Manager
npm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
--script
option is for "hashbang" usage https://github.com/vitest-dev/vitest/tree/main/packages/vite-node#hashbang. As explained there, when this is used, all other options are considered to be the process.argv
of the script itself.
I suppose you can just remove --script
in your case:
https://stackblitz.com/edit/vitejs-vite-29jczg?file=package.json
❯ npm run test:vite-node-config
> [email protected] test:vite-node-config
> vite-node --config vite-node.config.js ./scripts/test.ts
DUMMY_KEY
❯ npm run test:vite-config
> [email protected] test:vite-config
> vite-node ./scripts/test.ts
REAL_KEY
If I remember correctly, it should still process arguments that come before the file name. Only arguments after the file name are passed down as process.argv
to the script. This is how Node.js CLI is supposed to work if I am not mistaken.
Only arguments after the file name are passed down as process.argv to the script.
Probably it was difficult to implement this on top of cac argument handling https://github.com/vitest-dev/vitest/pull/2793. Currently options
are completely emptied when --script
is given:
https://github.com/vitest-dev/vitest/blob/b561c3290de85c58daf7f062cfa0fc163425199e/packages/vite-node/src/cli.ts#L55-L59
But now looking at the other PR https://github.com/vitest-dev/vitest/pull/3574 which changed options to be passed to the script itself, maybe it might be now easier to achieve this.
Btw, I realized #!/usr/bin/env vite-node --script
doesn't work on Linux (tested on my PC) and it must be #!/usr/bin/env -S ...
. This caveat is explained in ts-node doc https://typestrong.org/ts-node/docs/usage/#shebang but probably missed out when adding this feature on vite-node.
Using the shebang way might be a option..?
#!/usr/bin/env -S npx vite-node --config="vite-node.config.ts"
// Javascript/Typescript code here...