Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken source maps for bundles with packages from node_modules #6526

Open
6 tasks done
dmaretskyi opened this issue Sep 18, 2024 · 7 comments
Open
6 tasks done

Broken source maps for bundles with packages from node_modules #6526

dmaretskyi opened this issue Sep 18, 2024 · 7 comments
Labels

Comments

@dmaretskyi
Copy link

dmaretskyi commented Sep 18, 2024

Describe the bug

I have a workspace with two packages:

  • pkg-a build a bundle of its own code and packages from node_modules
  • pkg-b runs test from vitest that import code from pkg-a

The sourcemaps for errors thrown from inside pkg-a report incorrect source locations:

$ pnpm vitest

Error: Test error
 ❯ Module.foo ../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js:5:9
 ❯ index.test.js:5:3

Running similar code in node JS produces the correct stack trace:

$ node --enable-source-maps script.js
/Users/dmaretskyi/vitest-sourcemap-bug/pkg-a/index.js:5
  throw new Error("Test error");
        ^

Error: Test error
    at foo (/Users/dmaretskyi/vitest-sourcemap-bug/pkg-a/index.js:5:9)
    at file:///Users/dmaretskyi/vitest-sourcemap-bug/pkg-b/script.js:3:1
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)

Reproduction

https://github.com/dmaretskyi/vitest-sourcemap-bug

See README for repro steps

System Info

System:
    OS: macOS 14.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 78.80 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.9.0 - ~/.nodenv/versions/20.9.0/bin/node
    Yarn: 1.22.21 - ~/.nodenv/versions/20.9.0/bin/yarn
    npm: 10.2.4 - ~/.nodenv/versions/20.9.0/bin/npm
    pnpm: 8.11.0 - ~/.nodenv/versions/20.9.0/bin/pnpm
    bun: 1.0.25 - ~/.bun/bin/bun
  Browsers:
    Chrome: 128.0.6613.138
    Safari: 17.0
  npmPackages:
    vitest: ^2.1.1 => 2.1.1

Used Package Manager

pnpm

Validations

@dmaretskyi
Copy link
Author

For the bug to trigger it seems to be essential to have a package from node_modules in the bundle. Just bundling two local source files together does not repro the bug

@dmaretskyi
Copy link
Author

I've started debugging this...

It seems like the source file paths get corrupted when the source map is loaded
image

@dmaretskyi
Copy link
Author

After doing a dump with VITE_NODE_DEBUG_DUMP=1 it seems like those modules have broken source maps

@dmaretskyi
Copy link
Author

I've traced it down to vite's source map combining code. The actual source positions are correct, but something corrupts the source paths
image

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Sep 18, 2024

This looks similar to #5523. Workaround is to prevent Vitest/Vite from processing already bundled pkg-a and you can do that by configuring test.server.deps.external. I don't think Vite currently has a way to read existing dist.js.map and use it for remapping during own transform.

// pkg-b/vitest.config.ts
import { defineConfig } from "vitest/config"

export default defineConfig({
  test: {
    server: {
      deps: {
        external: [/pkg-a/],
      }
    }
  }
})

With this config, I see this error on Vitest:

 FAIL  index.test.js > repro
Error: Test error
 ❯ Module.foo ../pkg-a/index.js:5:9
 ❯ index.test.js:5:3

@AriPerkkio
Copy link
Member

AriPerkkio commented Sep 19, 2024

Looks like bug in Vite. map.sources are incorrect when file is SSR transformed.

Error: Test error
 ❯ Module.foo ../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js:5:9
                                                                                   ^^^ This is pkg-a/index.js, not contents of base32-decode

I renamed pkg-a/index.js to pkg-a/pkg-a-entrypoint.js here to make it unique from index.js.

When we request the pkg-a here, we get following. Notice the "...base32-decode/pkg-a-entrypoint.js" part:

result = await this.server.transformRequest(id, { ssr: true })

console.log(result.map.sources);
> {
>   "sources": [
>     "../node_modules/.pnpm/base32-decode@1.0.0/node_modules/node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js",
>     "../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/pkg-a-entrypoint.js"
>   ],
>   ...
> }

Minimal repro without Vitest:

import { createServer } from "vite";

const server = await createServer();
await server.listen();

const ssrResult = await server.transformRequest(
  "/Users/x/vitest-sourcemap-bug/pkg-a/dist.js",
  { ssr: true }
);
console.log(ssrResult.map);
/*
  sources: [
    '../node_modules/.pnpm/base32-decode@1.0.0/node_modules/node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js',
    '../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/pkg-a-entrypoint.js'
  ],
  file: '/Users/x/vitest-sourcemap-bug/pkg-a/dist.js'
*/

// Without SSR:
const webResult = await server.transformRequest(
  "/Users/x/vitest-sourcemap-bug/pkg-a/dist.js",
  { ssr: false }
);
console.log(webResult.map);
/*
  sources: [
    '../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js',
    'pkg-a-entrypoint.js'
  ],
*/

server.close();

If I manually fix the path in vite-node we get proper stack trace in errror:

 FAIL  index.test.js > repro
Error: Test error
  Module.foo ../pkg-a/pkg-a-entrypoint.js:5:9
  index.test.js:5:3
      3| 
      4| test("repro", () => {
      5|   foo();
       |   ^
      6| });
      7| 

I'll create upstream bug for this.

@AriPerkkio AriPerkkio added upstream p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage p3-minor-bug An edge case that only affects very specific usage (priority) labels Sep 19, 2024
@jwatte
Copy link

jwatte commented Sep 19, 2024

I just ran into this yesterday, and this bug is very timely!
Just a "me, too." Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants