vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.06k stars 1.17k forks source link

vite-node incompatible with oclif #3613

Open DanHulton opened 1 year ago

DanHulton commented 1 year ago

Describe the bug

I am writing a CLI for a project of mine and I want to be able to include files from that project when running the CLI for certain tasks. However, since I rune vite (for the UI) and vite-node (for the API) for that project, that means I must be able to run vite-node for the CLI as well. I have chosen oclif for the CLI, as it is a fairly popular CLI framework used by major companies to write their CLIs, however it does not run using vite-node. I get the following error when it attempts to read the commands folder and load the individual command files:

(node:51441) [ERR_UNKNOWN_FILE_EXTENSION] TypeError Plugin: oclif-vite-node-repro [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/danhulton/dev/oclif-vite-node-repro/src/commands/hello/index.ts
module: @oclif/core@2.8.7
task: toCached
plugin: oclif-vite-node-repro
root: /Users/danhulton/dev/oclif-vite-node-repro
See more details with DEBUG=*
(Use `node --trace-warnings ...` to show where the warning was created)
TypeError Plugin: oclif-vite-node-repro [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/danhulton/dev/oclif-vite-node-repro/src/commands/hello/index.ts
    at new NodeError (node:internal/errors:393:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:121:38)
    at defaultLoad (node:internal/modules/esm/load:81:20)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)
module: @oclif/core@2.8.7
task: toCached
plugin: oclif-vite-node-repro
root: /Users/danhulton/dev/oclif-vite-node-repro
See more details with DEBUG=*

I do not know which package is responsible for the error, so I am opening an issue in each project and am cross-linking them here:

"vite-node incompatible with oclif" at oclif/oclif

Reproduction

A full reproduction can be found at DanHulton/oclif-vite-node-repro.

This is a minimal install of both oclif and vite-node, as well as the bare minimum amount of changes required to integrate the two (which boil down to changing the shebang to use vite-node and converting oclif to use ESM).

System Info

System:
    OS: macOS 13.4
    CPU: (8) x64 Apple M1 Pro
    Memory: 76.40 MB / 32.00 GB
    Shell: 3.6.1 - /usr/local/bin/fish
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 3.3.1 - ~/.nvm/versions/node/v18.12.1/bin/yarn
    npm: 9.7.1 - ~/.nvm/versions/node/v18.12.1/bin/npm
  Browsers:
    Chrome: 114.0.5735.133
    Firefox: 113.0.2
    Safari: 16.5

Used Package Manager

npm

Validations

kachkaev commented 6 months ago

Getting the same error after upgrading aws-amplify:

FAIL  src/components/foobar.test.tsx [ src/components/foobar.test.tsx ]
TypeError: Unknown file extension ".ts" for /home/runner/work/project-name/node_modules/.pnpm/@aws-amplify+data-schema-types@0.7.17_@aws-sdk+types@3.387.0_aws-cdk-lib@2.139.0_constructs@10.3.0__constructs@10.3.0/node_modules/@aws-amplify/data-schema-types/lib-esm/index.d.ts
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 'ERR_UNKNOWN_FILE_EXTENSION' }

This is because of an intrinsic dependency @aws-amplify/data-schema-types@0.7.17. Their package.json is quite odd:

"main": "./lib-esm/index.d.ts",

We should probably mock the module or something like that, so I wonder if someone has already done something like this before in such cases.

hi-ogawa commented 6 months ago

@kachkaev Can you share more context of the error? Though their "main" field is indeed odd, the error itself is probably coming from NodeJS trying to import/require it, so as long as you put them into test.server.deps.inline (which makes Vite to process the dependency instead), it might work fine.

Also, OP was about the direct vite-node usage, so it might work slightly differently when using it from Vitest. It would be easier for us to help if you can raise an new issue with a proper reproduction.