Fix TS2878 error location reporting by adding errorNode checks #62437
+599
−4
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 an issue where TS2878 ("This import path is unsafe to rewrite because it resolves to another project...") and related import/export diagnostics were not being reported at the correct source locations, preventing red squiggles from appearing in the editor despite the errors being shown in the output.
Problem
When TypeScript encounters unsafe import path rewriting scenarios, the compiler would report TS2878 errors but without proper source location information. This meant that while the error appeared in the compiler output, IDEs wouldn't show red squiggles at the problematic import statements, making it harder for developers to identify and fix the issues.
Root Cause
The issue was in
src/compiler/checker.ts
where three error reporting branches were callingerror()
without first checking iferrorNode
exists. WhenerrorNode
is undefined, the error gets reported but without proper location information.Solution
Added
errorNode &&
checks before the relevant conditions in three places:This ensures that diagnostic errors are only reported when we have a valid error node to attach the location information to.
Example
With a project structure that triggers TS2878:
Testing
tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences4.ts
that demonstrates the error being reported at the correct location (line 1, column 24)The fix is minimal and surgical, ensuring that error location reporting works correctly while maintaining all existing functionality.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
plugins.dprint.dev
/home/REDACTED/work/TypeScript/TypeScript/node_modules/dprint/dprint fmt
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
This pull request was created as a result of the following prompt from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.