Skip to content

Improve error when an unresolved import matches a global namespace - #64726

Open
MaheshDesu (mahesh-desu) wants to merge 1 commit into
microsoft:mainfrom
mahesh-desu:fix/23185-global-namespace-module-error
Open

MaheshDesu (mahesh-desu) wants to merge 1 commit into
microsoft:mainfrom
mahesh-desu:fix/23185-global-namespace-module-error

Conversation

@mahesh-desu

@mahesh-desu MaheshDesu (mahesh-desu) commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #23185.

When a non-relative import fails to resolve and a global namespace with the same name exists, the generic Cannot find module error is misleading - the declaration exists, but it declares a namespace rather than an ambient module. This reports a dedicated error for that case:

Cannot find module 'messageformat'. Did you mean to use the global namespace 'messageformat'? If you meant to declare an ambient module, the name must be quoted: `declare module 'messageformat';`.

This covers declarations like declare module foo { } (or declare namespace foo { }), which do not provide an importable external module.

New diagnostic: TS18125.

Implementation: in resolveExternalModule, when the module-not-found diagnostic is the generic TS2307 and the referenced module name is non-relative, getCannotResolveModuleNameErrorForGlobalNamespace checks the global scope for a symbol with that name whose declarations include a module declaration with a non-string name. The lookup runs only on the failure path, so successful resolutions are unaffected.

Test: cannotFindModuleWithGlobalNamespace.ts covers both a matching global namespace (new message) and an import with no matching global symbol (unchanged TS2307).

Verified locally:

  • npx hereby build
  • go -C ./tsc test -run TestLocal ./internal/testrunner (full local suite passes)
  • npx hereby generate:diagnostics (generated files updated, baseline accepted)
  • npx hereby lint (0 issues)
  • npx hereby check:format

Copilot AI balanced review requested due to automatic review settings October 10, 2026 14:23
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 10, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Oct 10, 2026

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.

🟡 Changes recommended

The linked issue’s TS7016 path remains unaffected, and the error uses the message-only TS95xxx code range.

2 open findings
What changed in this PR

Adds a targeted diagnostic when an unresolved import matches a global namespace.

Changes:

  • Detects matching global namespace declarations during failed module resolution.
  • Adds TS95210 and generated diagnostic artifacts.
  • Adds compiler coverage and baselines.
File Description
tsc/​internal/​checker/​checker.go Selects the namespace-specific diagnostic.
tsc/​internal/​diagnostics/​diagnosticMessages.json Defines TS95210.
tsc/​internal/​diagnostics/​diagnostics_generated.go Adds generated Go diagnostic data.
tsc/​internal/​diagnostics/​diagnosticMessages.generated.json Adds generated diagnostic text.
tsc/​testdata/​tests/​cases/​compiler/​cannotFindModuleWithGlobalNamespace.ts Adds the regression case.
tsc/​testdata/​baselines/​reference/​compiler/​cannotFindModuleWithGlobalNamespace.errors.txt Captures expected diagnostics.
tsc/​testdata/​baselines/​reference/​compiler/​cannotFindModuleWithGlobalNamespace.js Captures emit output.
tsc/​testdata/​baselines/​reference/​compiler/​cannotFindModuleWithGlobalNamespace.symbols Captures symbol output.
tsc/​testdata/​baselines/​reference/​compiler/​cannotFindModuleWithGlobalNamespace.types Captures type output.
Files not reviewed (1)
  • tsc/internal/diagnostics/diagnostics_generated.go: Generated file

🧠 Review effort: Balanced


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

Comment thread tsc/internal/checker/checker.go Resolved
@mahesh-desu

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

When a non-relative import fails to resolve and a global namespace with the same name exists, report the global-namespace guidance instead of the generic module-not-found error. The new diagnostic is TS18142; generated diagnostics and the compiler baseline are regenerated.

Fixes microsoft#23185

Signed-off-by: MaheshDesu <mahesh.desu3@gmail.com>
@mahesh-desu
MaheshDesu (mahesh-desu) force-pushed the fix/23185-global-namespace-module-error branch from 6a0f817 to 07002bb Compare November 28, 2026 07:36
@typescript-automation

Copy link
Copy Markdown
Contributor

The TypeScript team hasn't accepted the linked issue #23185. If you can get it accepted, this PR will have a better chance of being reviewed.

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 Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Cannot Load Custom Definition File in Repository

2 participants