Skip to content

feat: Support Ruby 3.4 and 4.0 - #370

Open
dividedmind wants to merge 14 commits into
masterfrom
fix/ruby-3.4
Open

dividedmind wants to merge 14 commits into
masterfrom
fix/ruby-3.4

Conversation

@dividedmind

@dividedmind dividedmind commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

feat: Support Ruby 3.4 and 4.0

This adds support for Ruby 3.4 and 4.0, and fixes the problems that came up while getting the test suite to pass on them. Each commit is self-contained, so it's easiest to review one commit at a time.

Changes

  • chore: Stop tracking .ruby-version. It pinned Ruby 3.0.2, which nothing uses (CI sets versions explicitly), and it makes rbenv fail unless 3.0.2 is installed. It's added to .gitignore again, as it was in d051772.
  • test: Use the expect syntax to check that the config isn't saved. This removes the rspec-mocks deprecation warning about the old :should syntax (File.stub).
  • test: Compare expected events with plain RSpec matchers. The suite records itself: RSpec recording is on by default, and the repo's appmap.yml enables the builtin Kernel#system hook. The first time Diffy runs in a process it calls system("diff", …). Recording that call runs #to_s on the half-built Diffy::Diff, which runs the diff again and deletes its tempfiles. As a result the first Diffy comparison in every process came back empty and passed, so an example run on its own could never fail. The specs now use RSpec's eq, which already prints a readable diff, and the diffy dependency is gone.
  • fix: Record class methods that share a name with an instance method. When a class finished loading, its singleton methods were filtered against its instance methods, so e.g. def self.call was never hooked if an instance call also existed. This affects every Ruby version. It's visible in gem recordings too: shallow recordings of the parser gem now start at its entry point Parser::Base.parse instead of the internal default_parser call, and test/gem_test.rb is updated to match.
  • test: Make the keyword arguments spec pass with Ruby 3.4's Hash#inspect. Ruby 3.4 prints hashes as {kw3: :three}, which also has to be quoted in YAML, so the spec spells out the expected values for both formats.
  • test: Expect the embedded struct singleton method spec to fail on Prism. The spec is marked pending, not skipped, so it starts "failing" (and can be re-enabled) once Ruby fixes the bug below.
  • feat: Support Ruby 3.4. This adds 3.4 to SUPPORTED_RUBY_VERSIONS, so appmap-agent-validate and appmap-agent-status no longer report it as unsupported. It also adds 3.4 to the CI matrix, and adds a "Known issues" section to the README.
  • fix(swagger): The swagger task no longer fails with RDoc 8. RDoc 8 removed the options argument of RDoc::Markup::ToHtml.new, so rake appmap:swagger raised an ArgumentError whenever the bundle resolved RDoc 8 (e.g. Rails 8 apps, where railties pulls it in via irb). This affects every Ruby version. The Markdown conversion had no specs, so this adds one; since Gemfile.lock isn't tracked, CI resolves the latest RDoc and will catch future API changes.
  • fix(swagger): The swagger task no longer fails on Ruby 4.0 without rdoc. Ruby 4.0 turned rdoc from a default gem into a bundled gem, and it was only a development dependency, so the swagger task failed with cannot load such file -- rdoc in bundles that don't otherwise include it (such as Rails 7.x apps). It's now a runtime dependency.
  • test: Add gems the fixture apps need on Ruby 4.0. Ruby 4.0 also turned benchmark and ostruct into bundled gems. Rails 6.1 needs benchmark, and rswag-ui and cucumber 3 need ostruct, so the fixture apps failed to boot until they were added to their Gemfiles.
  • test: Replace Bundler.with_clean_env, which Bundler 4 removed. Bundler 4 ships with Ruby 4.0, and every minitest test that runs a fixture app failed with Bundler::RemovedError. Bundler.with_unbundled_env is its replacement since Bundler 2.1 (Ruby 3.0 ships with 2.2).
  • test: Run bundle install explicitly in the fixture apps. Bundler 4 warns that a bare bundle will show help instead of installing in a future version.
  • test: Drop the Windows-only tzinfo-data gem from the Rails 7 fixture. Bundler 4 deprecates the :mingw/:mswin/:x64_mingw platforms in favour of :windows, which older Bundler versions don't know. The fixture never runs on Windows or JRuby, so the gem is simply removed.
  • feat: Support Ruby 4.0. This adds 4.0 to SUPPORTED_RUBY_VERSIONS and the CI matrix. Apart from the bundled gems and Bundler 4 changes above, no changes were needed.

Known issue: Prism and class << self

Prism, the default parser since Ruby 3.4, doesn't fire the TracePoint :class/:end events for class << self bodies (Ruby bug #22629). AppMap relies on those events, so on Prism it misses methods defined in a class << self (or class << obj) body, unless it is a class << self that runs inside the definition of that same class or module. Examples are class << self inside a Struct.new do … end block, and class << SomeModule at the top level of a file. Methods in class << self directly in a class body are still recorded when the class definition ends; before the fix above, that wasn't true if an instance method had the same name.

