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

Sourcemap for ... points to missing source files for @import with sass modern api #18111

Closed
7 tasks done
hi-ogawa opened this issue Sep 16, 2024 · 1 comment · Fixed by #18113
Closed
7 tasks done

Sourcemap for ... points to missing source files for @import with sass modern api #18111

hi-ogawa opened this issue Sep 16, 2024 · 1 comment · Fixed by #18113
Labels
feat: css p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Sep 16, 2024

Describe the bug

There was a report on Nuxt nuxt/nuxt#28723 as they enables css.devSourcemap by default. It turns out the issue can be reproduced on Vite as well, but it wasn't caught by playground/css-sourcemap since it doesn't use @import.

This seems to happen when @import is resolved via custom importer. Here is an example.

// [not ok] this triggers Vite's custom importer
@import "/variables.scss";

// [ok] sass's own resolution is fine
// @import "../variables.scss";

body {
  color: $primary;
}
$ DEBUG=vite:sourcemap pnpm dev
...
Sourcemap for "/home/hiroshi/code/personal/reproductions/vite-sass-modern-sourcemap/src/style.scss" points to missing source files
  vite:sourcemap Missing sources:
  vite:sourcemap   /home/hiroshi/code/personal/reproductions/vite-sass-modern-sourcemap/src/data:;charset=utf-8,$primary: blue;
  vite:sourcemap  +0ms

Reproduction

https://github.com/hi-ogawa/reproductions/tree/main/vite-sass-modern-sourcemap

Steps to reproduce

System Info

(stackblitz)

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.4.1 => 5.4.5

Used Package Manager

pnpm

Logs

No response

Validations

@hi-ogawa
Copy link
Collaborator Author

hi-ogawa commented Sep 16, 2024

It looks like modern api's custom importer using canonicalize + load generates source map's sources as data: url. There's another way to implement custom importer using findFileUrl and this one would work, but this is not usable for Vite's custom import as it currently requires patching up file content for rebasing url references.

I made a quick demo here https://github.com/hi-ogawa/reproductions/blob/main/vite-sass-modern-sourcemap/repro.mjs

@import "/variables.scss";

body {
  color: $primary;
}
# canonical + load importer
$ node repro.mjs modern
{
  css: 'body {\n  color: blue;\n}',
  map: {
    version: 3,
    sourceRoot: '',
    sources: [ 'file:///main.scss', 'data:;charset=utf-8,$primary:%20blue;' ],
    names: [],
    mappings: 'AAEI;EACE,OCHI'
  }
}

# findFileUrl importer
$ node repro.mjs modern-findFileUrl
{
  css: 'body {\n  color: blue;\n}',
  map: {
    version: 3,
    sourceRoot: '',
    sources: [
      'file:///main.scss',
      'file:///home/hiroshi/code/personal/reproductions/vite-sass-modern-sourcemap/variables.scss'
    ],
    names: [],
    mappings: 'AAEI;EACE,OCHI'
  }
}

Probably quick solution/workaround is to simply silence "missing source files" warning by adding data: to this list:

// Virtual modules should be prefixed with a null byte to avoid a
// false positive "missing source" warning. We also check for certain
// prefixes used for special handling in esbuildDepPlugin.
const virtualSourceRE = /^(?:dep:|browser-external:|virtual:)|\0/


EDIT: It turned out sass supports returning ImporterResult.sourceMapUrl from custom importer and it works. I updated repro.mjs accordingly https://github.com/hi-ogawa/reproductions/blob/5ace1c1af9c78ec37d2b3e2cb4f5cd5fe07e1a40/vite-sass-modern-sourcemap/repro.mjs#L27

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: css p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant