Skip to content

Change Builder from a class to a module and only build app once - #1094

Closed
speckins wants to merge 1 commit into
omniauth:masterfrom
speckins:issue-1083-builder-as-module
Closed

speckins wants to merge 1 commit into
omniauth:masterfrom
speckins:issue-1083-builder-as-module

Conversation

@speckins

Copy link
Copy Markdown

Previously, OmniAuth::Builder was a subclass of Rack::Builder. Rack::Builder#call calls #to_app, rebuilding the Rack application on each request, and, as it says in the Rack::Builder comments, this may have a negative impact on performance.

This commit changes OmniAuth::Builder from a class to a module and defines a ::new method that extends a Rack::Builder instance with that module, instance_evals any block, and returns the value of #to_app, the built application.

Using OmniAuth::Builder will now only build the application once.

For users who have been subclassing OmniAuth::Builder, a fix would be subclassing Rack::Builder directly and including OmniAuth::Builder:

class MyBuilder < Rack::Builder
  include OmniAuth::Builder
  # ...
end

Resolves #1083.

Previously, OmniAuth::Builder was a subclass of Rack::Builder.
Rack::Builder#call calls #to_app, rebuilding the Rack application on
each request, and, as it says in the Rack::Builder comments, this may
have a negative impact on performance.

This commit changes OmniAuth::Builder from a class to a module and
defines a ::new method that extends a Rack::Builder instance with that
module, instance_evals any block, and returns the value of #to_app, the
built application.

Using OmniAuth::Builder will now only build the application once.

For users who have been subclassing OmniAuth::Builder, a fix would be
subclassing Rack::Builder directly and including OmniAuth::Builder:

    class MyBuilder < Rack::Builder
      include OmniAuth::Builder
      # ...
    end

Resolves omniauth#1083.
Comment thread lib/omniauth/builder.rb
Comment on lines +5 to +9
def self.new app, &block
builder = Rack::Builder.new(app).extend(self)
builder.instance_eval(&block) if block_given?
builder.to_app
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pass an app in here? Can we default it to nil like Rack::Builder does in internal code?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, no, but if no @run (in Rack::Builder internals) is defined by the time #to_app is called, an error will be raised, and the only way to add one would be to call #run in the block.

OmniAuth::Builder.new(nil) do
  provider :developer
  run proc {}
end

If you needed that, maybe we could extract the first two lines to a separate module method (e.g., ::build) that didn't require an argument.

def self.build(app = nil, &block)
  builder = Rack::Builder.new(app).extend(self)
  builder.instance_eval(&block) if block_given?
  builder
end

def self.new(app, &block)
  build(app, &block).to_app
end

Comment thread lib/omniauth/builder.rb
module OmniAuth
class Builder < ::Rack::Builder
module Builder
def self.new app, &block

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def self.new app, &block
def self.new(app, &block)

@sferik

sferik commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Adding the Breaking Change label: this turns OmniAuth::Builder from a class into a module, and OmniAuth::Builder.new now returns the built Rack app instead of a builder. Apps that subclass OmniAuth::Builder (as the PR description notes) or call builder methods on the instance would need changes, so under SemVer this has to ship in a major version.

@sferik

sferik commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Thanks for tracking this down! You were right about the cause. I fixed it on master in c29ad9b without the breaking change: OmniAuth::Builder#call now builds the stack on the first request and reuses it, so Builder stays a Rack::Builder subclass and existing subclasses keep working. Closing in favor of that.

@sferik sferik closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking Change Must be released in the next major version release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rack::Builder#to_app is not designed to be called for every request.

3 participants