I didn't add a workaround in AppMap. It would need a global hook such as BasicObject#singleton_method_added, and the affected patterns are uncommon in application code. A scan of ~580 installed gems found them in about 3% of the class << bodies that define methods, mostly in library code. Instead, the README documents the limitation and the workaround of running with RUBYOPT=--parser=parse.y. The bug is still present in Ruby 4.0.7, and the workaround still works there.

Testing

bundle exec rake test passes on Ruby 3.3.12 (227 examples, 0 failures), and on Ruby 3.4.10 and 4.0.7 with the default Prism parser (227 examples, 1 pending); all minitest tests pass on all three. The 3.4.10 and 4.0.7 runs used Bundler 4 (4.0.15 and 4.0.20), the 3.3.12 run Bundler 2.6.9. bundle exec rspec also passes with RUBYOPT=--parser=parse.y on 3.4.10, which exercises the pre-Prism code path; on 4.0.7 I only ran spec/hook_spec.rb that way (40 examples, 0 failures, including the embedded struct spec). The new swagger spec passes with both RDoc 7.2 and 8.1. Ruby 3.0–3.2 were not tested locally; CI covers them.

Assisted-by: Claude:claude-opus-5-5

@dividedmind
dividedmind requested review from kgilpin and a balanced review from Copilot November 23, 2026 11:56
@dividedmind dividedmind self-assigned this Nov 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The hard-coded YAML quoting in spec/hook_spec.rb causes the expectation to fail on Ruby 3.0–3.3 despite representing the same scalar value.

1 open issue
What changed in this PR

This PR adds Ruby 3.4 support to AppMap, fixes recording of class methods that share names with instance methods, and updates tests for parser and formatting differences.

Changes:

  • Adds Ruby 3.4 to supported versions and the CI matrix.
  • Fixes same-name method recording and adds regression coverage.
  • Replaces Diffy comparisons with RSpec equality checks and documents Prism’s recording limitation.
File Description
test/​test_helper.rb Removes the Diffy require.
test/​gem_test.rb Expects recording to start at Parser::Base.parse.
spec/​spec_helper.rb Adds Prism compiler detection.
spec/​record_net_http_spec.rb Replaces Diffy comparisons with equality checks.
spec/​hook_spec.rb Updates assertions, adds regression coverage, and marks the Prism case pending.
spec/​fixtures/​hook/​same_name_methods.rb Adds matching class and instance method names for regression coverage.
README.md Documents the Prism limitation and parser workaround.
lib/​appmap/​version.rb Declares Ruby 3.4 supported.
lib/​appmap/​hook.rb Stops filtering class methods by instance method names.
appmap.gemspec Removes the Diffy development dependency.
.github/​workflows/​ci.yml Adds Ruby 3.4 to CI.

Review effort: Balanced

Comment thread spec/hook_spec.rb Resolved
It pinned Ruby 3.0.2, which nothing uses: CI sets the Ruby version
explicitly for each job, and the gem supports a range of versions.
With rbenv the pin makes every command in the repository fail unless
3.0.2 happens to be installed. It was untracked for the same reason
once before (d051772), so also ignore it again, letting developers pin
a version locally.
`File.stub` is rspec-mocks' old :should syntax, which isn't enabled
explicitly and prints a deprecation warning on every run. The stub made
File.write raise, so that the example would fail if the config file
was written. `expect(File).not_to receive(:write)` says the same thing
directly, matching the example above it.

Assisted-by: Claude:claude-opus-5-5
The test suite records itself: AppMap's RSpec integration is enabled
by default, and the repository's appmap.yml (with no packages) still
turns on the builtin hooks, including Kernel#system.

Diffy locates its diff binary on first use by calling
system("diff", ...). That call is recorded, and recording it calls #to_s
on the receiver, which is the Diffy::Diff being computed. The nested
#to_s runs the diff a second time and then deletes the tempfiles, so the
outer diff runs against missing files and returns an empty string. As a
result the first Diffy comparison in every process passes no matter
what. When a single example is run on its own, it can never fail.

RSpec's eq matcher already prints a unified diff for multi-line strings,
which reads better than the diff-of-a-diff that comparing Diffy's
output to "" produced, so use it and drop the diffy dependency.
Turning off self-recording with APPMAP=false isn't an option: the
variable is inherited by the fixture apps that some specs run in
subprocesses, and those specs depend on recording.

Assisted-by: Claude:claude-opus-5-5
When a class finished loading, its singleton methods were filtered
against the list of its instance methods, so a class method such as
`def self.call` was never hooked if the class also defined an instance
method `call`. They are different methods, and nothing else hooks the
class method, so it was silently left out of recordings.

This used to be partly hidden: methods defined in a `class << self`
block were hooked when that block ended, before the instance methods
existed. Ruby 3.4's default Prism compiler no longer fires TracePoint
:class/:end events for `class << self`, so on 3.4 such methods are
also affected, which broke the protected singleton method spec.

