vitest icon indicating copy to clipboard operation
vitest copied to clipboard

vitest --ui errors when run from PowerShell/cmd

Open alextompkins opened this issue 2 years ago • 2 comments

Describe the bug

@vitest/ui seems unable to handle Windows absolute file paths. When running from PowerShell or cmd, we get the following error:

> vitest --ui

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: spawn undefined\System32\WindowsPowerShell\v1.0\powershell ENOENT
    at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
    at onErrorNT (node:internal/child_process:477:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ChildProcess instance at:
    at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
    at onErrorNT (node:internal/child_process:477:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'spawn undefined\\System32\\WindowsPowerShell\\v1.0\\powershell',
  path: 'undefined\\System32\\WindowsPowerShell\\v1.0\\powershell',
  spawnargs: [
    '-NoProfile',
    '-NonInteractive',
    '–ExecutionPolicy',
    'Bypass',
    '-EncodedCommand',
    'UwB0AGEAcgB0ACAAIgBoAHQAdABwADoALwAvADEAMgA3AC4AMAAuADAALgAxADoANQAxADIAMAA0AC8AXwBfAHYAaQB0AGUAcwB0AF8AXwAvACIA'
  ]
}

The problem does not occur when running the command via Git Bash, which uses synthesized unix-style file paths. As you can see from the stacktrace, it is trying to access a path with the drive letter of undefined.

Reproduction

Any repo using:

  • "vite": ^3.0.4
  • "vitest": "^0.22.0"
  • "@vitest/ui": "^0.22.0"

and running on Windows.

System Info

System:
    OS: Windows 10 10.0.22000
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
    Memory: 8.55 GB / 31.73 GB
  Binaries:
    Node: 16.13.1 - C:\Program Files\nodejs\node.EXE
    npm: 8.5.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (104.0.1293.54)
    Internet Explorer: 11.0.22000.120
  npmPackages:
    @vitejs/plugin-react: ^2.0.0 => 2.0.0
    vite: ^3.0.4 => 3.0.8
    vitest: ^0.22.0 => 0.22.0

Used Package Manager

npm

Validations

alextompkins avatar Aug 17 '22 22:08 alextompkins

We temporarily close this due to the lack of enough information. Please provide a minimal reproduction to reopen the issue. Thanks.

Why reproduction is required

antfu avatar Aug 29 '22 00:08 antfu

Hi @antfu, the issue occurs with any repo which uses vitest, if you run the vitest --ui command from PowerShell or cmd. There is no way (that I know of) I can provide a reproducible example which includes either of those, except maybe a Windows docker container (but you'd need Windows to run that anyway!).

alextompkins avatar Aug 29 '22 02:08 alextompkins

I had a similar issue.

It turns out my shell had an environment variable called BROWSER with an outdated value. Vitest uses this variable (if defined) to open the browser, so using an invalid value will attempt to call an invalid command. The solution was to erase this variable (in my case, from bash).

It's also possible to do something like "test-ui": "BROWSER=your-browser-name vitest --ui" in your package.json for testing purposes.

rodamaral avatar Jan 03 '23 22:01 rodamaral

@rodamaral interesting, although I'm fairly sure that's not the same issue. As you can see from the stacktrace, it seems like the issue stems from the drive letter in the path being undefined. I will check however that there isn't any BROWSER variable defined in the shell.

It's worth noting that I do not get this issue in bash, only cmd and PowerShell.

alextompkins avatar Jan 03 '23 22:01 alextompkins

I have the same issue in Windows 11:

"vite": "^4.1.4", "vitest": "^0.29.2", "@vitest/ui": "^0.29.2",

Also made sure that I added %SystemRoot%\System32\WindowsPowerShell\v1.0\ to Path in Environment variables.

I'm using Volta to manage node etc: https://volta.sh/

 DEV  v0.29.2 C:/Git/BlocPlatform/Bloc/Frontend/Bloc.Vue
      UI started at http://localhost:51204/__vitest__/

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: spawn undefined\System32\WindowsPowerShell\v1.0\powershell ENOENT
    at ChildProcess._handle.onexit (node:internal/child_process:283:19)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess._handle.onexit (node:internal/child_process:289:12)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'spawn undefined\\System32\\WindowsPowerShell\\v1.0\\powershell',
  path: 'undefined\\System32\\WindowsPowerShell\\v1.0\\powershell',
  spawnargs: [
    '-NoProfile',
    '-NonInteractive',
    '–ExecutionPolicy',
    'Bypass',
    '-EncodedCommand',
    'UwB0AGEAcgB0ACAAIgBoAHQAdABwAHMAOgAvAC8AbABvAGMAYQBsAGgAbwBzAHQAOgA1ADEAMgAwADQALwBfAF8AdgBpAHQAZQBzAHQAXwBfAC8AIgA='
  ]
}

Node.js v18.15.0

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run vitest" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

olemarius avatar Mar 08 '23 07:03 olemarius

Do you use loadEnv to include your own environment variables? If so, do you add them like this?

process.env = {...process.env, ...loadEnv(mode, process.cwd())};

The issue is that under the hood Vite seems to read the variable process.env.SYSTEMROOT, which is actually defined as process.env.SystemRoot (at least on Windows machines). While process.env doesn't seem to be case sensitive with its properties, it seems they WILL get case sensitive if you de- and restructure the object like above, making process.env.SYSTEMROOT undefined.

Instead of de-/restructuring, do an object assignment instead:

Object.assign(process.env, loadEnv(mode, process.cwd()))

Hope this might help anyone.

EDIT: I should add that this issue seems to regard Vite in general. I'm not sure how this translates to vitest, but I figure the issue might be similar.

StrikeAgainst avatar Apr 09 '23 14:04 StrikeAgainst

Do you use loadEnv to include your own environment variables? If so, do you add them like this?

process.env = {...process.env, ...loadEnv(mode, process.cwd())};

The issue is that under the hood Vite seems to read the variable process.env.SYSTEMROOT, which is actually defined as process.env.SystemRoot (at least on Windows machines). While process.env doesn't seem to be case sensitive with its properties, it seems they WILL get case sensitive if you de- and restructure the object like above, making process.env.SYSTEMROOT undefined.

Instead of de-/restructuring, do an object assignment instead:

Object.assign(process.env, loadEnv(mode, process.cwd()))

Hope this might help anyone.

EDIT: I should add that this issue seems to regard Vite in general. I'm not sure how this translates to vitest, but I figure the issue might be similar.

Thank you man! It really helps)

zerg41 avatar Jul 29 '23 09:07 zerg41

Cannot reproduce the error described in the issue on Windows 11. Both cmd and powershell work fine.

sheremet-va avatar Jul 29 '23 16:07 sheremet-va

Hello @alextompkins. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

github-actions[bot] avatar Jul 29 '23 16:07 github-actions[bot]