Skip to content

Fix token cache parent mismatch panics#2789

Merged
ahejlsberg merged 6 commits intomainfrom
fix-token-cache-issue
Feb 16, 2026
Merged

Fix token cache parent mismatch panics#2789
ahejlsberg merged 6 commits intomainfrom
fix-token-cache-issue

Conversation

@ahejlsberg
Copy link
Member

This PR fixes the 11 occurrences of "Token cache mismatch: parent" panics observed in #2778.

The PR includes a fourslash test that reproduces the issue in this run. The others are of a similar nature, so no need for further tests.

@ahejlsberg
Copy link
Member Author

ahejlsberg commented Feb 15, 2026

In an ideal world we wouldn't need a parent in the token cache key because a token a some location would always be a child of the same parent. However, the various token walking functions in tokens.go sometimes compute different parents in boundary conditions inside JSDoc comments, and the parent rarely matters. So, from a stability point of view, I think we're better off just saying that it is fine to have differently parented tokens at the same location.

Copilot AI review requested due to automatic review settings February 15, 2026 00:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes panics caused by token cache mismatches where tokens were cached by position alone, but different parent nodes could exist at the same position after reparsing. The solution changes the cache key to include both the parent node and position, and adds checks to skip reparsed nodes during traversal.

Changes:

  • Modified token cache to use parent + position as key instead of position alone
  • Added checks to skip reparsed nodes in AST traversal functions
  • Added regression test for JSDoc completion crash scenario

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
internal/ast/ast.go Introduced TokenCacheKey struct combining parent and position; updated tokenCache map key type and removed redundant parent validation
internal/astnav/tokens.go Added reparsed node checks to early-exit visitor functions, preventing traversal of stale AST nodes
internal/ls/lsutil/children.go Fixed logic to properly skip reparsed nodes using positive condition instead of double-negative
internal/fourslash/tests/completionJSDocNoCrash_test.go Added regression test verifying JSDoc completion doesn't crash

@ahejlsberg
Copy link
Member Author

@jakebailey Is there a typescript-bot command that'll kick off another fuzzer run?

@jakebailey
Copy link
Member

Nope, that run was manual, as Gabriela just added the pipeline.

@ahejlsberg ahejlsberg added this pull request to the merge queue Feb 15, 2026
Merged via the queue into main with commit 4b301b2 Feb 16, 2026
20 checks passed
@ahejlsberg ahejlsberg deleted the fix-token-cache-issue branch February 16, 2026 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants