Skip to content

ModuleRunner picking wrong sourceMappingURL #20551

@AriPerkkio

Description

@AriPerkkio

Describe the bug

When a source file has specific string that matches the MODULE_RUNNER_SOURCEMAPPING_REGEXP, getModuleSourceMapById will pick wrong source map URL and can fail.

const MODULE_RUNNER_SOURCEMAPPING_REGEXP = new RegExp(
`//# ${SOURCEMAPPING_URL}=data:application/json;base64,(.+)`,
)

      throw lazyDOMException('Invalid character', 'InvalidCharacterError');
            ^
DOMException [InvalidCharacterError]: Invalid character
    at atob (node:buffer:1302:13)
    at EvaluatedModules.getModuleSourceMapById (file:///Users/ari/Git/repros/module-runner/node_modules/.pnpm/vite@7.0.6/node_modules/vite/dist/node/module-runner.js:286:59)
    at file:///Users/ari/Git/repros/module-runner/index.mjs:24:31

In the reproduction, this will fix the crash:

- const text = "//# sourceMappingURL=data:application/json;base64,\${encoded}";
+ const text = "//# sourceMappingURL_ANYTHING=data:application/json;base64,\${encoded}";

Reproduction

https://stackblitz.com/~/edit/node-eolunju1?file=index.js

Steps to reproduce

npm i; node index.js

System Info

System:
    OS: macOS 15.5
    CPU: (8) arm64 Apple M2
    Memory: 121.16 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 22.17.0 - ~/.nvm/versions/node/v22.17.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.17.0/bin/npm
    pnpm: 8.15.9 - ~/.nvm/versions/node/v22.17.0/bin/pnpm
    bun: 1.2.16 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 131.1.73.101
    Chrome: 138.0.7204.184
    Safari: 18.5
  npmPackages:
    vite: ^7.0.6 => 7.0.6

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: sourcemapSourcemap supportp2-edge-caseBug, but has workaround or limited in scope (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions