Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/internal/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ function packageImportsResolve(name: string, base: URL, conditions?: Set<string>
const key = keys[i]!;
const patternIndex = key.indexOf("*");

if (patternIndex !== -1 && name.startsWith(key.slice(0, -1))) {
if (patternIndex !== -1 && name.startsWith(key.slice(0, patternIndex))) {
const patternTrailer = key.slice(patternIndex + 1);
if (
name.length >= key.length &&
Expand Down
1 change: 1 addition & 0 deletions test/fixture/imports-pkg/feat-any/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const via = "feat-any";
1 change: 1 addition & 0 deletions test/fixture/imports-pkg/feat-js/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const via = "feat-js";
1 change: 1 addition & 0 deletions test/fixture/imports-pkg/lib/deep/thing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const thing = "lib/deep/thing.js";
6 changes: 5 additions & 1 deletion test/fixture/imports-pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "imports-pkg",
"imports": {
"#/*": "./src/*",
"#internal": "./internal.js"
"#internal": "./internal.js",
"#*.js": "./runtime/*.js",
"#lib/*.js": "./lib/*.js",
"#feat*.js": "./feat-js/*.js",
"#feat*": "./feat-any/*"
}
}
1 change: 1 addition & 0 deletions test/fixture/imports-pkg/runtime/internal/marker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const marker = "runtime/internal/marker.js";
32 changes: 32 additions & 0 deletions test/resolve-imports-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,38 @@ describe("package.json imports field", () => {
expect(resolved).toMatch(/imports-pkg\/internal\.js$/);
expect(existsSync(fileURLToPath(resolved))).toBe(true);
});

// Regression (#56): the pattern base was computed as `key.slice(0, -1)`
// instead of `key.slice(0, patternIndex)`, so any wildcard key with a
// trailer (`#*.js`) was tested against `name.startsWith("#*.")` and could
// never match. Keys without a trailer (`#/*`) happened to work, which hid
// the bug. `exports` resolution already used the correct base.
it("resolves a wildcard import key with a trailer", () => {
const resolved = resolveModuleURL("#internal/marker.js", { from });
expect(resolved).toMatch(/imports-pkg\/runtime\/internal\/marker\.js$/);
expect(existsSync(fileURLToPath(resolved))).toBe(true);
});

it("resolves a nested wildcard import key with a trailer", () => {
const resolved = resolveModuleURL("#lib/deep/thing.js", { from });
expect(resolved).toMatch(/imports-pkg\/lib\/deep\/thing\.js$/);
expect(existsSync(fileURLToPath(resolved))).toBe(true);
});

// The same bug could also resolve the *wrong* file rather than throw: when
// `#feat*.js` was skipped, the less specific `#feat*` won by default and
// `feat-any/x.js` was loaded instead. Both targets exist, so this pins key
// selection (`patternKeyCompare`) and not mere existence.
it("prefers the more specific key when wildcard keys compete", () => {
const resolved = resolveModuleURL("#featx.js", { from });
expect(resolved).toMatch(/imports-pkg\/feat-js\/x\.js$/);
expect(resolved).not.toMatch(/feat-any/);
});

// Boundary: `name.length >= key.length` fails, so `#*.js` must not match.
it("does not match a wildcard key when the specifier is too short", () => {
expect(() => resolveModuleURL("#.js", { from })).toThrow();
});
});

describe("package.json conditional exports fall-through", () => {
Expand Down
Loading