Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

On load, PgHero::Stats assumes ActiveRecord::Base has a connection, crash if not. Context of Tapioca/Sorbet #424

Closed
dhnaranjo opened this issue Sep 9, 2022 · 1 comment

Comments

@dhnaranjo
Copy link

Yowhattup.

I'm trying out Sorbet and Tapioca and I'm running into a wall when PgHero is included. Tapioca requires all your gems and loads all their classes. What appears to happen is that when PgHero is require'd PgHero::Stats tries to make a database connection immediately and fails.

What happens is that connection_config on lib/pghero.rb#215 goes looking for info about the connection established by ActiveRecord::Base but there ain't one and we get an exception. In digging around I found a code-change-less stupid hack, which is to ensure that PGHERO_DATABASE_URL env var is not blank. I added that to my Tapioca binstub and we're in business.

I don't really know what kind of change I think would make sense here. It feels like there's a lot going on just on this class being loaded, you know? I dunno.

Minimal recreation, steps as follows:

  1. rails new --minimal
  2. Add the following to the Gemfile
gem "pghero"
gem "sorbet-runtime"
gem "tapioca", require: false, group: :development
  1. Run the following command and get the following output
dazmin@dazmin pghero_tapioca % bundle exec tapioca init
      create  sorbet/config
      create  sorbet/tapioca/config.yml
      create  sorbet/tapioca/require.rb
      create  bin/tapioca
Retrieving index from central repository... Done
Listing gems from Gemfile.lock... Done
Removing annotations for gems that have been removed...  Nothing to do
Fetching gem annotations from central repository... 

  Fetched actionmailer
      create  sorbet/rbi/annotations/actionmailer.rbi

  Fetched actionpack
      create  sorbet/rbi/annotations/actionpack.rbi

  Fetched actionview
      create  sorbet/rbi/annotations/actionview.rbi

  Fetched activerecord
      create  sorbet/rbi/annotations/activerecord.rbi

  Fetched activesupport
      create  sorbet/rbi/annotations/activesupport.rbi

  Fetched rails
      create  sorbet/rbi/annotations/rails.rbi

  Fetched railties
      create  sorbet/rbi/annotations/railties.rbi

Done
Removing RBI files of gems that have been removed:

  Nothing to do.

Generating RBI files of gems that are added or updated:

Requiring all gems to prepare for compiling... You don't have dalli installed in your application. Please add it to your Gemfile and run bundle install
bundler: failed to load command: tapioca (/Users/dazmin/.gem/ruby/3.1.2/bin/tapioca)
/Users/dazmin/.gem/ruby/3.1.2/gems/activerecord-7.0.3.1/lib/active_record/connection_handling.rb:309:in `connection_pool': ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
	from /Users/dazmin/.gem/ruby/3.1.2/gems/activerecord-7.0.3.1/lib/active_record/connection_handling.rb:305:in `connection_db_config'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/pghero-2.8.3/lib/pghero.rb:216:in `connection_config'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/pghero-2.8.3/lib/pghero.rb:122:in `config'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/pghero-2.8.3/lib/pghero.rb:90:in `stats_database_url'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/pghero-2.8.3/lib/pghero/stats.rb:4:in `<class:Stats>'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/pghero-2.8.3/lib/pghero/stats.rb:2:in `<module:PgHero>'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/pghero-2.8.3/lib/pghero/stats.rb:1:in `<top (required)>'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/zeitwerk-2.6.0/lib/zeitwerk/kernel.rb:35:in `require'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/zeitwerk-2.6.0/lib/zeitwerk/kernel.rb:35:in `require'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/runtime/reflection.rb:33:in `const_get'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/runtime/reflection.rb:33:in `constantize'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/runtime/trackers/autoload.rb:24:in `block in eager_load_all!'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/runtime/trackers/autoload.rb:47:in `with_disabled_exits'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/runtime/trackers/autoload.rb:19:in `eager_load_all!'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/loaders/gem.rb:63:in `require_gem_file'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/loaders/gem.rb:31:in `load'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/loaders/gem.rb:25:in `load_application'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/commands/gem.rb:241:in `block in perform_additions'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor/shell/basic.rb:44:in `indent'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/commands/gem.rb:237:in `perform_additions'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/commands/gem.rb:115:in `sync'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/cli.rb:255:in `block in gem'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca.rb:33:in `block in silence_warnings'
	from /Users/dazmin/.rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/user_interaction.rb:46:in `use_ui'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca.rb:32:in `silence_warnings'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `bind_call'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/sorbet-runtime-0.5.10413/lib/types/private/methods/_methods.rb:272:in `block in _on_method_added'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/cli.rb:224:in `gem'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor/invocation.rb:116:in `invoke'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/lib/tapioca/cli.rb:28:in `init'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/thor-1.2.1/lib/thor/base.rb:485:in `start'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/tapioca-0.10.0/exe/tapioca:23:in `<top (required)>'
	from /Users/dazmin/.gem/ruby/3.1.2/bin/tapioca:25:in `load'
	from /Users/dazmin/.gem/ruby/3.1.2/bin/tapioca:25:in `<top (required)>'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/cli/exec.rb:58:in `load'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/cli/exec.rb:58:in `kernel_load'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/cli/exec.rb:23:in `run'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/cli.rb:483:in `exec'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/cli.rb:31:in `dispatch'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/cli.rb:25:in `start'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/exe/bundle:48:in `block in <top (required)>'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/lib/bundler/friendly_errors.rb:120:in `with_friendly_errors'
	from /Users/dazmin/.gem/ruby/3.1.2/gems/bundler-2.3.19/exe/bundle:36:in `<top (required)>'
	from /Users/dazmin/.gem/ruby/3.1.2/bin/bundle:25:in `load'
	from /Users/dazmin/.gem/ruby/3.1.2/bin/bundle:25:in `<main>'
@ankane ankane closed this as completed in 8ad27c7 Sep 10, 2022
@ankane
Copy link
Owner

ankane commented Sep 10, 2022

Hey @dhnaranjo, thanks for sharing. Pushed an update to the config pattern that address it.

andyatkinson pushed a commit to onboardiq/pghero that referenced this issue Jan 5, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants