vitest-dev / vitest

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

Debugger stops at at end of file when "stepping into" #5569

Open oliverschwendener opened 3 months ago

oliverschwendener commented 3 months ago

Describe the bug

I'm trying to debug tests in Visual Studio Code. I followed this guide: https://vitest.dev/guide/debugging.html to set up the vscode debugger and everything works fine, until I "step into" a function or method. The debugger does not stop on the correct line, but at the end of the file. I created a minimal repo to reproduce the issue: https://github.com/oliverschwendener/vitest-vscode-debugging-issue. Does anyone have an idea what could cause this issue? Did I misconfigure something?

Reproduction

https://github.com/oliverschwendener/vitest-vscode-debugging-issue

Output

[INFO 15:58:09] [v0.8.4] Vitest extension is activated because Vitest is installed or there is a Vite/Vitest config file in the workspace.
[INFO 15:58:09] [API] Running Vitest: v1.5.0 (vite.config.mts)
[INFO 15:58:09] [API] Starting Vitest process with Node.js: /Users/oliverschwendener/.nvm/versions/node/v20.12.2/bin/node
[INFO 15:58:10] [API] Vitest process 94273 created
[INFO 15:58:11] [API] Collecting tests: src/Calculator.test.ts
[INFO 15:58:11] [API] Collecting tests: src/Calculator.test.ts
[INFO 15:58:33] [DEBUG] Starting debugging on localhost:53856
[INFO 15:58:33] Running 1 file(s) with name pattern: ^\s?add should add two numbers$
[Worker] Debugger listening on ws://127.0.0.1:53856/691b403d-7e0e-4ddc-8605-5c4ff72dd9f6
For help, see: https://nodejs.org/en/docs/inspector
[Worker] Debugger attached.
[INFO 15:58:33] [DEBUG] Debugging started

Version

v0.8.4

Validations

hi-ogawa commented 3 months ago

Thanks for the reproduction.

It looks like this is due to how Vite SSR transforms imports/exports internally. Vite replaces each named export with a getter of magic variable __vite_ssr..., so probably this getter call is making an weird jump. When I tested it, it goes to add body after "step into" 3 times, so that coincides with my assumption.

I'm not sure if there's a solution to this, but probably this issue is reproducible on plain Vite SSR, so actions need to be taken there.

Btw, you are using vscode debugger directly with own launch.json, so technically this is not vscode extension issue, which is what this repo is about (EDIT: now the issue is moved to main vitest repo). But, the same issue can reproduce either way.

AriPerkkio commented 3 months ago

This is very likely related to how Vite SSR transform modifies the variables: https://github.com/ariperkkio/vite-ssr-transform-sourcemaps-issue/