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

Better handle LoadErrors from prism #349

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions lib/rubocop/ast/processed_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,14 @@ def parser_class(ruby_version, parser_engine) # rubocop:disable Metrics/AbcSize,
raise ArgumentError, "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
end
when :parser_prism
begin
require 'prism'
rescue LoadError
warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
exit!
end
require_prism

case ruby_version
when 3.3
require 'prism/translation/parser33'
require_prism_translation_parser(ruby_version)
Prism::Translation::Parser33
when 3.4
require 'prism/translation/parser34'
require_prism_translation_parser(ruby_version)
Prism::Translation::Parser34
else
raise ArgumentError, 'RuboCop supports target Ruby versions 3.3 and above with Prism. ' \
Expand All @@ -306,6 +301,31 @@ def parser_class(ruby_version, parser_engine) # rubocop:disable Metrics/AbcSize,
end
end

# Prism is a native extension, a `LoadError` will be raised if linked to an incompatible
# Ruby version. Only raise if it really was caused by Prism not being present.
def require_prism
require 'prism'
rescue LoadError => e
raise unless e.path == 'prism'

warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
exit!
end

# While Prism is not yet a dependency, users may run with outdated versions that
# don't have all the parsers.
def require_prism_translation_parser(version)
require "prism/translation/parser#{version.to_s.delete('.')}"
rescue LoadError
warn <<~MESSAGE
Error: Unable to load Prism Parser for Ruby #{version}.
marcandre marked this conversation as resolved.
Show resolved Hide resolved
* If you're using Bundler and don't yet have `gem 'prism'` as a dependency, add it now.
* If you're using Bundler and already have `gem 'prism'` as a dependency, update it to the most recent version.
* If you don't use Bundler, run `gem update prism`.
MESSAGE
exit!
end

def create_parser(ruby_version, parser_engine)
builder = RuboCop::AST::Builder.new

Expand Down
Loading