Fix: Handle Negative VLQ Offsets in Source Maps for VSCode Jump to Definition #2419
+230
−1
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.
Problem
The
getMappedLocationfunction was creating inverted text ranges when source maps contained negative VLQ (Variable Length Quantity) offsets. Negative offsets are legal and valid in the source map specification - they occur when declarations in the generated.d.tsfile are out of order relative to the source file (non-monotonic mappings).When VSCode received these inverted ranges in LSP location responses, it mis-interpreted them, causing jump to definition to fail or navigate to incorrect locations. This was particularly problematic for projects like Convex, which generate
api.d.tsfiles with potentially out-of-order references that result in negative VLQ offsets in their source maps.Root cause:
.d.tsmaps to position 800 in source, but position 200 in.d.tsmaps to position 200 in source.d.ts, the end position (200) would be before the start position (800) in the source, creating an invalid inverted rangeSolution
Added validation logic in
getMappedLocationto ensure the end position is always valid:Changes
internal/ls/source_map.go: Added validation and clamping logic to prevent inverted rangesinternal/ls/source_map_test.go: Added comprehensive test suite (219 lines) covering:Testing
The fix includes extensive test coverage with multiple test cases:
TestGetMappedLocation_NonMonotonicMappings: Tests the core fix for non-monotonic mappingsTestGetMappedLocation_ZeroLengthRange: Verifies zero-length ranges are handled correctlyTestValidateAndClampSpanLogic: Directly tests the validation logicAll tests pass and verify that:
Impact
.d.tsfiles with out-of-order declarations