vscode: Vitest failes to start in vscode >=1.90 when using Yarn PnP

Describe the bug

With latest VSCode (version 1.90.2) the extension fails to start Vitest in a project setup with Yarn PnP:

SCR-20240630-shux SCR-20240630-shxf

Downgrading VSCode to version 1.89.1 results in Vitest process being successfully created:

SCR-20240630-sfac SCR-20240630-sffl

I suspect it has to do with Electron upgrade in latest VSCode release that comes bundled with Node.js 20.

Reproduction

https://github.com/Nithanim-Work/vitest-reproducer (reusing minimal example from https://github.com/vitest-dev/vscode/issues/416)

Output

[INFO 21:10:03] [v0.12.4] Vitest extension is activated because Vitest is installed or there is a Vite/Vitest config file in the workspace.
[INFO 21:10:04] [API] Running Vitest vpnp (web/vitest.config.ts) with Node.js: /Users/fima/.asdf/shims/node
[Error 21:10:05] [Error Error] Vitest failed to start: 

y
Error: Vitest failed to start: 

y
	at ChildProcess.f (/Users/fima/.vscode/extensions/vitest.explorer-0.12.4/dist/extension.js:18:5392)
	at ChildProcess.emit (node:events:514:28)
	at emit (node:internal/child_process:951:14)
	at process.processTicksAndRejections (node:internal/process/task_queues:83:21)

Version

0.12.4

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 21 (4 by maintainers)

Commits related to this issue

Most upvoted comments

After messing around for a while, I managed to figure out a workaround. The pnp loader doesn’t seem to want to resolve modules the way the worker hopes to be able to (i.e. it doesn’t seem to check the workspace at all unless the importer or the imported script are in the workspace). Not really sure if this is an issue with yarn or if the extension is meant to be doing something different (like using the plugin API or something)

Unpacking vitest and pointing directly to it fixes the vitest/node import as @viell-dev says above, but the extension seems to skip adding the pnp hooks when you specify vitestPackagePath, so vitest’s dependencies would also fail to resolve. This can be worked around through the NODE_OPTIONS environment variable; though the extension could be updated to skip this requirement

  1. Run yarn unplug vitest (don’t think -R is needed)
  2. Configure vitest.vitestPackagePath to point to the unpacked package.json (this may be different for you)
"vitest.vitestPackagePath": ".yarn/unplugged/vitest-virtual-b853d33a81/node_modules/vitest/package.json"
  1. Configure vitest.nodeEnv to set the NODE_OPTIONS env. var similar to the following:
"vitest.nodeEnv": {
    "NODE_OPTIONS": "--require C:\\your\\path\\to\\.pnp.cjs --experimental-loader file:///C:/your/path/to/.pnp.loader.mjs",
},

i imagine there’s a way to configure this so that you don’t have to hard-set the paths but this works for me

@TehPers please, check if v1.2.11 fixes it for you.

I’m also able to repro (Yarn PNP with workspaces) and have tried installing the extension globally, no luck.

You can get past this error and have extension make it past startup by installing whatever package it says is missing in the directory its looking for it… but you’ll then run into issues when you try to run any test via the UI as each test’s packages are going to be missing.

Node.js: v20.17.0 (with corepack enable) VSCode: v1.93.1

  1. yarn create vite to make a project. (yarn@3.5.0 is installed, as is vite@5.4.1) - all default settings
  2. yarn dlx @yarnpkg/sdks vscode to setup editor SDKs
  3. yarn add -D vitest (installs vitest@2.1.1)
  4. Add a src/example.test.ts with a very simple unit test. Verify it runs with yarn run vitest.
  5. Install v1.2.10 of the Vitest extension.
[INFO 12:38:43 PM] [v1.2.10] Vitest extension is activated because Vitest is installed or there is a Vite/Vitest config file in the workspace.
[Error 12:38:43 PM] [Error Error] Cannot find module 'vitest/node'
Require stack:
- E:\Dev\Random\blah
Error: Cannot find module 'vitest/node'
Require stack:
- E:\Dev\Random\blah
    at Module._resolveFilename (node:internal/modules/cjs/loader:1152:15)
    at Function.i._resolveFilename (node:electron/js2c/utility_init:2:13739)
    at callNativeResolution (e:\Dev\Random\blah\.pnp.cjs:11948:30)
    at resolveToUnqualified (e:\Dev\Random\blah\.pnp.cjs:12106:24)
    at resolveRequest (e:\Dev\Random\blah\.pnp.cjs:12355:31)
    at Object.resolveRequest (e:\Dev\Random\blah\.pnp.cjs:12418:26)
    at zr (c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:24:651)
    at c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:24:3210
    at Array.forEach (<anonymous>)
    at Lg (c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:24:3174)
    at Qc (c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:24:3075)
    at Bn._defineTestProfiles (c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:25:2126)
    at Bn.defineTestProfiles (c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:25:2028)
    at Bn.activate (c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:25:6333)
    at lv (c:\Users\tehpe\.vscode\extensions\vitest.explorer-1.2.10\dist\extension.js:25:1053)
    at m.n (c:\Users\tehpe\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:151:6409)
    at m (c:\Users\tehpe\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:151:6372)
    at m.l (c:\Users\tehpe\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:151:5829)

Edit: adding a vite.config.ts doesn’t fix the error either.

I may be missing something, but I’m unfortunately still getting a startup crash with Yarn PnP on v1.2.10:

[INFO 1:02:13 PM] [v1.2.10] Vitest extension is activated because Vitest is installed or there is a Vite/Vitest config file in the workspace.
[INFO 1:02:14 PM] [API] Running Vitest vpnp (ui/vite.config.ts) with Node.js: ~/Library/Caches/fnm_multishells/3213_1727368426358/bin/node 
[Error 1:02:14 PM] [Error Error] Vitest failed to start: 
TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected array buffer, or typed array to be returned for the "source" from the "transformSource" function but got undefined.
    at assertBufferSource (node:internal/modules/esm/translators:104:9)
    at stringify (node:internal/modules/esm/translators:119:3)
    at createCJSModuleWrap (node:internal/modules/esm/translators:279:12)
    at ModuleLoader.commonjsStrategy (node:internal/modules/esm/translators:358:10)
Error: Vitest failed to start: 
TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected array buffer, or typed array to be returned for the "source" from the "transformSource" function but got undefined.
    at assertBufferSource (node:internal/modules/esm/translators:104:9)
    at stringify (node:internal/modules/esm/translators:119:3)
    at createCJSModuleWrap (node:internal/modules/esm/translators:279:12)
    at ModuleLoader.commonjsStrategy (node:internal/modules/esm/translators:358:10)
	at ChildProcess.f (~/.vscode/extensions/vitest.explorer-1.2.10/dist/extension.js:19:5641)
	at ChildProcess.emit (node:events:519:28)
	at emit (node:internal/child_process:951:14)
	at process.processTicksAndRejections (node:internal/process/task_queues:83:21)