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

Init fails for gems with optional dependencies #925

Closed
jakehow opened this issue Jun 20, 2019 · 9 comments
Closed

Init fails for gems with optional dependencies #925

jakehow opened this issue Jun 20, 2019 · 9 comments
Labels
bug Something isn't working

Comments

@jakehow
Copy link

jakehow commented Jun 20, 2019

Input

Many gems include optional functionality using the following pattern:

begin
  require 'braintree'
rescue LoadError
  raise 'Could not load the braintree gem.  Use `gem install braintree` to install it.'
end

Examples:
https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/gateways/braintree_blue.rb#L3

Alternate method of same strategy in ActiveAdmin:
https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/dependency.rb#L78

When running srb init in a project, because every file is required, the init fails due to the errors raised by code like this (even though the project may not require this functionality).

Observed output

For the Braintree example the following error is raised and not rescued by the init process.

 Could not load the braintree gem.  Use `gem install braintree` to install it. (RuntimeError)

Expected behavior

There should be some way to ignore specific files during init if this class of error is not easily handled by the init strategy, and when errors are raised during init, a pointer to the documentation for this scenario would be helpful.

@jakehow jakehow added bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team labels Jun 20, 2019
@willisplummer
Copy link

Until this is fixed, it's probably worth mentioning @jakehow's workaround for anyone else who encounters the issue:

just install the gems that it complains about and run init again. once the init process has been completed, it seems that the gems can be uninstalled without breaking anything.

@ptarjan
Copy link
Contributor

ptarjan commented Jun 20, 2019

You can ignore files during srb init by putting # typed: ignore in those files. The output for srb init does mention this before you type Y, but maybe it got lost in the noise. Can you send us a PR changing that message that would help it become more discoverable?

@hdoan741
Copy link
Contributor

@ptarjan the file is in active-admin gem so they won't be able to # typed: ignore it easily

@jakehow
Copy link
Author

jakehow commented Jun 20, 2019

@ptarjan yes, this is for files that are not in the project, but in dependencies of the project

@alloy
Copy link
Contributor

alloy commented Jun 20, 2019

I added the sigil to https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/cancan_adapter.rb, but it’s still triggered:

$  head -n 1 ./vendor/bundle/ruby/2.5.0/gems/activeadmin-1.3.1/lib/active_admin/cancan_adapter.rb
# typed: ignore
$ head -n 1 ./vendor/bundle/ruby/2.5.0/gems/activeadmin-1.3.1/lib/active_admin/dependency.rb
# typed: ignore

$ bundle exec srb init
Generating: sorbet/config
Generating: sorbet/rbi/sorbet-typed/
Generating: sorbet/rbi/gems/
[242/8162] require_relative './db/schema.rb'-- enable_extension("pgcrypto")
[2/2] require_relative './db/schema.rb'-- enable_extension("pgcrypto")
Generating: sorbet/rbi/hidden-definitions/
Requiring all of your code
Naming all Modules
Naming ActionCable::ServerFailed to load ActionCable::Server::ActiveRecordConnectionManagement
Naming ActionControllerFailed to load ActionController::Middleware
Naming ActionDispatch::HttpFailed to load ActionDispatch::Http::Upload
Naming ActiveAdminTraceback (most recent call last):
[…]
         8: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/sorbet-0.4.4259/lib/real_stdlib.rb:77:in `const_get'
         7: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
         6: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
         5: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
         4: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
         3: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activeadmin-1.3.1/lib/active_admin/cancan_adapter.rb:4:in `<top (required)>'
         2: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activeadmin-1.3.1/lib/active_admin/dependency.rb:50:in `method_missing'
         1: from /Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activeadmin-1.3.1/lib/active_admin/dependency.rb:85:in `match!'
