Skip to content

fix: don't crash when an aerial query file fails to parse - #519

Merged
stevearc merged 2 commits into
stevearc:masterfrom
Booyaka101:fix/506-pcall-query-load
Jun 2, 2026
Merged

fix: don't crash when an aerial query file fails to parse#519
stevearc merged 2 commits into
stevearc:masterfrom
Booyaka101:fix/506-pcall-query-load

Conversation

@Booyaka101

Copy link
Copy Markdown
Contributor

Closes #506.

vim.treesitter.query.get(lang, \"aerial\") raises whenever the query file references node types absent from the installed grammar. The current reported case: SQL files crash aerial with

Query error at 37:2. Invalid node type \"create_policy\":
(create_policy
 ^

because queries/sql/aerial.scm uses create_policy, which was added to tree-sitter-sql in v0.3.10 (2026-01) and is missing from older parsers (very common when using nvim-treesitter master branch on Neovim < 0.12 — see DerekStride/tree-sitter-sql#347).

The fix

Wrap query.get in pcall and return nil on parse failure inside get_query. Both existing callers already handle nil (init.lua:22 in is_supported; init.lua:184 in get_lang_and_query), so aerial just treats the language as treesitter-unsupported for that one user instead of crashing on every buffer enter. Emit a single user-facing warning; cache the failure so the warning doesn't spam on every buffer.

This is intentionally a defensive layer only — I'm not removing create_policy from queries/sql/aerial.scm since that was added on purpose in #505 and might be used by people on newer grammars. If you'd like to additionally restructure that file (e.g. split into a stable core + extras, or drop create_policy to keep partial outline working on old grammars), happy to follow up in a separate PR.

How this was tested

End-to-end reproduction with a synthetic queries dir that references a non-existent node type for an installed grammar (lua):

Behavior
Baseline `FAIL: get_query raised: ...Query error at 5:2. Invalid node type "__deliberately_missing_node_simulating_create_policy"`
With fix `aerial: failed to load treesitter query for lua, treating as unsupported...` warning logged, `get_query` returns `nil`

3 regression tests in `tests/helpers_spec.lua`:

  • returns nil instead of crashing when the query fails to parse
  • caches the parse failure so the warning only fires once (across repeated calls)
  • still returns the query when parsing succeeds

All 3 pass with the fix; 2/3 fail on baseline (the success-case test passes either way).

`vim.treesitter.query.get(lang, "aerial")` raises whenever the query
file references node types absent from the installed grammar. Reported
in stevearc#506: SQL files crash aerial with `Query error at 37:2. Invalid node
type "create_policy"` because `queries/sql/aerial.scm` uses
`create_policy`, which was added to tree-sitter-sql in v0.3.10 (2026-01)
and is missing from older parsers (common on nvim-treesitter master
branch / Neovim < 0.12).

Wrap `query.get` in `pcall` and return `nil` on parse failure. Both
existing callers of `get_query` already handle `nil` (init.lua:22 in
`is_supported`; init.lua:184 in `get_lang_and_query`), so aerial just
treats the language as treesitter-unsupported for that one user instead
of crashing on every buffer enter.

Emit a single warning so the user can see they need to `:TSUpdate
<lang>` or switch nvim-treesitter branches; the cache stores the failure
so the warning doesn't spam on every buffer.

Closes stevearc#506.
@github-actions
github-actions Bot requested a review from stevearc May 24, 2026 04:52

@stevearc stevearc left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This has the look of minimally edited LLM output. Just from skimming the changes that I initially want to see are:

  • remove the unnecessary, overly verbose comments
  • instead of logging these errors, make them surface in the :AerialInfo diagnostics
  • ensure that all tests are valuable and we're not just testing trivial logic

@Booyaka101

Copy link
Copy Markdown
Contributor Author

Thanks, pushed an update — errors now flow through is_supported so they show in :AerialInfo. Also trimmed the comments and dropped the trivial test.

@github-actions
github-actions Bot requested a review from stevearc May 26, 2026 01:24
@stevearc

stevearc commented Jun 2, 2026

Copy link
Copy Markdown
Owner

LGTM, thanks for the PR!

@stevearc
stevearc merged commit 18ffb63 into stevearc:master Jun 2, 2026
7 checks passed
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.

bug: Latest aerial update error when opening .sql file

2 participants