ruby / strscan

Provides lexical scanning operations on a String.
BSD 2-Clause "Simplified" License
81 stars 32 forks source link

warning: already initialized constant StringScanner::Version #102

Open dabroz opened 5 months ago

dabroz commented 5 months ago

When using this gem with ruby 3.3.1 I'm getting

.../.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/strscan-3.1.0/lib/strscan.bundle: warning: already initialized constant StringScanner::Version
kou commented 5 months ago

Could you provide a way to reproduce this?

kinsomicrote commented 5 months ago

I am also seeing the same thing on my Rails project:

.../.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/strscan-3.1.0/lib/strscan.bundle: warning: already initialized constant StringScanner::Version
.../.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/strscan-3.1.0/lib/strscan.bundle: warning: already initialized constant StringScanner::Id

I get this when I run my test suite - rspec. In my Rails project, strscan is used by rexml which is used by rubocop.

dabroz commented 5 months ago

I've created a minified version of the project here: https://github.com/dabroz/strscantest To see this behaviour, clone the repo, then run bundle; rspec test.rb There is also a Dockerfile you can test wit h./dockme

But the strangest thing is that there have to be very specific circumstances to observe this problem:

kou commented 5 months ago

Thanks for preparing a minified project.

Why do you want to use rspec not bundle exec rspec? In general, you should use bundle exec ... when you use Gemfile to manage your dependency.

dabroz commented 5 months ago

Overall sure, but this happens even on a clean rbenv install, where only gems are the ones installed from the Gemfile.

kou commented 5 months ago

Could you show gem list output on the reproduced environment?

dabroz commented 5 months ago

Bingo!

strscan (3.1.0, default: 3.0.7)

After pinning it:

gem 'strscan', '3.0.7'

it doesn't show the warning anymore. I had to pin rubocop to an earlier version (1.63.0) as the latest requires the latest strscan:

    rubocop was resolved to 1.64.0, which depends on
      rexml (>= 3.2.5, < 4.0) was resolved to 3.2.8, which depends on
        strscan (>= 3.0.9)

So can we do anything if a newer version of a default gem is needed?

kou commented 5 months ago

The next REXML version that includes https://github.com/ruby/rexml/issues/132 will change the situation. It relaxes required strscan version. (But bundle exec rspec is still preferred.)

Soeren-Klatt commented 3 months ago

Got the same message when running only rspec without bundle exec. The message appeared after going from ruby 3.1.6 to ruby 3.2.0. Im using strscan (3.1.0)

kou commented 3 months ago

If gem list | grep strscan has multiple versions, you must use bundle exec to choose only one strscan.