diff options
author | Koichi ITO <koic.ito@gmail.com> | 2024-05-13 16:56:01 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-05-13 16:29:27 +0000 |
commit | 5931f857abf704c6fc62a93d2fc933b13950392d (patch) | |
tree | 2e8fa79f1fb5b26fe8458e5c8206ba9f7e92effa /lib/prism/translation | |
parent | e6340258f88064cabba5150cfef1f8898f6aa9d8 (diff) |
[ruby/prism] Add error handling for missing `parser` gem in `Prism::Translation`
Resolves https://github.com/ruby/prism/pull/2803.
This PR adds error handling for missing `parser` gem in `Prism::Translation`.
The `parser` gem is a required runtime dependency when using `Prism::Translation::Parser`.
But it is not required for other uses of Prism. To avoid unnecessary dependencies,
it is not added as a `runtime_dependency` in the prism.gemspec. Instead, if the dependency is missing,
instructions are given to add it to Gemfile.
## Before
```console
$ bundle exec ruby -e 'require "prism"; require "prism/translation/parser33"'
/Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require': cannot load such file -- parser (LoadError)
from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:3:in `<top (required)>'
from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser33.rb:6:in `<module:Translation>'
from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser33.rb:4:in `<module:Prism>'
from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser33.rb:3:in `<top (required)>'
from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
from /Users/koic/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
from -e:1:in `<main>'
```
## After
```console
$ bundle exec ruby -e 'require "prism"; require "prism/translation/parser33"'
Error: Unable to load parser. Add `gem "parser"` to your Gemfile.
```
https://github.com/ruby/prism/commit/4880aec22d
Diffstat (limited to 'lib/prism/translation')
-rw-r--r-- | lib/prism/translation/parser.rb | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/prism/translation/parser.rb b/lib/prism/translation/parser.rb index 193bbae406..8052e38570 100644 --- a/lib/prism/translation/parser.rb +++ b/lib/prism/translation/parser.rb @@ -1,6 +1,11 @@ # frozen_string_literal: true -require "parser" +begin + require "parser" +rescue LoadError => e + warn 'Error: Unable to load parser. Add `gem "parser"` to your Gemfile.' + exit! +end module Prism module Translation |