This is visible in gem recordings too. For example, `Parser::Base.parse`
(the documented entry point of the parser gem, which also has an
instance method `parse`) is now recorded. Previously a shallow recording
of the gem started at the `default_parser` call it makes internally.

Assisted-by: Claude:claude-opus-5-5
Ruby 3.4 changed Hash#inspect to print `{kw3: :three}` rather than
`{:kw3=>:three}`. The new format also contains ": ", so YAML has to
quote it, and the expected event YAML differs in more than the hash.
Spell out the expected values for both formats, instead of deriving
them with Hash#to_s or Psych, so that the spec still checks the actual
output.

Assisted-by: Claude:claude-opus-5-5
Prism, the default parser since Ruby 3.4, doesn't fire the TracePoint
:class and :end events for `class << self` bodies. AppMap hooks the
methods defined in such a body when its :end event fires. That event
is now missing, so methods defined in `class << self` outside a class
or module body, such as in a `Struct.new do ... end` block, are never
hooked. (Inside a class body they are still hooked when the class
itself ends.)

This is a Ruby bug, reported as https://bugs.ruby-lang.org/issues/22629.
Mark the spec pending when running on Prism, rather than skipping it,
so that it starts failing (and can be re-enabled) once Ruby is fixed.

Assisted-by: Claude:claude-opus-5-5
Add 3.4 to the supported versions, so that the config validator (used
by `appmap-agent-validate` and `appmap-agent-status`) no longer reports
an error on it, and run CI on 3.4 too.

One known limitation remains, caused by a bug in Prism, the default
parser since Ruby 3.4 (https://bugs.ruby-lang.org/issues/22629): methods
defined in a `class << self` (or `class << obj`) body are not recorded,
unless the body runs inside the definition of that same class or module.
This is documented in the README, together with the workaround of
running with `--parser=parse.y`. I didn't add a workaround in AppMap
itself. It would need a global hook such as
BasicObject#singleton_method_added, and the affected patterns (such as
`class << self` in a `Struct.new` block, or `class << SomeModule` at the
top level of a file) are uncommon in application code.

Assisted-by: Claude:claude-opus-5-5
RDoc 8 removed the options argument of RDoc::Markup::ToHtml.new, so
`rake appmap:swagger` failed with an ArgumentError whenever the bundle
resolved RDoc 8 (for example in Rails 8 apps, where railties pulls in
RDoc through irb). Pass the options only to older RDoc versions.

The Markdown conversion had no specs at all, so add one. Gemfile.lock
isn't tracked, so CI resolves the latest RDoc and the spec will catch
future API changes too.

Assisted-by: Claude:claude-opus-5-5
`rake appmap:swagger` converts descriptions with RDoc, but rdoc was only
a development dependency, relying on it being available as a default
gem. Since Ruby 4.0 it's a bundled gem instead, so in a bundle that
doesn't otherwise include it (such as a Rails 7.x app) the task failed
with "cannot load such file -- rdoc". Make it a runtime dependency.

Assisted-by: Claude:claude-opus-5-5
Ruby 4.0 moved benchmark and ostruct from default gems to bundled gems,
so they're no longer loadable unless they're in the bundle. Rails 6.1
requires benchmark, and rswag-ui (in the Rails 7 app) and cucumber 3
require ostruct, so the fixture apps failed to boot. Add them to the
fixture Gemfiles; they install fine on older Rubies too.

Assisted-by: Claude:claude-opus-5-5
@dividedmind dividedmind changed the title feat: Support Ruby 3.4 feat: Support Ruby 3.4 and 4.0 Nov 23, 2026
@dividedmind
dividedmind requested a balanced review from Copilot November 23, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the Ruby compatibility, recording, and Swagger updates.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Bundler 4, which ships with Ruby 4.0, removed Bundler.with_clean_env, so
every minitest test that runs a fixture app failed with
Bundler::RemovedError. Use Bundler.with_unbundled_env, its replacement
since Bundler 2.1 (Ruby 3.0 ships with 2.2).

Assisted-by: Claude:claude-opus-5-5
Bundler 4 warns that a future version will show help instead of
installing when `bundle` is run without a command. Spell out
`bundle install` to silence the warning and keep working after that.

Assisted-by: Claude:claude-opus-5-5
Bundler 4 warns that the :mingw, :mswin and :x64_mingw platforms are
deprecated in favour of :windows. That platform isn't known to the older
Bundler versions used with older Rubies, and the fixture app never runs
on Windows or JRuby, so just remove the gem.

Assisted-by: Claude:claude-opus-5-5
Add 4.0 to the supported versions, so that the config validator (used
by `appmap-agent-validate` and `appmap-agent-status`) no longer reports
an error on it, and run CI on 4.0 too.

Apart from bundled gems (fixed in the previous commits), no changes
were needed. The Prism limitation documented in the README for Ruby
3.4+ still applies, as the bug is not fixed in 4.0
(https://bugs.ruby-lang.org/issues/22629); the `--parser=parse.y`
workaround still works.

Assisted-by: Claude:claude-opus-5-5

This branch has not been deployed

No deployments
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