-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Support tree-sitter Rust crate 0.26.x #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Update tree_stump to support the tree-sitter Rust crate 0.26.x, enabling grammars with LANGUAGE_VERSION 15 to be loaded. Changes: - Replace Language::version() with Language::abi_version() - Remove deprecated parser timeout/cancellation APIs - Handle Parser::language() returning Option<LanguageRef> - Convert usize to u32 for Node::child() and Node::named_child() - Use streaming_iterator for QueryMatches iteration - Update Cargo.toml to tree-sitter = "0.26.3" - Add streaming-iterator = "0.1" dependency BREAKING CHANGE: Parser#timeout_micros method is removed (deprecated in 0.25, removed upstream in 0.26). Parser#language now returns nil after setting a language due to API changes in the Rust crate. Closes joker1007#16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR updates the tree_stump Ruby gem to support tree-sitter Rust crate version 0.26.x, enabling compatibility with grammars using LANGUAGE_VERSION 15. The update involves adapting to breaking API changes in tree-sitter, including removal of timeout methods, API signature changes, and adoption of streaming iterators.
Key changes:
- Updated tree-sitter dependency from 0.22 to 0.26 with new dependencies (tree-sitter-language, streaming-iterator)
- Removed deprecated timeout methods from the Parser API
- Added new Node methods (
is_missing) and compatibility aliases for better API consistency - Updated CI workflow to test additional Ruby implementations and versions
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| ext/tree_stump/Cargo.toml | Updated tree-sitter to 0.26, added tree-sitter-language and streaming-iterator dependencies |
| Cargo.lock | Updated dependency versions and added new transitive dependencies for tree-sitter 0.26 |
| ext/tree_stump/src/lib.rs | Updated language loading to use LanguageFn, removed timeout method definitions, added Node method aliases |
| ext/tree_stump/src/language.rs | Updated version() to call abi_version(), added new abi_version() method |
| ext/tree_stump/src/parser.rs | Removed timeout methods, added unsafe lifetime extension for LanguageRef |
| ext/tree_stump/src/tree.rs | Added is_missing() method, converted usize to u32 for child access, renamed method call to child_with_descendant |
| ext/tree_stump/src/query.rs | Updated to use StreamingIterator for query matches iteration |
| .github/workflows/rspec.yml | Enhanced CI with additional Ruby versions, experimental implementations, and improved workflow configuration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6343278 to
c3960c3
Compare
Consolidate tree-sitter environment setup using the kettle-rb/ts-grammar-action composite action instead of separate steps for Java, tree-sitter, and Rust. Before: - actions/setup-java@v5 (conditional for TruffleRuby/JRuby) - tree-sitter/setup-action@v2 (library and CLI) - actions-rust-lang/setup-rust-toolchain@v1 (Rust for tree_stump) After: - kettle-rb/ts-grammar-action@v1 (all-in-one) - install-cli: true - install-lib: true - setup-rust: true (required for tree_stump Rust extension) - setup-java: conditional (for TruffleRuby/JRuby) This reduces workflow complexity and ensures consistent tree-sitter environment setup across kettle-rb projects. Closes joker1007#17
c3960c3 to
9ec6ef7
Compare
- usize can be larger than u32. - On conversion failure (index > 4,294,967,295) - Now returns None - Previously truncated the value
1309b56 to
718a127
Compare
- Remove unsound unsafe transmute in Parser::language() that extended LanguageRef lifetime from borrowed scope to 'static. The safety comment claimed languages outlive parsers, but this wasn't enforced by the type system and could lead to use-after-free. - Redesign Parser to store language_name and look up the language directly from the global LANG_LANGUAGES map in build_query(), keeping proper lifetime bounds tied to the lock guard.
Rust source changes (ext/tree_stump/src/*.rs): - Fix compilation error in parser.rs by changing Query::new to accept &tree_sitter::Language instead of &tree_sitter::LanguageRef - Replace deprecated magnus::exception::runtime_error() calls with build_error() helper throughout lib.rs, query.rs, and tree.rs - Replace deprecated magnus::exception::type_error() with ruby.exception_type_error() in query.rs - Fix deprecated into_symbol() -> into_symbol_with(&ruby) in query.rs - Fix deprecated into_value() -> into_value_with(ruby) in query.rs - Add #[allow(deprecated)] for unavoidable fallback case in util.rs - Fix lifetime elision warnings by adding explicit <'_> lifetime parameters to Node and TreeCursor return types in tree.rs Spec changes: - Update spec_helper.rb to use TREE_SITTER_RUBY_PATH env var for grammar path, falling back to local path for CI - Add is_missing? and missing? method tests for Node class - Add helper methods find_missing_nodes and check_all_nodes for tree traversal in tests
- Ruby 4.0, 4.1 compat - oxidize-rb/rb-sys#697
|
The last build failed because ruby-head tag in setup ruby now points at ruby 4.1.0. The last time it ran it worked because it was still using Ruby 4.0.0, and there is a breaking change in the C API. I have fixed it in rbs-sys and magnus, but rbs-sys-env and magnus both still need to be released:
@joker1007 This can be merged, but the ruby-head build will continue to fail until new versions are released for rbs-sys-env and magnus. |
|
Also, I added, and then removed, CI for JRuby and Truffleruby. I just wanted to see if it was a reasonable lift to add support for them. Turns out it was more than I can handle right now. |
|
@joker1007 The new rbs-sys-env has been released, but still waiting on magnus. |
- Change gemspec extensions from Cargo.toml to extconf.rb to use rb_sys/mkmf - Add rb_sys ~> 0.9.119 as runtime dependency (required by extconf.rb) - Add CI build verification steps to ensure extension compiles before tests - Add Rust version verification and explicit compile step in workflow - Improve error diagnostics for extension build failures Fixes issue where extension built successfully but .so file wasn't found in CI due to RubyGems using native Cargo builder instead of rb_sys.
|
@joker1007 I think we need to make rb-sys a runtime dependency in the gemspec, because the gem seems to not build without it in some scenarios. I'm not sure why yet. I've added this, but happy to take it back out if it doesn't belong and I've misdiagnosed my build failures. Oh, and magnus will not be released for ruby-head support. We'll have to target main until there is a new release with the support... Oh, lol, maybe that's why the build was failing... 😆 |
This PR updates tree_stump to support the tree-sitter Rust crate 0.26.x, enabling grammars with
LANGUAGE_VERSION 15to be loaded and used. It also fixes the unsafe memory transmute that extended theLanguageReflifetime from the borrowed scope.Changes
ext/tree_stump/Cargo.toml
ext/tree_stump/src/language.rs
self.raw_language_ref.version()withself.raw_language_ref.abi_version()Language#versionmethod now callsabi_version()internallyext/tree_stump/src/parser.rs
timeout_micros()andset_timeout_micros()methodsParser::language()returningOption<LanguageRef<'_>>instead ofOption<Language>- returnNoneto Ruby since we can't convertLanguageRefback to
Languagewithout cloningext/tree_stump/src/tree.rs
usizetou32when callingNode::child(index)using.try_into().unwrap()usizetou32when callingNode::named_child(index)child_containing_descendantto usechild_with_descendant(renamed in 0.26)ext/tree_stump/src/query.rs
streaming_iterator::StreamingIteratorforQueryMatchesiteration:for m in matches { ... }patternBreaking Changes
Ruby API: The following methods are removed from
TreeStump::Parser:timeout_micros/timeout_micros=These were deprecated in tree-sitter 0.25 and removed in 0.26. Users should use
alternative cancellation mechanisms (e.g., Ruby's Timeout module).
Ruby API:
Parser#languagenow returnsnilwhen called afterlanguage=.This is because tree-sitter 0.26's
Parser::language()returns a borrowed referencethat cannot be converted back to an owned
Language. If you need to track the language,store it separately before setting it on the parser.
Backward Compatibility
LANGUAGE_VERSION13-15 are supportedTesting
Closes #16, #17
Additional Fixes
Eliminate unsafe memory transmute and prevent integer truncation.
Parser::language()that extendedLanguageReflifetime from borrowed scope to 'static. The safety comment claimed languages outlive parsers, but this wasn't enforced by the type system and could lead to use-after-free.LANG_LANGUAGESmap inbuild_query(), keeping proper lifetime bounds tied to the lock guard.as u32casts with.try_into().ok()?inNode::child()andNode::named_child()to prevent silent value truncation on 64-bit systems whereusizecan exceedu32::MAX.Fix build errors and deprecation warnings for tree-sitter 0.26
Rust source changes (
ext/tree_stump/src/*.rs):parser.rsby changingQuery::newto accept&tree_sitter::Languageinstead of&tree_sitter::LanguageRefmagnus::exception::runtime_error()calls withbuild_error()helper throughoutlib.rs,query.rs, andtree.rsmagnus::exception::type_error()withruby.exception_type_error()inquery.rsinto_symbol()->into_symbol_with(&ruby)inquery.rsinto_value()->into_value_with(ruby)inquery.rs#[allow(deprecated)]for unavoidable fallback case inutil.rs<'_>lifetimeparameters to
NodeandTreeCursorreturn types intree.rsSpec changes:
spec_helper.rbto useTREE_SITTER_RUBY_PATHenv var forgrammar path, falling back to local path for CI
is_missing?andmissing?method tests forNodeclassfind_missing_nodesandcheck_all_nodesfortree traversal in tests