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

require to require_relative #2964

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

require 'thread'

# use require, see https://github.com/puma/puma/pull/2381
require 'puma/puma_http11'
require 'puma/detect'
require 'puma/json_serialization'

require_relative 'puma/detect'
require_relative 'puma/json_serialization'

module Puma
autoload :Const, 'puma/const'
autoload :Server, 'puma/server'
autoload :Launcher, 'puma/launcher'
autoload :Const, "#{__dir__}/puma/const"
Copy link
Member

Choose a reason for hiding this comment

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

This feels weird. Is this weird? I've never seen this before.

Copy link
Member Author

Choose a reason for hiding this comment

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

This feels weird. Is this weird? I've never seen this before.

If it's an absolute path, it 'bypasses' the RubyGems resolution. So

autoload :Const, "puma/const" resolved by RubyGems, similar to require
autoload :Const, "#{__dir__}/puma/const" resolved as a path, no RubyGems, similar to require_relative

The Ruby docs for Kernel.autoload show an absolute path, Module.autoload show a 'RubyGems' like path.

I think most code paths that are followed with 'normal' Puma use will see require_relative for all the files autoloaded here, so these are kind of helpers for running single test files. autload only does something if the constant isn't defined...

autoload :Server, "#{__dir__}/puma/server"
autoload :Launcher, "#{__dir__}/puma/launcher"

# at present, MiniSSL::Engine is only defined in extension code (puma_http11),
# not in minissl.rb
Expand All @@ -26,7 +28,7 @@ module Puma
HAS_UNIX_SOCKET = Object.const_defined? :UNIXSocket

if HAS_SSL
require 'puma/minissl'
require_relative 'puma/minissl'
else
module MiniSSL
# this class is defined so that it exists when Puma is compiled
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/app/status.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'puma/json_serialization'
require_relative '../json_serialization'

module Puma
module App
Expand Down
10 changes: 5 additions & 5 deletions lib/puma/binder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
require 'uri'
require 'socket'

require 'puma/const'
require 'puma/util'
require 'puma/configuration'
require_relative 'const'
require_relative 'util'
require_relative 'configuration'

module Puma

if HAS_SSL
require 'puma/minissl'
require 'puma/minissl/context_builder'
require_relative 'minissl'
require_relative 'minissl/context_builder'
end

class Binder
Expand Down
10 changes: 5 additions & 5 deletions lib/puma/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require 'optparse'
require 'uri'

require 'puma'
require 'puma/configuration'
require 'puma/launcher'
require 'puma/const'
require 'puma/log_writer'
require_relative '../puma'
require_relative 'configuration'
require_relative 'launcher'
require_relative 'const'
require_relative 'log_writer'

module Puma
class << self
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module WaitReadable
end
end

require 'puma/detect'
require_relative 'detect'
require 'tempfile'
require 'forwardable'

Expand Down
10 changes: 5 additions & 5 deletions lib/puma/cluster.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require 'puma/runner'
require 'puma/util'
require 'puma/plugin'
require 'puma/cluster/worker_handle'
require 'puma/cluster/worker'
require_relative 'runner'
require_relative 'util'
require_relative 'plugin'
require_relative 'cluster/worker_handle'
require_relative 'cluster/worker'

require 'time'

Expand Down
11 changes: 6 additions & 5 deletions lib/puma/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require 'puma/rack/builder'
require 'puma/plugin'
require 'puma/const'
require_relative 'rack/builder'
require_relative 'plugin'
require_relative 'const'
# note that dsl is loaded at end of file, requires ConfigDefault constants

module Puma
# A class used for storing "leveled" configuration options.
Expand Down Expand Up @@ -285,7 +286,7 @@ def app
found = options[:app] || load_rackup

if @options[:log_requests]
require 'puma/commonlogger'
require_relative 'commonlogger'
logger = @options[:logger]
found = CommonLogger.new(found, logger)
end
Expand Down Expand Up @@ -383,4 +384,4 @@ def self.random_token
end
end

require 'puma/dsl'
require_relative 'dsl'
2 changes: 1 addition & 1 deletion lib/puma/control_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def run

private
def start
require 'puma/cli'
require_relative 'cli'

run_args = []

Expand Down
4 changes: 2 additions & 2 deletions lib/puma/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'puma/const'
require 'puma/util'
require_relative 'const'
require_relative 'util'

module Puma
# The methods that are available for use inside the configuration file.
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/error_logger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'puma/const'
require_relative 'const'

