Stale Semantic Tokens After didClose/didOpen (original) (raw)

Summary

When an LSP documention is closed and immediately reopened by the editor with completely different content (simulating an external file modification detected via file watchers), ty's response to the subsequent textDocument/semanticTokens/full response returns stale data from the previously opened version.

  1. Open a Python file with this content (Version A):

def calculate_sum(a): # Version A: Basic math return a

result = calculate_sum(5)

  1. Request semantic tokens - server correctly responds with tokens for this content
  2. Close the file via textDocument/didClose
  3. Immediately reopen the same file via textDocument/didOpen with completely different content (Version B):

Version B: Basic greeting

def say_hello(): print("Hello, World!")

say_hello()

  1. Request semantic tokens again

The expectation is that the server returns semantic tokens matching the new content (Version B with say_hello function).

Instead, it returns the exact same semantic tokens data from the previous version (Version A with calculate_sum function).

The current workaround is to kill the ty session and connect again.

In the attached transcript.

  1. Initial didOpen with Version A:
    • Content: "def calculate_sum(a):\n # Version A: Basic math\n return a\n\nresult = calculate_sum(5)\n"
  2. First semanticTokens/full[3] response:
    • Result: {"data":[0,4,13,7,1,0,14,1,2,1,2,11,1,2,0,2,0,6,5,1,0,9,13,7,0,0,14,1,11,0]}
  3. didClose followed by didOpen with Version B:
    • Content: "# Version B: Basic greeting\ndef say_hello():\n print(\"Hello, World!\")\n\nsay_hello()\n"
  4. Second semanticTokens/full[8] response:
    • Result: {"data":[0,4,13,7,1,0,14,1,2,1,2,11,1,2,0,2,0,6,5,1,0,9,13,7,0,0,14,1,11,0]}
    • ← IDENTICAL to response from step 2!

The semantic tokens data is byte-for-byte identical despite the file content being completely different. This causes incorrect syntax highlighting in the editor.

transcript.txt

Version

ty 0.0.9