Skip to content

Reverse imports / exports expanding keys priorities #49308

@PaperStrike

Description

@PaperStrike

Bug Report

🔎 Search Terms

imports priority, exports priority, imports order, exports order, imports lookup, exports lookup, multi imports, multi exports

🕗 Version & Regression Information

  • This changed between versions 4.5 and 4.7
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Node12

⏯ Playground Link

Not applicable

💻 Code

Given file ./test/setup.ts, file ./src/lib/Service.ts, and ./package.json:

{
  "imports": {
    "#test/*": "./test/*.js",
    "#lib/*": null,
    "#*": "./src/*.js",
  }
}

🙁 Actual behavior

TS can't find #test/setup, but can find #lib/Service. I debugged and found here, in loadModuleFromImportsOrExports,

const expandingKeys = sort(filter(getOwnKeys(lookupTable as MapLike<unknown>), k => k.indexOf("*") !== -1 || endsWith(k, "/")), (a, b) => a.length - b.length);
for (const potentialTarget of expandingKeys) {

TS sorts the expanding keys from short to long before the loop. So the first potentialTarget is #*, and it matches both, resolving to ./src/test/setup.ts and ./src/lib/Service.ts. It's very likely that it sorts in the reverse way.

🙂 Expected behavior

  • For #test/setup, TS tests #test/* first, and resolves to ./test/setup.ts.
  • For #lib/Service, TS tests #test/* first, not matched, then tests #lib/*, resolves to null, goes to the null branch in loadModuleFromTargetImportOrExport and returns undefined.

The same algorithm applies to exports field.

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions