vite icon indicating copy to clipboard operation
vite copied to clipboard

Line numbers in TypeScript broken for Chrome/VSCode debugging

Open anselanza opened this issue 2 years ago • 5 comments

Describe the bug

I have a web application that does not use any frameworks such as React, Vue, etc. but does use TypeScript instead of normal JS files. When breakpoints are placed in my source code (using VSCode) the application does indeed break at the expected moment, and both Chrome Debugger and VSCode highlight the same (correct) lines but all Call Stack and variables info clearly shows the debugger is being referred to a different line. In addition, the line numbers reported for console.log statements (in Debug Console, for example) do not match the source code.

Reproduction

This is unfortunately tricky to reproduce because the problem seems to accumulate over a number of lines of code, and I'm not sure exactly where the misalignment happens. I will try to describe what I'm seeing as carefully as possible:

Line 201 is indeed the same line I set a breakpoint, and Chrome stops at this breakpoint and highlights the correct line: Screenshot 2021-11-25 at 13 23 07

Back in VSCode, the same (correct) line is still highlighted and the breakpoint has not moved, BUT notice how the "Variables" pane is showing information relating to a function a few lines down (removeControlledPlayer): Screenshot 2021-11-25 at 13 22 56

Notice how Chrome Debugger is doing the same thing - the Call Stack is showing the correct line number as per the sourcecode (201) but entirely the wrong function (removeControlledPlayer starts only from line 204 in the source code): Screenshot 2021-11-25 at 13 23 12

In what I assume is related, the line numbers for nearby console.log statements are curiously out. For example, the string "pointerup: remove!" is attributed to line 195 in the logs: Screenshot 2021-11-25 at 13 23 30

...But this statement actually appears on line 211 in the source code: Screenshot 2021-11-25 at 13 23 38

When bundling the exact same source code using another bundler (in my case, Parcel) the debugger works as normal and line numbers do not misalign like this.

Is there something wrong in ESBuild sourcemaps for TypeScript, which is what I believe Vite uses for TypeScript transpilation?

System Info

System:
    OS: macOS 11.6
    CPU: (4) x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
    Memory: 101.52 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 7.21.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 96.0.4664.55
    Firefox: 88.0.1
    Safari: 15.0
  npmPackages:
    vite: ^2.6.14 => 2.6.14

Used Package Manager

npm

Logs

No response

Validations

anselanza avatar Nov 25 '21 12:11 anselanza

In case it helps anyone, for my project removing multi-line imports worked around the issue.

import {
funcA,
funcB
} from './lib'

to

import { funcA, funcB } from './lib' 

cphillips avatar May 07 '22 18:05 cphillips

same problem, hope somebody to fix this bug.

m430 avatar May 10 '22 17:05 m430

Any update?

winston0410 avatar Jun 28 '22 08:06 winston0410

Same problem here

redplant3d avatar Jul 04 '22 08:07 redplant3d

Here is a regex that I used to find multiline imports in my IDE

^import .*\{[^\}]+$

Flamenco avatar Sep 24 '22 12:09 Flamenco

I can confirm that I too see this bug with v4.3.8 in FF and Safari.

I can also confirm that @cphillips's workaround works still.

paxperscientiam avatar Jun 05 '23 04:06 paxperscientiam

Avoiding multi-line imports did not help in my case. Line numbers are totally off in stacktraces.

dmoebius avatar Jun 06 '23 09:06 dmoebius

Hello @anselanza. 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 Jun 14 '23 10:06 github-actions[bot]