Skip to content

Commit

Permalink
Merge pull request #10695 from Bo98/gem_path
Browse files Browse the repository at this point in the history
Prevent loading all non-Bundler gems
  • Loading branch information
Bo98 committed Feb 26, 2021
2 parents 8a3561e + 65c0357 commit a65c5d6
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 74 deletions.
1 change: 1 addition & 0 deletions Library/Homebrew/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source "https://rubygems.org"
gem "bootsnap", require: false
gem "byebug", require: false
gem "codecov", require: false
gem "minitest", require: false
gem "nokogiri", require: false
gem "parallel_tests", require: false
gem "ronn", require: false
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ DEPENDENCIES
codecov
concurrent-ruby
mechanize
minitest
nokogiri
parallel_tests
patchelf
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,11 @@ then
# Don't allow non-developers to customise Ruby warnings.
unset HOMEBREW_RUBY_WARNINGS

# Disable Ruby options we don't need. RubyGems provides a decent speedup.
RUBY_DISABLE_OPTIONS="--disable=gems,did_you_mean,rubyopt"
# Disable Ruby options we don't need.
RUBY_DISABLE_OPTIONS="--disable=did_you_mean,rubyopt"
else
# Don't disable did_you_mean for developers as it's useful.
RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt"
RUBY_DISABLE_OPTIONS="--disable=rubyopt"
fi

if [[ -z "$HOMEBREW_RUBY_WARNINGS" ]]
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

old_trap = trap("INT") { exit! 130 }

require "global"
require_relative "global"
require "build_options"
require "cxxstdlib"
require "keg"
Expand Down
1 change: 0 additions & 1 deletion Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

require "utils/bottles"

require "utils/gems"
require "formula"
require "cask/cask_loader"
require "set"
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ def self.[](env)
tmp.mkpath unless tmp.exist?
tmp.realpath
end.freeze

# The Ruby path and args to use for forked Ruby calls
HOMEBREW_RUBY_EXEC_ARGS = [
RUBY_PATH,
ENV["HOMEBREW_RUBY_WARNINGS"],
].freeze
9 changes: 4 additions & 5 deletions Library/Homebrew/dev-cmd/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def test_args
def test
args = test_args.parse

Homebrew.install_bundler_gems!

require "formula_assertions"
require "formula_free_port"

Expand Down Expand Up @@ -75,10 +77,7 @@ def test
env = ENV.to_hash

begin
exec_args = %W[
#{RUBY_PATH}
#{ENV["HOMEBREW_RUBY_WARNINGS"]}
-I #{$LOAD_PATH.join(File::PATH_SEPARATOR)}
exec_args = HOMEBREW_RUBY_EXEC_ARGS + %W[
--
#{HOMEBREW_LIBRARY_PATH}/test.rb
#{f.path}
Expand Down Expand Up @@ -106,7 +105,7 @@ def test
rescue Exception => e # rubocop:disable Lint/RescueException
retry if retry_test?(f, args: args)
ofail "#{f.full_name}: failed"
puts e, e.backtrace
$stderr.puts e, e.backtrace
ensure
ENV.replace(env)
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/extend/ENV.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def activate_extensions!(env: nil)
params(
env: T.nilable(String),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
build_bottle: T::Boolean,
bottle_arch: T.nilable(String),
_block: T.proc.returns(T.untyped),
).returns(T.untyped)
Expand Down
11 changes: 6 additions & 5 deletions Library/Homebrew/extend/ENV/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ module SharedEnvExtension

sig {
params(
formula: T.nilable(Formula),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
bottle_arch: T.nilable(T::Boolean),
formula: T.nilable(Formula),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
bottle_arch: T.nilable(String),
testing_formula: T::Boolean,
).void
}
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil)
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false)
@formula = formula
@cc = cc
@build_bottle = build_bottle
Expand Down
11 changes: 6 additions & 5 deletions Library/Homebrew/extend/ENV/std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ module Stdenv
# @private
sig {
params(
formula: T.nilable(Formula),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
bottle_arch: T.nilable(T::Boolean),
formula: T.nilable(Formula),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
bottle_arch: T.nilable(String),
testing_formula: T::Boolean,
).void
}
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil)
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false)
super

self["HOMEBREW_ENV"] = "std"
Expand Down
11 changes: 6 additions & 5 deletions Library/Homebrew/extend/ENV/super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ def reset
# @private
sig {
params(
formula: T.nilable(Formula),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
bottle_arch: T.nilable(T::Boolean),
formula: T.nilable(Formula),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
bottle_arch: T.nilable(String),
testing_formula: T::Boolean,
).void
}
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil)
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false)
super
send(compiler)