/Users/eloy/Code/Artsy/exchange/vendor/bundle/ruby/2.5.0/gems/activeadmin-1.3.1/lib/active_admin/dependency.rb:76:in `spec!': To use cancan you need to specify it in your Gemfile. (ActiveAdmin::DependencyError)

Using the workaround of (temporarily) installing the gems works indeed.

@ptarjan ptarjan removed the unconfirmed This issue has not yet been confirmed by the Sorbet team label Jun 20, 2019
@joho
Copy link
Contributor

joho commented Jun 20, 2019

Same issue with https://github.com/jch/html-pipeline

Naming HTML::PipelineTraceback (most recent call last):
/usr/server/vendor/bundle/gems/sorbet-0.4.4263/bin/srb-rbi: Bootsnap::LoadPathCache::FallbackScan
	42: from /usr/server/vendor/bundle/gems/sorbet-0.4.4263/bin/srb-rbi:232:in `<main>'
SNIP
	 2: from /usr/server/vendor/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:89:in `register'
	 1: from /usr/server/vendor/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/usr/server/vendor/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require': cannot load such file -- redcloth (LoadError)
	33: from /usr/server/vendor/bundle/gems/sorbet-0.4.4263/bin/srb-rbi:232:in `<main>'
SNIP
	 1: from /usr/server/vendor/bundle/gems/html-pipeline-2.11.0/lib/html/pipeline.rb:47:in `require_dependency'
/usr/server/vendor/bundle/gems/html-pipeline-2.11.0/lib/html/pipeline.rb:50:in `rescue in require_dependency': Missing dependency 'redcloth' for RedCloth. See README.md for details. (HTML::Pipeline::MissingDependencyError)
LoadError: cannot load such file -- redcloth

@risinglf
Copy link

Same issue with https://github.com/kjvarga/sitemap_generator

Naming SitemapGeneratorTraceback (most recent call last):
	27: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/bin/srb-rbi:232:in `<main>'
	26: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/bin/srb-rbi:101:in `main'
	25: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/bin/srb-rbi:227:in `block in make_step'
	24: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/hidden-definition-finder.rb:38:in `main'
	23: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/hidden-definition-finder.rb:44:in `main'
	22: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/hidden-definition-finder.rb:75:in `all_modules_and_aliases'
	21: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/hidden-definition-finder.rb:68:in `constant_cache'
	20: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/gem-generator-tracepoint/tracer.rb:40:in `new'
	19: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/gem-generator-tracepoint/tracer.rb:40:in `new'
	18: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/constant_cache.rb:58:in `initialize'
	17: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/constant_cache.rb:178:in `dfs_module'
	16: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/constant_cache.rb:178:in `each'
	15: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/constant_cache.rb:179:in `block in dfs_module'
	14: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/constant_cache.rb:113:in `dfs_module'
	13: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/constant_cache.rb:113:in `each'
	12: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/constant_cache.rb:125:in `block in dfs_module'
	11: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/real_stdlib.rb:77:in `real_const_get'
	10: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/real_stdlib.rb:77:in `call'
	 9: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sorbet-0.4.4296/lib/real_stdlib.rb:77:in `const_get'
	 8: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `require'
	 7: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:258:in `load_dependency'
	 6: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `block in require'
	 5: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
	 4: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
	 3: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
	 2: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
	 1: from /Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/Users/luca/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/sitemap_generator-6.0.2/lib/sitemap_generator/adapters/wave_adapter.rb:2:in `<main>': Error: `CarrierWave::Uploader::Base` is not defined. (RuntimeError)

Please `require 'carrierwave'` - or another library that defines this class.

@connorshea
Copy link
Contributor

😬 This is now a problem for everyone on Ruby 3.0 because of sorted_set: https://github.com/ruby/ruby/blob/be9b5553a3ae3a90c73d4c11786cb1a813e915f6/lib/set/sorted_set.rb

@jez
Copy link
Collaborator

jez commented Jul 27, 2022

Going to close this in favor of Tapioca

https://sorbet.org/blog/2022/07/27/srb-tapioca

@jez jez closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants