I spent a few hours fighting this because I was not paying attention to the warnings but I learned a couple of things along the way, so I decided to put this in a short post so that I remember it for next time!
For some file operations, I needed to use FileUtils which provides a lot of handy methods to deal with file and directory operations. So, I went to the FileUtils page on Ruby-doc and found what I needed. The module is called FileUtils and I added require 'FileUtils'
to my code file.
When I ran the script, I got an error that basically dumped a list of constants and complained:
C:/Ruby26-x64/lib/ruby/2.6.0/fileutils.rb:1267: warning: already initialized constant FileUtils::Entry_::S_IF_DOOR
C:/Ruby26-x64/lib/ruby/2.6.0/FileUtils.rb:1267: warning: previous definition of S_IF_DOOR was here
...
I thought that there is a conflict between different versions of FileUtils and a Google search gave me further confirmation which led to me changing the code to use the bundled version by doing Bundler.require 'FileUtils'
and the warnings disappeared. I wasn’t fully satisfied but I let it be for a while… it wasn’t warning any more and that was good.
Then, while running the script from the executable of a gem that I was working on, I received an error relating to calling Bundler.require
and I found that the error goes away when you change it back to just require 'FileUtils'
but the warnings come back. Clearly, this needed to be looked at now!
I decided to follow the instructions to clean up my fileutils gem, and I also did gem update fileutils --default
so that the later version is the default. But that still did not solve it. Then, I did what I should have done ages ago – I read the warning correctly.
C:/Ruby26-x64/lib/ruby/2.6.0/fileutils.rb:1267: warning: already initialized constant FileUtils::Entry_::S_IF_DOOR
C:/Ruby26-x64/lib/ruby/2.6.0/FileUtils.rb:1267: warning: previous definition of S_IF_DOOR was here
...
The previous definition was lib/ruby/2.6.0/FileUtils.rb:1267
and the new definition was at: lib/ruby/2.6.0/fileutils.rb:1267
– it was reading the same file twice and complaining that it had been defined. Ruby was doing a require again because the path it stores is case-sensitive and I had insisted on requiring FileUtils
instead of fileutils
.
So, after my face-palm moment and calling myself stupid for a bit, I fixed it by changing that line to be:
require 'fileutils'
…and that was it!
It feels silly to admit it but I imagine that I am not alone. As always, this is for me to be able to remember how to do it but if it helps someone, that’s great! Also, if you have some comments, please add below so that I can reflect changes here.