Expand Down
5 changes: 4 additions & 1 deletion Library/Homebrew/extend/os/linux/extend/ENV/std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

module Stdenv
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false)
generic_setup_build_environment(formula: formula, cc: cc, build_bottle: build_bottle, bottle_arch: bottle_arch)
generic_setup_build_environment(
formula: formula, cc: cc, build_bottle: build_bottle,
bottle_arch: bottle_arch, testing_formula: testing_formula
)

prepend_path "CPATH", HOMEBREW_PREFIX/"include"
prepend_path "LIBRARY_PATH", HOMEBREW_PREFIX/"lib"
Expand Down
5 changes: 4 additions & 1 deletion Library/Homebrew/extend/os/linux/extend/ENV/super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def self.bin

# @private
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false)
generic_setup_build_environment(formula: formula, cc: cc, build_bottle: build_bottle, bottle_arch: bottle_arch)
generic_setup_build_environment(
formula: formula, cc: cc, build_bottle: build_bottle,
bottle_arch: bottle_arch, testing_formula: testing_formula
)
self["HOMEBREW_OPTIMIZATION_LEVEL"] = "O2"
self["HOMEBREW_DYNAMIC_LINKER"] = determine_dynamic_linker_path
self["HOMEBREW_RPATH_PATHS"] = determine_rpath_paths(@formula)
Expand Down
5 changes: 4 additions & 1 deletion Library/Homebrew/extend/os/mac/extend/ENV/std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def homebrew_extra_pkg_config_paths
end

def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false)
generic_setup_build_environment(formula: formula, cc: cc, build_bottle: build_bottle, bottle_arch: bottle_arch)
generic_setup_build_environment(
formula: formula, cc: cc, build_bottle: build_bottle,
bottle_arch: bottle_arch, testing_formula: testing_formula
)

# sed is strict, and errors out when it encounters files with
# mixed character sets
Expand Down
5 changes: 4 additions & 1 deletion Library/Homebrew/extend/os/mac/extend/ENV/super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_a
self["HOMEBREW_SDKROOT"] = nil
self["HOMEBREW_DEVELOPER_DIR"] = nil
end
generic_setup_build_environment(formula: formula, cc: cc, build_bottle: build_bottle, bottle_arch: bottle_arch)
generic_setup_build_environment(
formula: formula, cc: cc, build_bottle: build_bottle,
bottle_arch: bottle_arch, testing_formula: testing_formula
)

# Filter out symbols known not to be defined since GNU Autotools can't
# reliably figure this out with Xcode 8 and above.
Expand Down
38 changes: 34 additions & 4 deletions Library/Homebrew/formula_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,38 @@ module Homebrew
module Assertions
include Context

require "test/unit/assertions"
include ::Test::Unit::Assertions
require "minitest"
require "minitest/assertions"
include ::Minitest::Assertions

attr_writer :assertions

def assertions
@assertions ||= 0
end

# Test::Unit backwards compatibility methods
{
assert_raise: :assert_raises,
assert_not_empty: :refute_empty,
assert_not_equal: :refute_equal,
assert_not_in_delta: :refute_in_delta,
assert_not_in_epsilon: :refute_in_epsilon,
assert_not_includes: :refute_includes,
assert_not_instance_of: :refute_instance_of,
assert_not_kind_of: :refute_kind_of,
assert_no_match: :refute_match,
assert_not_nil: :refute_nil,
assert_not_operator: :refute_operator,
assert_not_predicate: :refute_predicate,
assert_not_respond_to: :refute_respond_to,
assert_not_same: :refute_same,
}.each do |old_method, new_method|
define_method(old_method) do |*args|
# odeprecated old_method, new_method
send(new_method, *args)
end
end

# Returns the output of running cmd, and asserts the exit status.
# @api public
Expand All @@ -18,7 +48,7 @@ def shell_output(cmd, result = 0)
output = `#{cmd}`
assert_equal result, $CHILD_STATUS.exitstatus
output
rescue Test::Unit::AssertionFailedError
rescue Minitest::Assertion
puts output if verbose?
raise
end
Expand All @@ -35,7 +65,7 @@ def pipe_output(cmd, input = nil, result = nil)
end
assert_equal result, $CHILD_STATUS.exitstatus unless result.nil?
output
rescue Test::Unit::AssertionFailedError
rescue Minitest::Assertion
puts output if verbose?
raise
end
Expand Down
6 changes: 2 additions & 4 deletions Library/Homebrew/formula_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ def initialize(info)
# Returns nil if formula is absent or if there was an error reading it.
def self.lookup(name)
json = Utils.popen_read(
RUBY_PATH,
ENV["HOMEBREW_RUBY_WARNINGS"],
"-I", $LOAD_PATH.join(File::PATH_SEPARATOR),
*HOMEBREW_RUBY_EXEC_ARGS,
HOMEBREW_LIBRARY_PATH/"brew.rb",
"info",
"--json=v1",
name
name,
)

