-
Notifications
You must be signed in to change notification settings - Fork 771
Fix: Handle Negative VLQ Offsets in Source Maps for VSCode Jump to Definition #2419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…suring it remains within bounds and avoids inverted ranges.
|
@sethconvex please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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