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

Requiring fileutils directly from the ruby lib dir can cause already initialized constant warnings #285

Closed
aar0nr opened this issue Dec 12, 2019 · 4 comments

Comments

@aar0nr
Copy link

aar0nr commented Dec 12, 2019

If you're using Spring with Bootsnap, Spring requires fileutils via gem name (i.e. require 'fileutils', which in turn, always loads the latest version installed. Bootsnap requires fileutils directly from the ruby lib dir, which can cause already initialized constant FileUtils:: if you have multiple versions installed.

For example, on my system I have the following versions installed:

fileutils (1.4.1, default: 1.1.0)

Spring will load v1.4.1, and Bootsnap will load v1.1.0.

Any suggestions? Or maybe Bootsnap should check if the FileUtils constant is already loaded before trying to require it? Thanks for your time.

Update 1

This really has nothing to do with Spring, it just so happens it uses fileutils and it's run before Bootsnap.

@rafaelfranca
Copy link
Member

Can you provide an example application that reproduce the issue?

@aar0nr
Copy link
Author

aar0nr commented Dec 13, 2019

@rafaelfranca Sure, here is an example:

https://github.com/aar0nr/bootsnap-issue-285

Just run, bin/test and you should see something like the following:

~/.rubies/ruby-2.6.5/lib/ruby/2.6.0/fileutils/version.rb:4: warning: already initialized constant FileUtils::VERSION
~/.gem/ruby/2.6.5/gems/fileutils-1.4.1/lib/fileutils.rb:105: warning: previous definition of VERSION was here
~/.rubies/ruby-2.6.5/lib/ruby/2.6.0/fileutils.rb:1267: warning: already initialized constant FileUtils::Entry_::S_IF_DOOR
~/.gem/ruby/2.6.5/gems/fileutils-1.4.1/lib/fileutils.rb:1284: warning: previous definition of S_IF_DOOR was here
~/.rubies/ruby-2.6.5/lib/ruby/2.6.0/fileutils.rb:1540: warning: already initialized constant FileUtils::Entry_::DIRECTORY_TERM
~/.gem/ruby/2.6.5/gems/fileutils-1.4.1/lib/fileutils.rb:1568: warning: previous definition of DIRECTORY_TERM was here
~/.rubies/ruby-2.6.5/lib/ruby/2.6.0/fileutils.rb:1595: warning: already initialized constant FileUtils::OPT_TABLE
~/.gem/ruby/2.6.5/gems/fileutils-1.4.1/lib/fileutils.rb:1626: warning: previous definition of OPT_TABLE was here
~/.rubies/ruby-2.6.5/lib/ruby/2.6.0/fileutils.rb:1649: warning: already initialized constant FileUtils::LOW_METHODS
~/.gem/ruby/2.6.5/gems/fileutils-1.4.1/lib/fileutils.rb:1685: warning: previous definition of LOW_METHODS was here
~/.rubies/ruby-2.6.5/lib/ruby/2.6.0/fileutils.rb:1656: warning: already initialized constant FileUtils::METHODS
~/.gem/ruby/2.6.5/gems/fileutils-1.4.1/lib/fileutils.rb:1692: warning: previous definition of METHODS was here

TLDR: If any code executed before bootsnap uses the fileutils gem with a version that's not bundled with ruby, you get these warnings.

@rafaelfranca
Copy link
Member

Bootsnap must be setup before any other file is required. As explained in the README: https://github.com/Shopify/bootsnap#usage.

If you are using Rails, add this to config/boot.rb immediately after require 'bundler/setup'

@aar0nr
Copy link
Author

aar0nr commented Dec 13, 2019

Yes, but Spring requires fileutils and is loaded before Bootsnap, so you get these warnings on a default Rails install assuming you have a newer fileutils version installed. 🤷‍♂

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

No branches or pull requests

2 participants