return unless $CHILD_STATUS.success?
Expand Down
13 changes: 6 additions & 7 deletions Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,12 @@ def build
# 1. formulae can modify ENV, so we must ensure that each
# installation has a pristine ENV when it starts, forking now is
# the easiest way to do this
args = %W[
nice #{RUBY_PATH}
#{ENV["HOMEBREW_RUBY_WARNINGS"]}
-I #{$LOAD_PATH.join(File::PATH_SEPARATOR)}
--
#{HOMEBREW_LIBRARY_PATH}/build.rb
#{formula.specified_path}
args = [
"nice",
*HOMEBREW_RUBY_EXEC_ARGS,
"--",
HOMEBREW_LIBRARY_PATH/"build.rb",
formula.specified_path,
].concat(build_argv)

Utils.safe_fork do
Expand Down
1 change: 0 additions & 1 deletion Library/Homebrew/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
RUBY_PATH = Pathname.new(RbConfig.ruby).freeze
RUBY_BIN = RUBY_PATH.dirname.freeze

require "rubygems"
# Only require "core_ext" here to ensure we're only requiring the minimum of
# what we need.
require "active_support/core_ext/object/blank"
Expand Down
3 changes: 0 additions & 3 deletions Library/Homebrew/homebrew_bootsnap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
end

if homebrew_bootsnap_enabled
require "rubygems"

begin
require "bootsnap"
rescue LoadError
unless ENV["HOMEBREW_BOOTSNAP_RETRY"]
require "utils/gems"
Homebrew.install_bundler_gems!(only_warn_on_failure: true)

ENV["HOMEBREW_BOOTSNAP_RETRY"] = "1"
Expand Down
21 changes: 15 additions & 6 deletions Library/Homebrew/load_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@

HOMEBREW_LIBRARY_PATH = Pathname(__dir__).realpath.freeze

$LOAD_PATH.push HOMEBREW_LIBRARY_PATH.to_s
require_relative "utils/gems"
Homebrew.setup_gem_environment!(setup_path: false)

require "vendor/bundle/bundler/setup"
require "homebrew_bootsnap"
$LOAD_PATH.push HOMEBREW_LIBRARY_PATH.to_s unless $LOAD_PATH.include?(HOMEBREW_LIBRARY_PATH.to_s)
require_relative "vendor/bundle/bundler/setup"
$LOAD_PATH.uniq!

unless defined?(Bootsnap)
$LOAD_PATH.select! { |d| Pathname(d).directory? }
$LOAD_PATH.uniq!
# Block any gem loading by bypassing rubygem's `require`.
# Helps make sure we don't accidentally use things not in bundler's load path.
# Bundler 2.2.7+ and non-standalone mode both do this automatically.
# https://github.com/rubygems/rubygems/blob/5841761974bef324a33ef1cb650bbf8a2457805b/bundler/lib/bundler/installer/standalone.rb#L55-L63
if Kernel.private_method_defined?(:gem_original_require)
Kernel.send(:remove_method, :require)
Kernel.send(:define_method, :require, Kernel.instance_method(:gem_original_require))
Kernel.send(:private, :require)
end

require_relative "homebrew_bootsnap"
2 changes: 1 addition & 1 deletion Library/Homebrew/postinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

old_trap = trap("INT") { exit! 130 }

require "global"
require_relative "global"
require "debrew"
require "fcntl"
require "socket"
Expand Down
1 change: 0 additions & 1 deletion Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
require "utils/bottles"
require "patch"
require "compilers"
require "global"
require "os/mac/version"
require "extend/on_os"

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

old_trap = trap("INT") { exit! 130 }

require "global"
require_relative "global"
require "extend/ENV"
require "timeout"
require "debrew"
Expand Down
3 changes: 1 addition & 2 deletions Library/Homebrew/test/dev-cmd/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
describe "brew test" do
it_behaves_like "parseable arguments"

# randomly segfaults on Linux with portable-ruby.
it "tests a given Formula", :integration_test, :needs_macos do
it "tests a given Formula", :integration_test do
install_test_formula "testball", <<~'RUBY'
test do
assert_equal "test", shell_output("#{bin}/test")
Expand Down
1 change: 0 additions & 1 deletion Library/Homebrew/test/formula_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# frozen_string_literal: true

require "formula_info"
require "global"

describe FormulaInfo, :integration_test do
it "tests the FormulaInfo class" do
Expand Down

0 comments on commit a65c5d6

Please sign in to comment.