fix: normalize separators in getPossibleRequests for Windows (#1308)#1309
Merged
Conversation
In the modern API, Sass resolves relative URLs against the containing file's URL itself and hands the importer an absolute file: URL with containingUrl === null. The previous code routed that URL through the webpack resolver, which on Windows fails because getPossibleRequests builds variant requests via string concatenation and produces mixed-separator paths that enhanced-resolve cannot resolve. Treat the URL as already canonical: verify the file exists with loaderContext.fs.stat and return it. If the file is missing, return null so Sass retries with the relative URL form and a populated containingUrl. Closes #1308
…t-circuit
Earlier commit added a special-case in getModernWebpackImporter for
absolute file: URLs with containingUrl=null. The actual root cause is
narrower: getPossibleRequests builds variants by concatenating
'${path.dirname(request)}/' with the basename. On Windows path.dirname
returns backslashes, mixing with the hardcoded forward slash and
producing mixed-separator paths like 'C:\\foo/bar.scss' that
enhanced-resolve cannot resolve.
Normalize the dirname output to forward slashes so every variant has a
single, consistent separator. This is a one-liner that fixes the root
cause and works on both POSIX and Windows without any special case.
Refs #1308
|
The previous commit added tests for relative @import/@use inside an npm package but only updated loader.test.js.no-node-sass.snap (the snapshot file used when node-sass is unavailable). Windows × Node 20 in CI runs with node-sass installed and consults loader.test.js.snap, which was missing the new entries — jest's --ci flag forbids creating snapshots, so the job failed. Regenerate loader.test.js.snap on Node 20 with node-sass so both snapshot files are in sync. Adds 78 new snapshot entries (24 dart-sass + 24 sass-embedded entries already in the no-node-sass file, plus 30 new ones, of which 4 are node-sass legacy).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1309 +/- ##
=======================================
Coverage 94.13% 94.13%
=======================================
Files 3 3
Lines 409 409
Branches 152 154 +2
=======================================
Hits 385 385
Misses 22 22
Partials 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1308 — on Windows the modern API's webpack importer fails to resolve relative SCSS imports inside packages installed under
node_modules(e.g.@import './themes/light.scss';from inside@primer/css/color-modes/index.scss). Same code works on Linux/macOS.Root cause
When Sass's modern API encounters a relative URL inside a
file:-scheme stylesheet, it resolves the URL itself and hands the importer an absolutefile:URL withcontainingUrl === null. sass-loader then converts that URL to a path and feeds it through the webpack resolver.getPossibleRequestsbuilds variant requests by string-concatenating${path.dirname(request)}/+ name. On POSIXpath.dirnamereturns forward slashes, matching the hardcoded/joiner — variants are clean. On Windowspath.dirnamereturns backslashes, producing mixed-separator strings likeC:\…\themes/_light.scss. enhanced-resolve cannot resolve those (verified locally: even a single mid-path backslash spliced into a POSIX absolute path makes it fail).This was confirmed not to be a Next.js or enhanced-resolve bug specifically:
sass-loader@16.0.5is bit-for-bit identical to upstream — no Next.js-side modification.\and all-/absolute Windows paths, but not the mixed form sass-loader feeds it.Fix
One-line normalization: lowercase
path.dirnameoutput to forward slashes so every constructed variant has a single, consistent separator.This is platform-agnostic (a no-op on POSIX) and avoids any modern-API-specific special case.
Tests
Added a fixture package whose
index.scssdoes a relative@import './themes/light.scss'(and a sibling for@use), plus a~pkg/index.scssentry. The test runs across everydart-sass | sass-embedded×legacy | modern | modern-compiler×scss | sasscombination — 24 new test cases that exercise the exact bug path.Test plan
npm test— 1676/1676 passnpm run lint— cleanCloses #1308