Repository navigation
Resolve package exports for SCSS imports without the pkg: prefix - #528
Eugene Ledenev (ledeneveugene) wants to merge 1 commit into
Conversation
Bundlers such as Vite and webpack apply the `exports` field of `package.json` to plain module imports, not only to `pkg:` imports. When a package maps `"./static/*": "./build/static/*"`, the import string does not match the file system, so document links, hover and go to definition resolve to a path that does not exist. Look up `exports` for bare module paths before falling back to a plain `node_modules` path. A relative file still takes precedence, like in the Sass compiler, and the `exports` result is only used when the file exists, so existing resolution is unchanged when no entry matches. Along the way `resolvePkgModulePath` (now `resolvePackageExports`, since both kinds of import share it) learns three things that the case above needs, and that also apply to `pkg:` imports: - an export target can be a plain string, not only an object of conditions - a target without a file extension, such as `./build/static/*`, is resolved the way Sass resolves an import: `.scss`, partials, index files. Targets like `./index.js` still do not match - subpath patterns are matched against the whole subpath and the pattern with the longest prefix wins, as in Node. The regular expression had no anchors before
|
@microsoft-github-policy-service agree |
Martin Aeschlimann (aeschli)
left a comment
There was a problem hiding this comment.
Export pattern selection still returns incorrect destinations
Two package-resolution cases are reproducible through the public navigation API:
1. Equal-prefix patterns need suffix precedence. With these entries in this order and both target files present:
{"exports":{"./styles/*":{"sass":"./general/*.scss"},"./styles/*-theme":{"sass":"./special/*.scss"}}}@use "pkg/styles/a-theme" resolves to general/a-theme.scss; the more specific pattern should select special/a.scss. Sorting by prefix length alone leaves these entries in insertion order. Node's pattern comparator also considers the remaining pattern length. Location: src/services/scssNavigation.ts around lines 116-118.
2. A matching excluded export must not fall through. Given:
{"exports":{"./styles/private/*":null,"./styles/*":{"sass":"./general/*.scss"}}}@use "pkg/styles/private/a" resolves to general/private/a.scss when that file exists, despite the matching explicit exclusion. After selecting the more specific null entry, resolution should remain blocked rather than continue to the broader pattern. Location: around lines 128-132.
Please add navigation fixtures for both cases and correct pattern selection/exclusion handling. Attribution matters: equal-prefix ordering is already wrong for pkg: imports on the base, and the base crashes on the null case; this PR improves the crash but extends incorrect successful resolution to newly supported bare imports. These are not both new regressions in existing pkg: behavior.
Reviewed commit: 1a94c0e.
The
exportsfield ofpackage.jsonis read only forpkg:imports. Bundlers such as Vite and webpack applyexportsto plain module imports as well, so a lot of projects write imports like this and get working builds:where the package publishes the file in a build folder:
The import is resolved to
node_modules/@scope/ui-kit/static/styles/theme, which does not exist. Go to definition and hover find nothing, and following the document link offers to create the file.importAliasesdoesn't help, because thenode_moduleslookup returns that missing path before the setting is checked.Changes
In
scssNavigation.ts:exportsof the package innode_modulesis looked up and the result is used when the file exists. Otherwise the previous lookup runs unchanged.sass,styleordefaultconditions../dist/static/*, is resolved the way Sass resolves an import:.scss, partials and index files. Targets like./index.jsstill don't match.resolvePkgModulePathis renamed toresolvePackageExports, sincepkg:imports and plain imports share it now.The string target, the extension handling and the pattern matching apply to
pkg:imports too.Tests
The new fixture
test/linksTestFixtures/node_modules/@foo/build-exportsmaps"./static/*": "./build/static/*"and contains a partial, a nested folder and a"./node/*": "./build/node/*.js"entry that is not a stylesheet. Unlike the other fixtures it ships real files, because the new code path checks that the resolved file exists.The new test covers imports with and without an extension, a partial, a nested path, a conditional export and the root export without the prefix, a non-stylesheet target that still falls back to the plain
node_modulespath, and apkg:import that goes through a string subpath pattern. It fails onmain.On Node 24.x,
npm testpasses compilation, 353 tests and 7 lint checks;mainpasses 352 tests and the same 7 lint checks.git diff --checkis clean.No dependency or language service API changes.