vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Option `--config` of `vite-node` not working

Open jagu-sayan opened this issue 1 year ago • 4 comments

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

jagu-sayan avatar Jul 01 '23 14:07 jagu-sayan

--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

hi-ogawa avatar Jan 16 '24 04:01 hi-ogawa

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.

sheremet-va avatar Jan 16 '24 08:01 sheremet-va

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.

hi-ogawa avatar Jan 17 '24 00:01 hi-ogawa

Using the shebang way might be a option..?

#!/usr/bin/env -S npx vite-node --config="vite-node.config.ts"

// Javascript/Typescript code here...

larscmagnusson avatar Jul 04 '24 20:07 larscmagnusson