module Puma
# The implementation of a detailed error logging.
Expand Down
20 changes: 10 additions & 10 deletions lib/puma/launcher.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require 'puma/log_writer'
require 'puma/events'
require 'puma/detect'
require 'puma/cluster'
require 'puma/single'
require 'puma/const'
require 'puma/binder'
require_relative 'log_writer'
require_relative 'events'
require_relative 'detect'
require_relative 'cluster'
require_relative 'single'
require_relative 'const'
require_relative 'binder'

module Puma
# Puma::Launcher is the single entry point for starting a Puma server based on user
Expand Down Expand Up @@ -113,7 +113,7 @@ def write_state
permission = @options[:state_permission]
return unless path

require 'puma/state_file'
require_relative 'state_file'

sf = StateFile.new
sf.pid = Process.pid
Expand Down Expand Up @@ -276,7 +276,7 @@ def restart!
if Puma.jruby?
close_binder_listeners

require 'puma/jruby_restart'
require_relative 'jruby_restart'
JRubyRestart.chdir_exec(@restart_dir, restart_args)
elsif Puma.windows?
close_binder_listeners
Expand Down Expand Up @@ -317,7 +317,7 @@ def integrate_with_systemd
return unless ENV["NOTIFY_SOCKET"]

begin
require 'puma/systemd'
require_relative 'systemd'
rescue LoadError
log "Systemd integration failed. It looks like you're trying to use systemd notify but don't have sd_notify gem installed"
return
Expand Down
4 changes: 2 additions & 2 deletions lib/puma/log_writer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'puma/null_io'
require 'puma/error_logger'
require_relative 'null_io'
require_relative 'error_logger'
require 'stringio'
require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT

Expand Down
1 change: 1 addition & 0 deletions lib/puma/minissl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
end

# need for Puma::MiniSSL::OPENSSL constants used in `HAS_TLS1_3`
# use require, see https://github.com/puma/puma/pull/2381
require 'puma/puma_http11'

module Puma
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/plugin/tmp_restart.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'puma/plugin'
require_relative '../plugin'

Puma::Plugin.create do
def start(launcher)
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/rack/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def call(env)
private

def generate_map(default_app, mapping)
require 'puma/rack/urlmap'
require_relative 'urlmap'

mapped = default_app ? {'/' => default_app} : {}
mapping.each { |r,b| mapped[r] = self.class.new(default_app, &b).to_app }
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/rack_default.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'rack/handler/puma'
require_relative '../rack/handler/puma'

module Rack::Handler
def self.default(options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/reactor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'puma/queue_close' unless ::Queue.instance_methods.include? :close
require_relative 'queue_close' unless ::Queue.instance_methods.include? :close

module Puma
class UnsupportedBackend < StandardError; end
Expand Down
6 changes: 3 additions & 3 deletions lib/puma/runner.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'puma/server'
require 'puma/const'
require_relative 'server'
require_relative 'const'

module Puma
# Generic class that is used by `Puma::Cluster` and `Puma::Single` to
Expand Down Expand Up @@ -61,7 +61,7 @@ def start_control
str = @options[:control_url]
return unless str

require 'puma/app/status'
require_relative 'app/status'

if token = @options[:control_auth_token]
token = nil if token.empty? || token == 'none'
Expand Down
22 changes: 11 additions & 11 deletions lib/puma/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

require 'stringio'

require 'puma/thread_pool'
require 'puma/const'
require 'puma/log_writer'
require 'puma/events'
require 'puma/null_io'
require 'puma/reactor'
require 'puma/client'
require 'puma/binder'
require 'puma/util'
require 'puma/io_buffer'
require 'puma/request'
require_relative 'thread_pool'
require_relative 'const'
require_relative 'log_writer'
require_relative 'events'
require_relative 'null_io'
require_relative 'reactor'
require_relative 'client'
require_relative 'binder'
require_relative 'util'
require_relative 'io_buffer'
require_relative 'request'

require 'socket'
require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT
Expand Down
6 changes: 3 additions & 3 deletions lib/puma/single.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'puma/runner'
require 'puma/detect'
require 'puma/plugin'
require_relative 'runner'
require_relative 'detect'
require_relative 'plugin'

module Puma
# This class is instantiated by the `Puma::Launcher` and used
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'thread'

require 'puma/io_buffer'
require_relative 'io_buffer'

module Puma
# Internal Docs for A simple thread pool management object.
Expand Down
8 changes: 4 additions & 4 deletions lib/rack/handler/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ module Puma
}

def self.config(app, options = {})
require 'puma'
require 'puma/configuration'
require 'puma/log_writer'
require 'puma/launcher'
require_relative '../../puma'
require_relative '../../puma/configuration'
require_relative '../../puma/log_writer'
require_relative '../../puma/launcher'

default_options = DEFAULT_OPTIONS.dup

Expand Down