Skip to content

Support Ruby implementations without Ractor or RubyVM - #86

Open
sampokuokkanen wants to merge 2 commits into
ruby:masterfrom
sampokuokkanen:support-without-ractor-rubyvm
Open

sampokuokkanen wants to merge 2 commits into
ruby:masterfrom
sampokuokkanen:support-without-ractor-rubyvm

Conversation

@sampokuokkanen

Copy link
Copy Markdown

error_highlight uses Ractor and RubyVM unconditionally. On JRuby, which has neither, formatting an error message fails with NameError: uninitialized constant ErrorHighlight::RubyVM, and changing the formatter settings fails with ...DefaultFormatter::Ractor.

With this change, implementations without them fall back gracefully:

  • Without Ractor, the formatter settings live in module instance variables. They're shared across threads like Ractor-local storage, so a formatter set at boot still applies in worker threads. I picked instance variables over a Hash because JRuby serializes concurrent ivar updates but doesn't make Hash thread-safe (https://github.com/jruby/jruby/wiki/Concurrency-in-jruby).
  • RubyVM::AbstractSyntaxTree is only used when it exists. Other implementations can provide nodes through Thread::Backtrace::Location#syntax_tree (7755b9b); otherwise spot returns nil and no snippet is shown.

TruffleRuby currently patches its bundled copy around the same problem (switching to fiber-local Thread.current storage and skipping NameError highlighting), and could drop those patches with this.

sampokuokkanen and others added 2 commits November 6, 2026 14:57
error_highlight referenced Ractor and RubyVM unconditionally. On JRuby,
which defines neither, formatting an error message fails with
`NameError: uninitialized constant ErrorHighlight::RubyVM`, and changing
the formatter settings fails with `NameError: uninitialized constant
ErrorHighlight::DefaultFormatter::Ractor`.

* When Ractor is not available, store the formatter settings in module
  instance variables. Like Ractor-local storage, they are shared by all
  threads. Unlike Hash, which is not thread-safe on JRuby, concurrent
  instance variable updates are serialized
  (https://github.com/jruby/jruby/wiki/Concurrency-in-jruby), so threads
  that format errors concurrently cannot corrupt the settings. Both
  storages are behind a private ErrorHighlight::LocalSettings module.
* Only consult RubyVM::AbstractSyntaxTree when it is defined. Other
  implementations can provide nodes through
  Thread::Backtrace::Location#syntax_tree; without it, spot returns nil.
* Guard the RubyVM references in the tests, and test the settings with
  Ractor hidden so that the non-Ractor storage also runs on CRuby.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ruby loads error_highlight at boot unless it is disabled, so the child
process loaded it before the script hid Ractor and took the Ractor branch.
It only passed where RUBYOPT had --disable-error_highlight, and failed in
CI, where bundle exec also adds -rbundler/setup to RUBYOPT. Run the child
with --disable-gems and without RUBYOPT.
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