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 'sinatra' and 'sinatra/contrib' causes method redefined warnings on Test #1319

Open
faragorn opened this issue Jul 3, 2017 · 8 comments

Comments

@faragorn
Copy link

faragorn commented Jul 3, 2017

I created a repo with very basic setup that replicates the issue: https://github.com/faragorn/sinatra-method-redefined
If you comment out require 'sinatra/contrib' in app.rb the warnings will go away.

Environment:

Ruby Version: 2.2.2
Sinatra Version: 2.0.0
Testing Framework: Minitest (haven't tested this with any other frameworks, but pretty sure the outcome is gonna be the same)

Run

rake test

Result

/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old config_file
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of config_file was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old head
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of head was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old delete
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of delete was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old get
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of get was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old options
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of options was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old patch
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of patch was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old post
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of post was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old put
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of put was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old mime_type
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of mime_type was here
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: method redefined; discarding old respond_to
/Users/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:1921: warning: previous definition of respond_to was here
@faragorn faragorn changed the title Requiring 'sinatra' and 'sinatra/contrib' causes method redefined on Test Requiring 'sinatra' and 'sinatra/contrib' causes method redefined warnings on Test Jul 3, 2017
@zzak
Copy link
Member

zzak commented Jul 4, 2017

This is probably due to our Delegator doing weird things:
https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1915-L1939

@sammyhenningsson
Copy link

I'm extremely new to sinatra so I might be wrong, but I think problem is within the ConfigFile extension.
When the extension is registered (which will setup the delegation for the config_file instance method) then it sets up the delegation again by itself. Please see: https://github.com/sinatra/sinatra/blob/master/sinatra-contrib/lib/sinatra/config_file.rb#L172
Just deleting that line did the trick for me :)

@namusyaka namusyaka added this to the v2.0.2 milestone Feb 6, 2018
@namusyaka namusyaka modified the milestones: v2.0.2, v2.0.3, v2.0.4 Jun 5, 2018
@namusyaka namusyaka modified the milestones: v2.0.4, v2.0.5 Sep 14, 2018
@namusyaka namusyaka modified the milestones: v2.0.5, v2.0.6 Dec 22, 2018
@darkn3rd
Copy link

I am running into this problem with Sinatra 2.0.5:

source "https://rubygems.org"

gem 'sinatra'
gem 'sinatra-contrib'

group :test do
  gem 'rack-test'
  gem 'ci_reporter_test_unit'
end

And getting all these errors with:

/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old get
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of get was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old put
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of put was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old post
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of post was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old patch
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of patch was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old delete
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of delete was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old options
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of options was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old head
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of head was here
Loaded suite /Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/rake_test_loader
Started
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old config_file
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of config_file was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old mime_type
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of mime_type was here
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: method redefined; discarding old respond_to
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/sinatra-2.0.5/lib/sinatra/base.rb:1923: warning: previous definition of respond_to was here

@aonemd
Copy link

aonemd commented Jul 14, 2019

@darkn3rd can confirm, currently running sinatra 2.0.5.

@danbee
Copy link

danbee commented Nov 17, 2019

@darkn3rd @aonemd I'm seeing the same thing with sinatra and sinatra-contrib. Did either of you figure out a solution?

@danbee
Copy link

danbee commented Nov 17, 2019

I solved this by only requireing the parts of contrib that I need:

require "sinatra"
require "sinatra/namespace"
require "sinatra/json"

@jkowens jkowens removed this from the v2.0.6 milestone Jul 25, 2022
@jkowens
Copy link
Member

jkowens commented Jul 27, 2022

It seems like this issue is when requiring any/all of these: ConfigFile, MultiRoute, RespondWith. It's definitely a Delegator issue as @zzak mentioned, but I'm not sure how to resolve the warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants