Skip to content

Commit 390ef0f

Browse files
sampokuokkanenclaude
authored andcommitted
Support Ruby implementations without Ractor or RubyVM
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>
1 parent eb7a3a2 commit 390ef0f

3 files changed

Lines changed: 77 additions & 11 deletions

File tree

‎lib/error_highlight/base.rb‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ def self.spot(obj, **opts)
5858

5959
Spotter.new(node, **opts).spot
6060

61-
when RubyVM::AbstractSyntaxTree::Node, prism::Node
62-
Spotter.new(obj, **opts).spot
63-
6461
else
65-
raise TypeError, "Exception is expected"
62+
if (defined?(RubyVM::AbstractSyntaxTree::Node) && RubyVM::AbstractSyntaxTree::Node === obj) || # CRuby only
63+
prism::Node === obj
64+
Spotter.new(obj, **opts).spot
65+
else
66+
raise TypeError, "Exception is expected"
67+
end
6668
end
6769

6870
rescue SyntaxError,
@@ -77,6 +79,9 @@ def self.find_node_from_loc(loc)
7779
return loc.syntax_tree
7880
end
7981

82+
# Without Thread::Backtrace::Location#syntax_tree, nodes can only be located through RubyVM, which is CRuby-only
83+
return nil unless defined?(RubyVM::AbstractSyntaxTree)
84+
8085
begin
8186
RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
8287
rescue RuntimeError => error

‎lib/error_highlight/formatter.rb‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def self.message_for(spot)
3737
end
3838

3939
def self.max_snippet_width
40-
return if Ractor.current[:__error_highlight_max_snippet_width__] == :disabled
40+
return if LocalSettings[:max_snippet_width] == :disabled
4141

42-
Ractor.current[:__error_highlight_max_snippet_width__] ||= terminal_width
42+
LocalSettings[:max_snippet_width] ||= terminal_width
4343
end
4444

4545
def self.max_snippet_width=(width)
46-
return Ractor.current[:__error_highlight_max_snippet_width__] = :disabled if width.nil?
46+
return LocalSettings[:max_snippet_width] = :disabled if width.nil?
4747

4848
width = width.to_i
4949

@@ -52,7 +52,7 @@ def self.max_snippet_width=(width)
5252
width = MIN_SNIPPET_WIDTH
5353
end
5454

55-
Ractor.current[:__error_highlight_max_snippet_width__] = width
55+
LocalSettings[:max_snippet_width] = width
5656
end
5757

5858
def self.terminal_width
@@ -64,11 +64,45 @@ def self.terminal_width
6464
end
6565
end
6666

67+
# The formatter settings are Ractor-local. On Ruby implementations without
68+
# Ractor (e.g., JRuby), they are module instance variables instead, which are
69+
# likewise shared by all threads. Unlike Hash, which is not thread-safe on
70+
# JRuby, concurrent instance variable updates are serialized
71+
# (https://github.com/jruby/jruby/wiki/Concurrency-in-jruby), so threads
72+
# formatting errors concurrently cannot corrupt the settings.
73+
module LocalSettings
74+
RACTOR_KEYS = {
75+
formatter: :__error_highlight_formatter__,
76+
max_snippet_width: :__error_highlight_max_snippet_width__,
77+
}.freeze
78+
79+
if defined?(Ractor)
80+
def self.[](name)
81+
Ractor.current[RACTOR_KEYS.fetch(name)]
82+
end
83+
84+
def self.[]=(name, value)
85+
Ractor.current[RACTOR_KEYS.fetch(name)] = value
86+
end
87+
else
88+
IVARS = RACTOR_KEYS.to_h { |name, _| [name, :"@#{ name }"] }.freeze
89+
90+
def self.[](name)
91+
instance_variable_get(IVARS.fetch(name))
92+
end
93+
94+
def self.[]=(name, value)
95+
instance_variable_set(IVARS.fetch(name), value)
96+
end
97+
end
98+
end
99+
private_constant :LocalSettings
100+
67101
def self.formatter
68-
Ractor.current[:__error_highlight_formatter__] || DefaultFormatter
102+
LocalSettings[:formatter] || DefaultFormatter
69103
end
70104

71105
def self.formatter=(formatter)
72-
Ractor.current[:__error_highlight_formatter__] = formatter
106+
LocalSettings[:formatter] = formatter
73107
end
74108
end

‎test/test_error_highlight.rb‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,7 @@ def exc.backtrace_locations = []
14541454
->{}.call(1)
14551455
rescue ArgumentError => exc
14561456
MethodDefLocationSupported =
1457+
defined?(RubyVM::AbstractSyntaxTree) &&
14571458
RubyVM::AbstractSyntaxTree.respond_to?(:node_id_for_backtrace_location) &&
14581459
RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(exc.backtrace_locations.first)
14591460
end
@@ -1689,7 +1690,7 @@ def test_spot_with_backtrace_location
16891690
end
16901691

16911692
def test_spot_with_node
1692-
omit unless RubyVM::AbstractSyntaxTree.respond_to?(:node_id_for_backtrace_location)
1693+
omit unless defined?(RubyVM::AbstractSyntaxTree) && RubyVM::AbstractSyntaxTree.respond_to?(:node_id_for_backtrace_location)
16931694

16941695
# We can't revisit instruction sequences to find node ids if the prism
16951696
# compiler was used instead of the parse.y compiler. In that case, we'll
@@ -1846,6 +1847,32 @@ def test_detailed_message_does_not_raise_when_backtrace_locations_is_nil
18461847
end
18471848
end
18481849

1850+
def test_settings_without_ractor
1851+
# Ruby implementations without Ractor (e.g., JRuby) store the settings
1852+
# differently, so load error_highlight with Ractor hidden.
1853+
lib = File.expand_path("../..", ErrorHighlight.method(:spot).source_location[0])
1854+
script = <<~'RUBY'
1855+
Object.send(:remove_const, :Ractor) if defined?(Ractor)
1856+
require "error_highlight"
1857+
1858+
formatter = Object.new
1859+
ErrorHighlight.formatter = formatter
1860+
ErrorHighlight::DefaultFormatter.max_snippet_width = 40
1861+
p ErrorHighlight.formatter.equal?(formatter)
1862+
p Thread.new { [ErrorHighlight.formatter.equal?(formatter), ErrorHighlight::DefaultFormatter.max_snippet_width] }.value
1863+
ErrorHighlight.formatter = nil
1864+
ErrorHighlight::DefaultFormatter.max_snippet_width = nil
1865+
p ErrorHighlight.formatter, ErrorHighlight::DefaultFormatter.max_snippet_width
1866+
RUBY
1867+
out = IO.popen([RbConfig.ruby, "-I", lib, "-e", script], err: [:child, :out], &:read)
1868+
assert_equal(<<~END, out)
1869+
true
1870+
[true, 40]
1871+
ErrorHighlight::DefaultFormatter
1872+
nil
1873+
END
1874+
end
1875+
18491876
private
18501877

18511878
def find_node_by_id(node, node_id)

0 commit comments

Comments
 (0)