fix(files): handle stale treesitter nodes gracefully - #1076
Open
seflue wants to merge 2 commits into
Open
Conversation
kristijanhusak
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR! It seems unfinished though.
seflue
force-pushed
the
fix/treesitter-parsing-errors
branch
from
January 4, 2026 20:52
8c61052 to
5a46807
Compare
seflue
force-pushed
the
fix/treesitter-parsing-errors
branch
from
January 25, 2026 13:00
5a46807 to
e7dd352
Compare
Member
|
Do you know how can I reproduce this? Cache should revalidate when node changes, and I'm curious if we could handle it differently. |
seflue
force-pushed
the
fix/treesitter-parsing-errors
branch
from
February 9, 2026 01:17
e7dd352 to
7e9471b
Compare
Memoized results are keyed by the root node id, and the key is read before the method runs. A buffer edited since the last parse still carries the old root, so the cache hands back headlines whose nodes point into text that no longer exists. Reading them threw "Index out of bounds" for callers that walk files without a reload first (telescope-orgmode building its refile picker, org-roam). The memoize key getter now compares the buffer's changedtick with the one recorded at the last parse and parses first when they differ. The earlier approach of swallowing the error in get_node_text is dropped: it hid the stale data instead of refreshing it. Refs: nvim-orgmode#1076
Walks a buffer-backed file the way telescope-orgmode builds its refile picker: headlines, then a property, with no reload in between. Fails with "Index out of bounds" without the reparse. Refs: nvim-orgmode#1076
seflue
force-pushed
the
fix/treesitter-parsing-errors
branch
from
September 13, 2026 13:31
7e9471b to
b4c8e67
Compare
kristijanhusak
requested changes
Sep 13, 2026
kristijanhusak
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR, and sorry for the delay.
I left a comment on the logic, we can simplify it a bit.
| local memoize = Memoize:new(OrgFile, function(self) | ||
| local bufnr = self:bufnr() | ||
| if bufnr > -1 and self._parse_tick ~= vim.api.nvim_buf_get_changedtick(bufnr) then | ||
| self:parse() |
Member
There was a problem hiding this comment.
No need to parse here, the method that is being called should do the parsing when it's really called. We just need to make sure that the cache is invalidated. We can append the changedtick to the id, and that does the trick. This works for me:
local memoize = Memoize:new(OrgFile, function(self)
local tick = 0
local bufnr = self:bufnr()
if bufnr and bufnr > -1 then
tick = vim.api.nvim_buf_get_changedtick(bufnr)
end
return {
file = self,
id = table.concat({ 'file', self.root and self.root:id() or '', tick }, '_'),
}
end)We also don't need to track the _parse_tick any more.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes intermittent "Index out of bounds" errors when treesitter nodes become stale after buffer modifications.
The error occurs when:
vim.schedule)Example error:
Related Issues
Changes
get_node_text()inpcallto gracefully handle stale nodes_parse_tickfield to track buffer state at parse timeOrgFile:is_tree_stale()for callers that need explicit staleness checksChecklist
I confirm that I have:
feat: add new feature,fix: correct bug,docs: update documentation).make test.