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

Ruby warning: circular require considered harmful #490

Closed
martijnbolhuis opened this issue Feb 23, 2024 · 2 comments · Fixed by #491
Closed

Ruby warning: circular require considered harmful #490

martijnbolhuis opened this issue Feb 23, 2024 · 2 comments · Fixed by #491

Comments

@martijnbolhuis
Copy link
Contributor

Steps to reproduce

rails new test_project
cd test_project
echo "gem 'dotenv'" >> Gemfile
bundle install
sed -i '1i$VERBOSE = true' config/application.rb

rails s

Expected behavior

No Ruby warnings should occur

Actual behavior

The following ruby warning occurs:

/home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74: warning: /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74: warning: loading in progress, circular require considered harmful - /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/dotenv-3.0.2/lib/dotenv.rb
        from bin/rails:4:in  `<main>'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `block (2 levels) in replace_require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/commands.rb:18:in  `<main>'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command.rb:69:in  `invoke'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command.rb:149:in  `with_argv'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command.rb:71:in  `block in invoke'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command/base.rb:73:in  `perform'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.0/lib/thor.rb:527:in  `dispatch'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command/base.rb:178:in  `invoke_command'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in  `invoke_command'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.0/lib/thor/command.rb:28:in  `run'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/commands/server/server_command.rb:136:in  `perform'
        from <internal:kernel>:90:in  `tap'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/commands/server/server_command.rb:139:in  `block in perform'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `block (2 levels) in replace_require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `require'
        from /home/myuser/code/test_project/config/application.rb:8:in  `<main>'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundler.rb:187:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundler/runtime.rb:44:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundler/runtime.rb:44:in  `each'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundler/runtime.rb:55:in  `block in require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundler/runtime.rb:55:in  `each'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundler/runtime.rb:60:in  `block (2 levels) in require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `block (2 levels) in replace_require'
        from <internal:/home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:37:in  `require'
        from <internal:/home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:37:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/dotenv-3.0.2/lib/dotenv.rb:139:in  `<main>'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `block (2 levels) in replace_require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/dotenv-3.0.2/lib/dotenv/rails.rb:7:in  `<main>'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in  `require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `block (2 levels) in replace_require'
        from /home/myuser/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in  `require'

System configuration

dotenv version: 3.0.2

Rails version: 7.1.3.2

Ruby version: 3.3.0

This is caused by the fact that in lib/dotenv.rb:139:

require "dotenv/rails" if defined?(Rails::Railtie)

But in lib/dotenv/rails.rb:7

require "dotenv"

I think the solution is to remove the latter require statement in lib/dotenv/rails.rb. This shouldn't be necessary since it is already required as the entrypoint of the gem. I suspect that it is still there from the time that Rails integration was a separate gem. Removing this gets rid of the warning in my app and does not seem to break the specs in the dotenv gem. I can make a PR if desired.

@bkeepers
Copy link
Owner

Thanks for reporting the issue! A PR would be great!

martijnbolhuis added a commit to martijnbolhuis/dotenv that referenced this issue Feb 26, 2024
@martijnbolhuis
Copy link
Contributor Author

Thanks for reporting the issue! A PR would be great!

I've created a PR: #491

@bkeepers bkeepers linked a pull request Feb 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants