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

Capybara 'webrick' server registration not compatible with rack 3 #2640

Closed
jrochkind opened this issue Jan 30, 2023 · 14 comments
Closed

Capybara 'webrick' server registration not compatible with rack 3 #2640

jrochkind opened this issue Jan 30, 2023 · 14 comments

Comments

@jrochkind
Copy link

jrochkind commented Jan 30, 2023

Capybara Version: 3.38.0

The capybara 'webrick' registration tries to require and use: require 'rack/handler/webrick'.

require 'rack/handler/webrick'
options = { Host: host, Port: port, AccessLog: [], Logger: WEBrick::Log.new(nil, 0) }.merge(options)
Rack::Handler::WEBrick.run(app, **options)

The file rack/handler/webrick is no longer at that location in rack in 3.0. It looks like they have been removed to a separate rackup gem. rack/rack#1937

This means if you try to use capybara with rack 3 and Capybara.server = :webrick, you get an error something like:

       cannot load such file -- rack/handler/webrick
       capybara-3.38.0/lib/capybara/registrations/servers.rb:8:in `block in <top (required)>

I discovered this because I run CI on a project against rails "edge" for early warnings, and Rails edge main branch seems to have recently allowed rack 3?

rails/rails@859b526

I'm a bit confused about what Rails is doing here. Current edge of main definitely allows rack 3, as far as what's in the gemspec. And this commit saying "Merge PR 46594" seems to be committed: rails/rails@1b44989

But actual Rails PR 46594 "Allow rack >= 3 in Rails" is still in an open state? rails/rails#46594. (And I think current Rails edge of main actually not only allows but possibly requires rack 3.0.x, or at any rate I'm having trouble getting bundler to create a resolution with rack 2.x... I'm getting confused about what's going on in Rails land)

At any rate, regardless of what's going on in Rails, capybara is not only used with Rails, and rack 3.0 was released in Nov 2022, and capybara gemspec allows any rack >= 1.6.0 -- should Capybara.server = :webrick be fixed to work with rack 3?

@simi
Copy link
Contributor

simi commented Jan 30, 2023

Per my understanding Rails edge is currently locked to rack 3.0+ and (sadly) not fully compatible yet. There is no easy way to use Rack 2.x+ for now.

If you would like to use rack 3 + webrick I think something like this should work (not tested):

# Gemfile
gem 'rackup'
...
# test helper
Capybara.register_server :webrick do |app, port, host, **options|
  require 'rackup/handler/webrick'
  options = { Host: host, Port: port, AccessLog: [], Logger: WEBrick::Log.new(nil, 0) }.merge(options)
  Rackup::Handler::WEBrick.run(app, **options)
end

Capybara.server = :webrick

@jrochkind
Copy link
Author

jrochkind commented Jan 30, 2023

Ah, so rails edge is actually known to not actually be working properly? In the sense that it requires rack 3, but is known to not yet work with it? So much for my plan of testing against Rails edge! I had thought that Rails edge should always be in an internally consistent working state, i guess I misunderstood!

Thanks for that demo code, i will try it out, super helpful! (update: Does appear to work!)

But regardless of what Rails is doing or does -- should Capybara.server = :webrick already work with rack 3, since rack 3 is long-released, and capybara can be used without Rails too? Does capybara need like conditional logic for what webrick should do depending on rack 2 or 3? or a separate :webrick_rack_3 registration?

@dentarg
Copy link

dentarg commented Jan 30, 2023

Indeed Capybara needs to be updated (to either work with both Rack 2 and 3 or just one of them), the changes are in the Rack 3 upgrade guide: https://github.com/rack/rack/blob/7acfe495c907c4b219125309f97f7309e64a563c/UPGRADE-GUIDE.md#binrackup-rackserver-rackhandlerand--racklobster-were-moved-to-a-separate-gem

@skipkayhil
Copy link

In the sense that it requires rack 3, but is known to not yet work with it?

Just wanted to add some clarification here: Rails edge allows either Rack 2.x or 3.x.

3.x was first allowed in rails/rails#47133 so that compatibility could be fixed across multiple PRs since the diff ended up really big in a single PR.

You should be able to prevent a Rack 3.x resolution with something like gem "rack", "< 3" in your Gemfile/gemspec. Rails itself does this to test against both major versions of Rack:

https://github.com/rails/rails/blob/c8190750f9d28b831f9b38b4ded8a3ee5b41fd9d/Gemfile#L72

and capybara gemspec allows any rack >= 1.6.0 -- should Capybara.server = :webrick be fixed to work with rack 3?

probably a good idea to add a < 4 too once 3.x compatibility is fixed

@simi
Copy link
Contributor

simi commented Jan 31, 2023

@skipkayhil ahh, thanks for clarification. I was a little confused since there was a lot of changes related to actionpack gemspec happening at short time.

@jrochkind
Copy link
Author

jrochkind commented Jan 31, 2023

(update: I at first had trouble getting a rack 2 resolution with "rack", "< 3" in an app with current rails edge -- bundler was refusing to resolve it, claiming conflicts. But a rack 2 resolution was possible, bundler was just having trouble with it for it's own reasons on an existing Gemfile.lock, and eventually after messing with it, adding and removing different restrictions from my gemfile, I got bundler to resolve with rack 2)

tricknotes added a commit to tricknotes/ember-cli-rails that referenced this issue Feb 10, 2023
Rails main allow rack 3 in rails/rails#46594.
However, capybara doesn't support rack 3 yet. teamcapybara/capybara#2640

To test only for rails gem, lock rack version in 2.x.
tricknotes added a commit to tricknotes/ember-cli-rails that referenced this issue Feb 10, 2023
Rails main allow rack 3 in rails/rails#46594.
However, capybara doesn't support rack 3 yet. teamcapybara/capybara#2640

To test only for rails gem, lock rack version in 2.x.
seanpdoyle pushed a commit to thoughtbot/ember-cli-rails that referenced this issue Feb 10, 2023
Rails main allow rack 3 in rails/rails#46594.
However, capybara doesn't support rack 3 yet. teamcapybara/capybara#2640

To test only for rails gem, lock rack version in 2.x.
@twalpole
Copy link
Member

Issue here is that Capybaras tests depend on sinatra which doesn't yet have a versions which supports rack 3, so we can't currently add rack 3 to the automated tests.

@twalpole
Copy link
Member

Trial "fix" merged here - 2db340d - please give it a try and let me know - we can reopen if anything else is required

@jrochkind
Copy link
Author

jrochkind commented Feb 12, 2023

Thank you for the workaround commit for :webrick server in capybara!

simi's local workaround at #2640 (comment) also works fine for me, and is reasonable.

The bigger concern is of course the block on capybara running CI for rack 3! Would it be useful to make an open issue here to track that, for people curious to find? And I wonder the most realistic path to resolving that. The sinatra project does not currently seem to have resources engaged for adding support for rack 3. sinatra/sinatra#1797 Swapping sinatra out in capybara CI may or may not be desirable/feasible. (swapping it out for... roda? The roda gemspec does not specify any rack version requirements, not sure if that actually means it is fully supported for rack 3; I haven't found it's CI to see what it builds with; some posts in Github Discussions and Issues seem to suggest some people are using it with rack 3).

@dentarg
Copy link

dentarg commented Feb 12, 2023

@jrochkind Feel free to try out sinatra/sinatra#1857 :-)

@twalpole
Copy link
Member

@dentarg Looks like Rack::File was removed, and it needs to be Rack::Files::Iterator at https://github.com/dentarg/sinatra/blob/rack-3/lib/sinatra/base.rb#L294 now -- not sure where else in the code that may affect

@dentarg
Copy link

dentarg commented Feb 13, 2023

@twalpole Yes, but not until Rack 3.1: rack/rack#1811 (will try to address that too)

@twalpole
Copy link
Member

@dentarg Files is available in Rack 2.x too --- it's just that File was an alias of it --- should be able to just swap to using Files in both 2 and 3

@dentarg
Copy link

dentarg commented Feb 15, 2023

I noticed

tricknotes added a commit to tricknotes/ember-cli-rails that referenced this issue Feb 18, 2024
This landed in thoughtbot#601
due to capybara incompatibility with rack 3.

However, this issue has been solved teamcapybara/capybara#2640.
tricknotes added a commit to tricknotes/ember-cli-rails that referenced this issue Feb 18, 2024
This landed in thoughtbot#601
due to capybara incompatibility with rack 3.

However, this issue has been solved in teamcapybara/capybara#2640.
seanpdoyle pushed a commit to thoughtbot/ember-cli-rails that referenced this issue Feb 18, 2024
This landed in #601
due to capybara incompatibility with rack 3.

However, this issue has been solved in teamcapybara/capybara#2640.
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

5 participants