From ef2e297d3b084c2ae28088f5749fe2365839f2e5 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 18 Apr 2019 17:43:33 +0900 Subject: [PATCH 1/3] Disable RubyGems by default. This speeds up all Ruby invocations where we don't need RubyGems by around 10%. Where we do need RubyGems: include it manually. --- Library/Homebrew/brew.sh | 8 +++++++- Library/Homebrew/formula_assertions.rb | 1 + Library/Homebrew/shims/super/cc | 4 +--- .../helper/spec/shared_context/integration_test.rb | 1 + Library/Homebrew/utils/gems.rb | 3 +++ Library/Homebrew/utils/ruby.sh | 2 +- 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 3679d4af40..427602055c 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -327,6 +327,12 @@ 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" +else + # Don't disable did_you_mean for developers as it's useful. + RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt" fi if [[ -z "$HOMEBREW_RUBY_WARNINGS" ]] @@ -480,5 +486,5 @@ else # Unshift command back into argument list (unless argument list was empty). [[ "$HOMEBREW_ARG_COUNT" -gt 0 ]] && set -- "$HOMEBREW_COMMAND" "$@" - { update-preinstall "$@"; exec "$HOMEBREW_RUBY_PATH" $HOMEBREW_RUBY_WARNINGS "$HOMEBREW_LIBRARY/Homebrew/brew.rb" "$@"; } + { update-preinstall "$@"; exec "$HOMEBREW_RUBY_PATH" $HOMEBREW_RUBY_WARNINGS "$RUBY_DISABLE_OPTIONS" "$HOMEBREW_LIBRARY/Homebrew/brew.rb" "$@"; } fi diff --git a/Library/Homebrew/formula_assertions.rb b/Library/Homebrew/formula_assertions.rb index f81087a3dd..2ef69d6d7c 100644 --- a/Library/Homebrew/formula_assertions.rb +++ b/Library/Homebrew/formula_assertions.rb @@ -1,5 +1,6 @@ module Homebrew module Assertions + require "rubygems" require "test/unit/assertions" include ::Test::Unit::Assertions diff --git a/Library/Homebrew/shims/super/cc b/Library/Homebrew/shims/super/cc index 2c90e5e939..9957789aba 100755 --- a/Library/Homebrew/shims/super/cc +++ b/Library/Homebrew/shims/super/cc @@ -1,13 +1,11 @@ #!/bin/sh # Make sure this shim uses the same Ruby interpreter that is used by Homebrew. -unset RUBYLIB -unset RUBYOPT if [ -z "$HOMEBREW_RUBY_PATH" ] then echo "${0##*/}: The build tool has reset ENV; --env=std required." >&2 exit 1 fi -exec "$HOMEBREW_RUBY_PATH" -x "$0" "$@" +exec "$HOMEBREW_RUBY_PATH" --disable=gems,did_you_mean,rubyopt -x "$0" "$@" #!/usr/bin/env ruby -W0 require "pathname" diff --git a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb index 902204e1d8..f79df38dd4 100644 --- a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb +++ b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb @@ -89,6 +89,7 @@ RSpec.shared_context "integration test" do "-I", $LOAD_PATH.join(File::PATH_SEPARATOR) ] if ENV["HOMEBREW_TESTS_COVERAGE"] + require "rubygems" simplecov_spec = Gem.loaded_specs["simplecov"] specs = [simplecov_spec] simplecov_spec.runtime_dependencies.each do |dep| diff --git a/Library/Homebrew/utils/gems.rb b/Library/Homebrew/utils/gems.rb index 5b16b0bee2..04beb2c8cb 100644 --- a/Library/Homebrew/utils/gems.rb +++ b/Library/Homebrew/utils/gems.rb @@ -12,6 +12,7 @@ module Homebrew end def gem_user_bindir + require "rubygems" "#{Gem.user_dir}/bin".freeze end @@ -39,6 +40,7 @@ module Homebrew ENV["GEM_PATH"] = ENV["GEM_HOME"] # Make RubyGems notice environment changes. + require "rubygems" Gem.clear_paths Gem::Specification.reset @@ -76,6 +78,7 @@ module Homebrew end def install_bundler! + require "rubygems" setup_gem_environment!(gem_home: Gem.user_dir, gem_bindir: gem_user_bindir) install_gem_setup_path!("bundler", version: ">=2", executable: "bundle", setup_gem_environment: false) end diff --git a/Library/Homebrew/utils/ruby.sh b/Library/Homebrew/utils/ruby.sh index 91b30efe15..6aff2185a8 100644 --- a/Library/Homebrew/utils/ruby.sh +++ b/Library/Homebrew/utils/ruby.sh @@ -37,7 +37,7 @@ setup-ruby-path() { if [[ -n "$HOMEBREW_RUBY_PATH" && -z "$HOMEBREW_FORCE_VENDOR_RUBY" ]] then - ruby_version_new_enough="$("$HOMEBREW_RUBY_PATH" -rrubygems -e "puts Gem::Version.new(RUBY_VERSION.to_s.dup) >= Gem::Version.new('$minimum_ruby_version')")" + ruby_version_new_enough="$("$HOMEBREW_RUBY_PATH" --disable=gems -rrubygems -e "puts Gem::Version.new(RUBY_VERSION.to_s.dup) >= Gem::Version.new('$minimum_ruby_version')")" fi if [[ -z "$HOMEBREW_RUBY_PATH" || -n "$HOMEBREW_FORCE_VENDOR_RUBY" || "$ruby_version_new_enough" != "true" ]] From 9e7b98d3e2ce61f3ab43fb95b4ac95e58ed24039 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 18 Apr 2019 17:54:05 +0900 Subject: [PATCH 2/3] Don't check system Ruby is new enough when unnecessary. We know this is the case in macOS >=10.13.3 so save a Ruby process call. --- Library/Homebrew/brew.sh | 13 +++++++++++++ Library/Homebrew/utils/ruby.sh | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 427602055c..ba6592eba5 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -127,6 +127,17 @@ then HOMEBREW_CACHE="${HOMEBREW_CACHE:-${HOME}/Library/Caches/Homebrew}" HOMEBREW_LOGS="${HOMEBREW_LOGS:-${HOME}/Library/Logs/Homebrew}" HOMEBREW_SYSTEM_TEMP="/private/tmp" + + # Set a variable when the macOS system Ruby is new enough to avoid spawning + # a Ruby process unnecessarily. + if [[ "$HOMEBREW_MACOS_VERSION_NUMERIC" -lt "101303" ]] + then + unset HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH + else + # Used in ruby.sh. + # shellcheck disable=SC2034 + HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH="1" + fi else HOMEBREW_PROCESSOR="$(uname -m)" HOMEBREW_PRODUCT="${HOMEBREW_SYSTEM}brew" @@ -157,6 +168,8 @@ else HOMEBREW_CACHE="${HOMEBREW_CACHE:-${CACHE_HOME}/Homebrew}" HOMEBREW_LOGS="${HOMEBREW_LOGS:-${CACHE_HOME}/Homebrew/Logs}" HOMEBREW_SYSTEM_TEMP="/tmp" + + unset HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH fi if [[ -n "$HOMEBREW_MACOS" || -n "$HOMEBREW_FORCE_HOMEBREW_ON_LINUX" ]] diff --git a/Library/Homebrew/utils/ruby.sh b/Library/Homebrew/utils/ruby.sh index 6aff2185a8..b1a2988505 100644 --- a/Library/Homebrew/utils/ruby.sh +++ b/Library/Homebrew/utils/ruby.sh @@ -35,7 +35,10 @@ setup-ruby-path() { HOMEBREW_RUBY_PATH="$(type -P ruby)" fi - if [[ -n "$HOMEBREW_RUBY_PATH" && -z "$HOMEBREW_FORCE_VENDOR_RUBY" ]] + if [[ -n "$HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH" ]] + then + ruby_version_new_enough="true" + elif [[ -n "$HOMEBREW_RUBY_PATH" && -z "$HOMEBREW_FORCE_VENDOR_RUBY" ]] then ruby_version_new_enough="$("$HOMEBREW_RUBY_PATH" --disable=gems -rrubygems -e "puts Gem::Version.new(RUBY_VERSION.to_s.dup) >= Gem::Version.new('$minimum_ruby_version')")" fi From 892d0ac5febb943e103e01c863fc934c5663b8ee Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 18 Apr 2019 21:42:42 +0900 Subject: [PATCH 3/3] =?UTF-8?q?style:=20don=E2=80=99t=20run=20shellcheck?= =?UTF-8?q?=20on=20specified=20ruby=20file.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Homebrew/style.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index f2ce76b0ee..f4d53e5a1b 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -100,7 +100,7 @@ module Homebrew raise "Invalid output_type for check_style_impl: #{output_type}" end - return !rubocop_success if !files.nil? && !has_non_formula + return !rubocop_success if files.present? || !has_non_formula shellcheck = which("shellcheck") shellcheck ||= which("shellcheck", ENV["HOMEBREW_PATH"])