Skip to content
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

Build Rails console on top of IRB's latest official APIs #51705

Merged
merged 1 commit into from May 1, 2024

Conversation

st0012
Copy link
Contributor

@st0012 st0012 commented May 1, 2024

Motivation / Background

Due to the lack of extension APIs from IRB, Rails console currently relies on patching its private components to provide features like backtrace filtering, additional commands & helpers...etc. This has a few problems:

  • Refactoring changes on the IRB side could potentially break Rails console
  • Because commands and helpers are added as simple Ruby methods, they don't show up in the help message, which makes the features significantly less discoverable.

Detail

  1. Commands and helper methods are now added to IRB through its extension APIs.
    • For tests to work properly after this change, I converted all command/helper method tests into integration tests.
  2. Backtrace filtering logic is now applied through IRB's new IRB.conf[:BACKTRACE_FILTER] config instead of by patching its private WorkSpace class.
  3. To use these new APIs & configs, railties now need to require IRB v1.13.0.
    • v1.13.0 should still work with older versions of Rails as is
  4. To accommodate now a bit lengthy IRB-specific logic, I move the IRBConsole class to a new dedicated file.

Result

Now Rails' console helpers/commands will be displayed in IRB's help message.

Screenshot 2024-05-02 at 11 49 04

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the railties label May 1, 2024
@st0012 st0012 force-pushed the update-irb branch 3 times, most recently from 35a1f74 to af860c5 Compare May 1, 2024 18:10
This will greatly increase the visibility of Rails console commands and helpers,
and stop rely on IRB's internal components.

Extension API reference: https://github.com/ruby/irb/blob/master/EXTEND_IRB.md

And because we need to create new classes to use the new APIs, I also
moved all the IRB-specific code to a new file, `irb_console.rb`.

Use IRB.conf[:BACKTRACE_FILTER] for backtrace filtering in console

This change uses the new `IRB.conf[:BACKTRACE_FILTER]` to inject the backtrace
filtering logic into IRB. This avoids the need to patch IRB's internal
WorkSpace class.

Update changelog
@rafaelfranca rafaelfranca merged commit 6b67657 into rails:main May 1, 2024
3 of 4 checks passed
@rafaelfranca rafaelfranca deleted the update-irb branch May 1, 2024 22:38
@chaadow
Copy link
Contributor

chaadow commented May 3, 2024

Cool!

st0012 added a commit to st0012/mission_control-jobs that referenced this pull request May 5, 2024
After rails/rails#51705, the current way to add
commands to the console will no longer work as `Rails::ConsoleMethods`
will not be directly included in the IRB internal anymore.

So this commit uses the new IRB extension API to add commands instead,
which would also bring a few benefits:

- Commands can be displayed in IRB's help message to make them easier to
  discover.
- The parameter of `connect_to` doesn't need to be a string anymore as
  registered commands aren't treated as Ruby methods.
st0012 added a commit to st0012/mission_control-jobs that referenced this pull request May 7, 2024
After rails/rails#51705, the current way to add
commands to the console will no longer work as `Rails::ConsoleMethods`
will not be directly included in the IRB internal anymore.

So this commit uses the new IRB extension API to add commands instead,
which would also bring a few benefits:

- Commands can be displayed in IRB's help message to make them easier to
  discover.
- The parameter of `connect_to` doesn't need to be a string anymore as
  registered commands aren't treated as Ruby methods.
melopilosyan added a commit to melopilosyan/pry-rails that referenced this pull request May 10, 2024
After upgrading [Rails console using IRB's latest official extension APIs](rails/rails#51705)
pry-rails is failing to open the console.

This change takes Rails' console helper methods defined in
IRB::HelperMethod.helper_methods and adds to the main object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants