vitest-dev / vitest

Next generation testing framework powered by Vite.

Home Page:https://vitest.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Coverage of types files can end up with a fatal error: `Cannot read properties of undefined (reading 'endCol')`

danielroe opened this issue · comments

Describe the bug

I apologise for not submitting a minimal reproduction, but thought you'd appreciate notice of this issue while I work on creating the minimal repro.

⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯
TypeError: Cannot read properties of undefined (reading 'endCol')
 ❯ range.sliceRange node_modules/.pnpm/@vitest+coverage-v8@1.5.2_vitest@1.5.2_@types+node@20.12.7_happy-dom@14.7.1_sass@1.69.4_terser@5.27.0_/node_modules/@vitest/coverage-v8/dist/provider.js:424:[55](https://github.com/nuxt/nuxt/actions/runs/8883861363/job/24393092343?pr=26922#step:7:56)
 ❯ CovSource.offsetToOriginalRelative node_modules/.pnpm/@vitest+coverage-v8@1.5.2_vitest@1.5.2_@types+node@20.12.7_happy-dom@14.7.1_sass@1.69.4_terser@5.27.0_/node_modules/@vitest/coverage-v8/dist/provider.js:1545:20
 ❯ V8ToIstanbul._maybeRemapStartColEndCol node_modules/.pnpm/@vitest+coverage-v8@1.5.2_vitest@1.5.2_@types+node@20.12.7_happy-dom@14.7.1_sass@1.69.4_terser@5.27.0_/node_modules/@vitest/coverage-v8/dist/provider.js:1946:92
 ❯ node_modules/.pnpm/@vitest+coverage-v8@1.5.2_vitest@1.5.2_@types+node@20.12.7_happy-dom@14.7.1_sass@1.69.4_terser@5.27.0_/node_modules/@vitest/coverage-v8/dist/provider.js:1855:60
 ❯ node_modules/.pnpm/@vitest+coverage-v8@1.5.2_vitest@1.5.2_@types+node@20.12.7_happy-dom@14.7.1_sass@1.69.4_terser@5.27.0_/node_modules/@vitest/coverage-v8/dist/provider.js:1853:20
 ❯ V8ToIstanbul.applyCoverage node_modules/.pnpm/@vitest+coverage-v8@1.5.2_vitest@1.5.2_@types+node@20.12.7_happy-dom@14.7.1_sass@1.69.4_terser@5.27.0_/node_modules/@vitest/coverage-v8/dist/provider.js:1852:12
 ❯ node_modules/.pnpm/@vitest+coverage-v8@1.5.2_vitest@1.5.2_@types+node@20.12.7_happy-dom@14.7.1_sass@1.69.4_terser@5.27.0_/node_modules/@vitest/coverage-v8/dist/provider.js:2453:19

Debugging, this reveals in this code:

while (start < end) {
  let mid = (start + end) >> 1;
  if (startCol >= lines[mid].endCol) {
    start = mid + 1;
  } else if (endCol < lines[mid].startCol) {
    end = mid - 1;
  } else {
    end = mid;
    while (mid >= 0 && startCol < lines[mid].endCol && endCol >= lines[mid].startCol) {
      --mid;
    }
    start = mid + 1;
    break
  }
}

When the error occurs, start is 0, end is 1, and therefore mid initialises at 0. This means end gets set to -1 and lines[end] therefore ends up undefined.

When I log the file being processed, it seems to be one of the files which export pure types. (But note I have not been able to reproduce this naively, so 🤷.)

Reproduction

nuxt/nuxt#26922

System Info

System:
    OS: macOS 14.2.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 45.02 GB / 96.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.local/share/mise/installs/node/20/bin/node
    npm: 10.2.3 - ~/.local/share/mise/installs/node/20/bin/npm
    pnpm: 9.0.6 - ~/.local/share/mise/installs/node/20/bin/pnpm
    bun: 1.0.25 - ~/.bun/bin/bun
  Browsers:
    Chrome: 124.0.6367.92
    Safari: 17.2.1
  npmPackages:
    @vitest/coverage-v8: 1.5.2 => 1.5.2 
    vitest: 1.5.2 => 1.5.2

Used Package Manager

pnpm

Validations

I haven't yet looked into the reproduction case or possible root cause but remembered this issue from upstream: istanbuljs/v8-to-istanbul#198

Regression of #5457.

This condition is invalid now as all results have source maps at this point. Looks like I forgot to update this part. Files that were not run in vite-node's wrapper incorrectly apply its offset which causes v8-to-istanbul to attempt to read out of array indexes.

// If no source map was found from vite-node we can assume this file was not run in the wrapper
const wrapperLength = sources.sourceMap ? WRAPPER_LENGTH : 0