From 4c6cadd58c70ca9d1189081a3e38ed31fa0fc25f Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:50:45 +0800 Subject: [PATCH 1/3] cmd/install: infer `--overwrite` when in GitHub Actions Based on discussion at Homebrew/homebrew-core#195288. I was initially inclined to scope this only to Python, but I think it makes sense to apply more generally. The issue is that users often have no control over what's already inside `HOMEBREW_PREFIX` when they run `brew install`. Given that, I think it makes sense to assume users want `--overwrite` unless configured otherwise with `HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE`. --- Library/Homebrew/cmd/install.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index defbe8f820d5d..b4bb8c70d599f 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -298,6 +298,32 @@ def run Install.perform_preinstall_checks_once Install.check_cc_argv(args.cc) + overwrite = if GitHub::Actions.env_set? && + ENV["HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE"].blank? && + ENV["HOMEBREW_GITHUB_ACTIONS"].blank? + if ENV["HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE_WARNING"].blank? + message = <<~WARNING + In GitHub Actions, `brew install` behaves the same as `brew install --overwrite`. + + To disable this behaviour, set `HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE`. + + To silence this warning, set `HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE_WARNING`. + WARNING + + puts GitHub::Actions::Annotation.new(:warning, message) + + # Print warning only once. + github_env = ENV.fetch("GITHUB_ENV", "") + if File.exist?(github_env) && File.writable?(github_env) + File.open(github_env, "a") { |f| f.puts("HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE_WARNING=1") } + end + end + + true + else + args.overwrite? + end + Install.install_formulae( installed_formulae, build_bottle: args.build_bottle?, @@ -313,7 +339,7 @@ def run keep_tmp: args.keep_tmp?, debug_symbols: args.debug_symbols?, force: args.force?, - overwrite: args.overwrite?, + overwrite:, debug: args.debug?, quiet: args.quiet?, verbose: args.verbose?, From 79959ca0d0b862347cfeade4eb024c50dbd545d2 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:21:29 +0800 Subject: [PATCH 2/3] formula_installer: propagate `--overwrite` flag to dependencies --- Library/Homebrew/formula_installer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index c26e12b424013..5e2eeea3b29e2 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -811,6 +811,7 @@ def install_dependency(dep, inherited_options) debug: debug?, quiet: quiet?, verbose: verbose?, + overwrite: overwrite?, ) oh1 "Installing #{formula.full_name} dependency: #{Formatter.identifier(dep.name)}" fi.install From 04d7c400916cd82f75a2b44bd4cd198ad1816ef3 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:34:27 +0800 Subject: [PATCH 3/3] Also set `--overwrite` for pre-install checks And skip displaying the message if `--overwrite` was already requested. --- Library/Homebrew/cmd/install.rb | 39 +++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index b4bb8c70d599f..5fc4e6927275c 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -281,24 +281,8 @@ def run puts "You're on your own. Failures are expected so don't create any issues, please!" end - installed_formulae = formulae.select do |f| - Install.install_formula?( - f, - head: args.HEAD?, - fetch_head: args.fetch_HEAD?, - only_dependencies: args.only_dependencies?, - force: args.force?, - quiet: args.quiet?, - overwrite: args.overwrite?, - ) - end - - return if formulae.any? && installed_formulae.empty? - - Install.perform_preinstall_checks_once - Install.check_cc_argv(args.cc) - - overwrite = if GitHub::Actions.env_set? && + overwrite = if !args.overwrite? && + GitHub::Actions.env_set? && ENV["HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE"].blank? && ENV["HOMEBREW_GITHUB_ACTIONS"].blank? if ENV["HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE_WARNING"].blank? @@ -307,7 +291,7 @@ def run To disable this behaviour, set `HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE`. - To silence this warning, set `HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE_WARNING`. + To silence this warning, set `HOMEBREW_GITHUB_ACTIONS_NO_INSTALL_OVERWRITE_WARNING` or pass `--overwrite` to `brew install`. WARNING puts GitHub::Actions::Annotation.new(:warning, message) @@ -324,6 +308,23 @@ def run args.overwrite? end + installed_formulae = formulae.select do |f| + Install.install_formula?( + f, + head: args.HEAD?, + fetch_head: args.fetch_HEAD?, + only_dependencies: args.only_dependencies?, + force: args.force?, + quiet: args.quiet?, + overwrite:, + ) + end + + return if formulae.any? && installed_formulae.empty? + + Install.perform_preinstall_checks_once + Install.check_cc_argv(args.cc) + Install.install_formulae( installed_formulae, build_bottle: args.build_bottle?,