Skip to content

Check package reachability before using exports in declarations - #64599

Open
Mateusz Burzyński (Andarist) wants to merge 1 commit into
microsoft:mainfrom
Andarist:fix-unreachable-exports-declaration
Open

Mateusz Burzyński (Andarist) wants to merge 1 commit into
microsoft:mainfrom
Andarist:fix-unreachable-exports-declaration

Conversation

@Andarist

Copy link
Copy Markdown
Contributor

fixes #64590

The issue was causedd by an early return~, the code sequence was roughly was:

  1. Inspect package.json.
  2. If exports supplies a specifier, return it immediately.
  3. Check whether the importing file can reach that node_modules directory.

So the step 2 bypassed the step 3's validation~ entirely. The fix simply relies on reordering the steps listed above. It now validates the reachability before checking if the exports have the specifier.

Copilot AI balanced review requested due to automatic review settings October 2, 2026 17:10
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 2, 2026
@typescript-automation

Copy link
Copy Markdown
Contributor

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

Review effort: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Fixes a TypeScript module specifier generation bug where package exports could return a bare specifier before validating that the importing file can actually reach the corresponding node_modules folder.

Changes:

  • Reorders reachability validation to occur before consulting package.json exports when computing node module specifiers.
  • Adds a compiler test case covering unreachable-package scenarios involving exports (root, subpath, and pattern exports).
  • Adds baselines for both module=preserve and module=nodenext for the new test.
File Description
tsc/​internal/​modulespecifiers/​specifiers.go Moves node_modules reachability check earlier to prevent exports from bypassing validation.
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitUnreachablePackageExports.ts New repro test for declaration emit when exports paths are unreachable from the importer.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitUnreachablePackageExports(module=preserve).js Baseline output for the new test under module=preserve.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitUnreachablePackageExports(module=preserve).errors.txt Baseline errors for the new test under module=preserve.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitUnreachablePackageExports(module=nodenext).js Baseline output for the new test under module=nodenext.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitUnreachablePackageExports(module=nodenext).errors.txt Baseline errors for the new test under module=nodenext.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +769 to +770

if !stringutil.HasPrefix(info.SourceDirectory, pathToTopLevelNodeModules, caseSensitive) || len(globalTypingsCacheLocation) > 0 && stringutil.HasPrefix(globalTypingsCacheLocation, pathToTopLevelNodeModules, caseSensitive) {
Comment on lines +765 to +772
// Check reachability before consulting package.json exports, which can return
// a bare specifier directly. An exported path is only usable if the importing
// file can reach the package through node_modules.
pathToTopLevelNodeModules := pathObj.FileName[0:parts.TopLevelNodeModulesIndex]

if !stringutil.HasPrefix(info.SourceDirectory, pathToTopLevelNodeModules, caseSensitive) || len(globalTypingsCacheLocation) > 0 && stringutil.HasPrefix(globalTypingsCacheLocation, pathToTopLevelNodeModules, caseSensitive) {
return ""
}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Declaration emit writes an import the file cannot resolve when the package has exports; no TS2883

2 participants