Skip to content

fix(OrgCache): Cache miss failure on buffers with loaded org files#1121

Open
dtvillafana wants to merge 1 commit intonvim-orgmode:masterfrom
dtvillafana:cache-fix
Open

fix(OrgCache): Cache miss failure on buffers with loaded org files#1121
dtvillafana wants to merge 1 commit intonvim-orgmode:masterfrom
dtvillafana:cache-fix

Conversation

@dtvillafana
Copy link
Copy Markdown

@dtvillafana dtvillafana commented Mar 26, 2026

Summary

Org buffers opened via BufRead were not added to orgmode’s buffer cache. For .org files, OrgBuffers.get_buffer_by_filename() only consulted that cache and skipped the bufnr() fallback, so a command like 'cit' on a header could fail with “No valid buffer for file” even when the file was loaded.

This fixes the issue in two places: org buffers are now registered on both BufNew and BufRead, and get_buffer_by_filename() now falls back to vim.fn.bufnr() for org files as well. That makes loaded org buffers resolvable even if the cache misses them.

The reason this could be an issue (and was for me) is that restoring a session did not trigger BufNew, and even when I closed the buffer with the org file, and re-opened it, it still was not added to the cache.

to verify this issue, I ran the following on the buffer:

:lua print("cur name:", vim.api.nvim_buf_get_name(0))
:lua print("cur bufnr:", vim.api.nvim_get_current_buf())
:lua print("loaded:", vim.api.nvim_buf_is_loaded(0))
:lua local B=require('orgmode.state.buffers'); print("cache hit:", vim.inspect(B._bufs[B._resolve_filename(vim.api.nvim_buf_get_name(0))]))
:lua local B=require('orgmode.state.buffers'); print("fallback bufnr:", vim.fn.bufnr(B._resolve_filename(vim.api.nvim_buf_get_name(0))))

and got the following results

cur name: /home/vir/git-repos/orgfiles/personal/calendar.org
cur bufnr: 79
loaded: true
cache hit: nil
fallback bufnr: 79

Closes #
Should I create a corresponding issue?

Checklist

I confirm that I have:

  • Followed the
    Conventional Commits
    specification
    (e.g., feat: add new feature, fix: correct bug,
    docs: update documentation).
  • My PR title also follows the conventional commits specification.
  • Updated relevant documentation, if necessary.
  • Thoroughly tested my changes.
  • Added tests (if applicable) and verified existing tests pass with
    make test. (tests fail on my NixOS machine)
  • Checked for breaking changes and documented them, if any.

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.

1 participant