pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

LSP: missing `save` in TextDocumentSyncOptions prevents cross-file invalidation under spec-conformant clients (Zed)

Open NathanTempest opened this issue 5 months ago • 1 comments

Describe the Bug

In project-config mode (pyrefly.toml or [tool.pyrefly] present), edits to a dependency file leave dependent open files showing stale diagnostics. Only manifests in Zed (and any other strictly spec-conformant LSP client); VS Code and Helix are unaffected.

Reproduction In Zed with pyrefly installed:

uv init test_project && cd test_project
pyrefly init
mkdir -p test_project
touch test_project/bar.py
printf 'from test_project.bar import FizzBuzz\n' > test_project/foo.py

Open both files in Zed. foo.py correctly shows Could not import FizzBuzz from test_project.bar (expected). Now in bar.py, add: class FizzBuzz: pass Save with Cmd+S. Switch back to foo.py. The "Could not import" error persists. Restarting the language server (Cmd+Shift+P → "editor: restart language server") clears it.

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

NathanTempest avatar Jun 24 '26 14:06 NathanTempest

Root cause Pyrefly's LSP advertises its sync capability using the shorthand form:

text_document_sync: Some(TextDocumentSyncCapability::Kind(
    TextDocumentSyncKind::INCREMENTAL,
)),

Per the LSP spec, this shorthand only enables didChange servers must explicitly include save inside TextDocumentSyncOptions to opt into didSave delivery. VS Code and Helix historically send didSave regardless (lenient reading of the spec), which masked the issue. Zed tightened its client to strict spec compliance in zed-industries/zed#36441 / #36485, so pyrefly under Zed no longer receives save events.

Pyrefly's cross-file invalidation in project-config mode is gated on didSave → invalidate_disk (in-editor didChange intentionally stays local). With didSave never arriving, dependents are never re-validated.

Verified by capturing pyrefly's LSP event log under Zed: ~30 DidChangeTextDocument entries from typing in a dependency file, zero DidSaveTextDocument entries after Cmd+S. Peer Python LSPs (ty, basedpyright) are unaffected because they propagate on every didChange rather than depending on save.

Proposed fix Switch the capability declaration to the full Options form with explicit save:

text_document_sync: Some(TextDocumentSyncCapability::Options(
    TextDocumentSyncOptions {
        open_close: Some(true),
        change: Some(TextDocumentSyncKind::INCREMENTAL),
        save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
            include_text: Some(false),
        })),
        ..Default::default()
    },
)),

include_text: false because pyrefly already has the buffer contents from prior didChanges. The existing did_save handler does the right thing once invoked.

Related zed-extensions/pyrefly#19 user reports under Zed zed-industries/zed#52984 parent Zed issue zed-industries/zed#55730 same root cause hit by ansible-language-server; explains the Zed-side spec-compliance change

NathanTempest avatar Jun 24 '26 14:06 NathanTempest