Repository navigation
Improve error when an unresolved import matches a global namespace - #64726
Open
MaheshDesu (mahesh-desu) wants to merge 2 commits into
Open
MaheshDesu (mahesh-desu) wants to merge 2 commits into
MaheshDesu (mahesh-desu) wants to merge 2 commits into
Conversation
When a non-relative import fails to resolve and a global namespace with the same name exists (for example, one declared with an unquoted module declaration instead of a quoted ambient module name), report a more specific error instead of the generic one. Fixes microsoft#23185 Signed-off-by: MaheshDesu <mahesh.desu3@gmail.com>
Contributor
There was a problem hiding this comment.
🟡 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.
Author
|
@microsoft-github-policy-service agree |
Route the global-namespace guidance through errorOnImplicitAnyModule so imports that resolve to an untyped JavaScript module get the improved message, matching the linked repro. Use error code 18118 instead of the message-only 95xxx range. Extend the regression test with the resolved JavaScript package layout and an unresolved import that matches a global namespace. Signed-off-by: MaheshDesu <mahesh.desu3@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #23185.
When a non-relative import fails to resolve and a global namespace with the same name exists, the generic
Cannot find moduleerror is misleading - the declaration exists, but it declares a namespace rather than an ambient module. This reports a dedicated error for that case:This covers declarations like
declare module foo { }(ordeclare namespace foo { }), which do not provide an importable external module.New diagnostic: TS95210.
Implementation: in
resolveExternalModule, when the module-not-found diagnostic is the generic TS2307 and the referenced module name is non-relative,getCannotResolveModuleNameErrorForGlobalNamespacechecks 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.tscovers both a matching global namespace (new message) and an import with no matching global symbol (unchanged TS2307).Verified locally:
npx hereby buildgo -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