Skip to content

Conversation

@seflue
Copy link
Contributor

@seflue seflue commented Dec 29, 2025

Summary

This PR fixes intermittent "Index out of bounds" errors when treesitter nodes become stale after buffer modifications.

The error occurs when:

  1. Buffer is parsed, nodes are cached
  2. Buffer content changes (user edits)
  3. Cached nodes are accessed via async callback (e.g., vim.schedule)
  4. Node positions now point to invalid ranges → crash

Example error:

Error executing vim.schedule lua callback: .../treesitter.lua:195: Index out of bounds
stack traceback:
        [C]: in function 'nvim_buf_get_text'
        .../treesitter.lua:195: in function 'get_node_text'
        .../orgmode/files/headline.lua:545: in function 'get_property'

Related Issues

Changes

  • Wrap get_node_text() in pcall to gracefully handle stale nodes
  • Return empty string on failure instead of crashing
  • Add _parse_tick field to track buffer state at parse time
  • Add OrgFile:is_tree_stale() for callers that need explicit staleness checks

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.
  • Checked for breaking changes and documented them, if any.

Copy link
Member

@kristijanhusak kristijanhusak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! It seems unfinished though.

@seflue seflue force-pushed the fix/treesitter-parsing-errors branch from 8c61052 to 5a46807 Compare January 4, 2026 20:52
@seflue seflue requested a review from kristijanhusak January 4, 2026 20:56
When a buffer changes after parsing, node positions become invalid.
Calling get_node_text on stale nodes throws "Index out of bounds"
errors, crashing async callers like org-roam.

Changes:
- Wrap get_node_text in pcall, return '' on failure
- Add is_tree_stale() for callers needing explicit detection
- Track buffer changedtick at parse time for comparison
@seflue seflue force-pushed the fix/treesitter-parsing-errors branch from 5a46807 to e7dd352 Compare January 25, 2026 13:00
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.

2 participants