Skip to content

[Bug]: nvim - LSP Restarts required to show and clear balance #685

Closed

Description

@lutzky

Description

Errors seem to only show up or disappear on LSP Restarts. See attached asciinema.

asciicast

Steps to Reproduce

Create errors and clear them, with or without saving the file. See asciinema above.

Expected Behavior

Errors should appear or disappear based on the correctness of the file as-you-type

Minimal Beancount File


Version

68b153b (main today)

Operating System

Linux

Additional Context

No response

Activity

  1. added
    bug
    on Apr 6, 2026
  2. robcohen commented on Apr 6, 2026

    @robcohen
    Member

    I analyzed the asciinema recording and the LSP source code. Here's what I found:

    Observed Behavior

    1. ~14s: User changes -5000 to -5001 (creates imbalance) — no error appears
    2. ~26s: Error finally appears (Transaction does not balance) — but only after what appears to be an LSP restart or save
    3. ~56s: User fixes it back to -5000 — error persists even though the text is now correct
    4. Error only clears after another LSP restart

    Code Analysis

    The on_did_change handler in main_loop.rs:1069 looks correct — it updates the VFS and calls publish_diagnostics synchronously with the new text. The publish_diagnostics function re-parses the document and runs validation fresh each time.

    Two possible root causes to investigate:

    1. Neovim client-side diagnostic caching

    The LSP sends PublishDiagnostics notifications (push model), but some neovim LSP client configurations may not immediately refresh inline diagnostics on every publishDiagnostics notification. Check if the user's neovim config has any diagnostic display settings that require manual refresh.

    2. textDocument/didChange not being sent

    If neovim is configured to only send didChange on save (not on every keystroke), the LSP never sees the buffer changes until :w. The server declares TextDocumentSyncKind::FULL which should cause neovim to send full content on every change, but some neovim LSP client wrappers may override this behavior.

    Debugging suggestion

    Add a tracing::info! in on_did_change that logs the text length and first few characters, then check the LSP log to verify didChange notifications are actually being received on each keystroke. If they are, the issue is on the nvim diagnostic display side. If they're not, the issue is in the nvim LSP client configuration.

    // In on_did_change, after getting the text:
    tracing::info!("didChange: {} bytes, starts with: {:?}", text.len(), &text[..text.len().min(50)]);
  3. robcohen commented on Apr 6, 2026

    @robcohen
    Member

    Confirmed: Server is working correctly, issue is client-side

    I tested the LSP server directly by sending JSON-RPC messages and verified diagnostics update correctly:

    === After didOpen (balanced) ===
    Diagnostics: 0
    
    === After didChange (unbalanced -5001) ===
    Diagnostics: 1
      1: Transaction does not balance: residual -1 USD (2024-01-15)
    
    === After didChange (fixed back to -5000) ===
    Diagnostics: 0
    

    The server sends publishDiagnostics notifications immediately and correctly after every textDocument/didChange.

    Likely cause

    This is a neovim client configuration issue. Common causes:

    1. The nvim LSP client is not forwarding didChange on every keystroke. Check if your LSP client config sets flags = { debounce_text_changes = ... } or similar. The server needs TextDocumentSyncKind.Full changes on every edit.

    2. Diagnostic display refresh. Some nvim diagnostic display plugins (or vim.diagnostic settings) don't refresh inline virtual text immediately. Try:

      vim.diagnostic.config({ update_in_insert = true })
    3. If using a wrapper like nvim-lspconfig, ensure the on_attach callback doesn't override diagnostic refresh behavior.

    Quick test

    You can verify if didChange is being sent by checking the LSP log:

    :lua vim.lsp.set_log_level("debug")

    Then edit a file and check ~/.local/state/nvim/lsp.log for didChange messages.

  4. robcohen commented on Apr 6, 2026

    @robcohen
    Member

    Update: Verified working with stock Neovim 0.11.7

    I tested with a minimal nvim config (no plugins, no lspconfig) and the LSP works correctly — diagnostics update live as you type:

    Before edit: 0 diagnostics
    After changing -5000 to -5001: 1 diagnostic (E3001: Transaction does not balance)
    After fixing back: 0 diagnostics
    

    The LSP log confirms textDocument/didChange is sent and publishDiagnostics is received immediately.

    The issue is in your neovim configuration, not the LSP server. Most likely causes:

    1. A plugin debouncing didChange — some LSP plugins (e.g., older nvim-lspconfig configs) add flags.debounce_text_changes which delays sending changes to the server
    2. update_in_insert = false (the default) — nvim won't display new diagnostics while in insert mode. Add to your config:
      vim.diagnostic.config({ update_in_insert = true })
    3. A diagnostic display plugin (like trouble.nvim or custom virtual text handlers) overriding the default diagnostic rendering

    To reproduce the working behavior

    Save this as /tmp/init.lua and run nvim -u /tmp/init.lua test.bean:

    vim.lsp.set_log_level("debug")
    vim.api.nvim_create_autocmd("FileType", {
      pattern = "beancount",
      callback = function()
        vim.lsp.start({
          name = "rledger-lsp",
          cmd = { "rledger-lsp" },  -- adjust path if needed
          root_dir = vim.fn.getcwd(),
        })
      end,
    })
    vim.filetype.add({ extension = { bean = "beancount", beancount = "beancount" } })
    vim.diagnostic.config({ update_in_insert = true, virtual_text = true, signs = true, underline = true })

    If diagnostics work with this minimal config but not your normal config, bisect your plugins to find the culprit.

  5. robcohen commented on Apr 6, 2026

    @robcohen
    Member

    Please reopen this issue if you determine it is not a client side issue.

  6. lutzky commented on Apr 7, 2026

    @lutzky
    Author

    The diagnostics still don't behave correctly with the minimal config.

    asciicast

    Note, I don't have permission to reopen the issue.

  7. robcohen commented on Apr 11, 2026

    @robcohen
    Member

    @lutzky you were right, this is a server-side bug and my earlier diagnosis was wrong. Apologies for the runaround.

    The JSON-RPC test I ran earlier didn't have a journal file loaded, which took a different code path than the one your nvim setup actually hits. With a real journal the LSP uses directives from ledger_state (loaded from disk at startup, refreshed only by file-watcher events on save) rather than from the fresh parse of the buffer. In-memory edits never reach validation until you save, which is exactly what you were seeing.

    Root cause

    crates/rustledger-lsp/src/handlers/diagnostics.rs:282 pulls full_directives from ledger_state, and validation_errors_to_diagnostics at line 147 prefers that over the fresh parse whenever a current_file_id is known:

    let directives_to_validate = if current_file_id.is_some() {
        full_directives.unwrap_or(directives)  // stale directives win
    } else {
        directives  // fresh directives only used in single-file mode
    };

    ledger_state is loaded in main_loop.rs:141 and only reloaded via reload_journal() from the file-watcher. Buffer edits never touch it. So the sequence is:

    1. LSP starts, loads file from disk (balanced)
    2. You edit -5000 to -5001, nvim sends didChange
    3. publish_diagnostics parses the fresh text, finds current_file_id, then validates against the stale ledger_state directives and reports no error
    4. You save, file-watcher fires, reload_journal picks up the new disk content, diagnostics republish, error appears
    5. You fix it back to -5000 in the buffer, but ledger_state still holds the saved -5001, so the error persists until the next save

    That matches both your asciicast and the original report.

    Fix

    Live overlay at the all_diagnostics layer: when both ledger_state and current_file_id are available, build a merged directive list that takes full_directives minus the stale entries for current_file_id and appends the fresh parse from the buffer (remapped to current_file_id so the existing per-file filter still works). PR on the way.

  8. lutzky commented on Apr 11, 2026

    @lutzky
    Author

    Perhaps it's advisable to use plenary.nvim to create an integration test? Or VHS by charm for an end-to-end visual integration test?

  9. added a commit that references this issue on Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions