Avoid frozen array errors in brew upgrade

The `Homebrew::CLI::NamedArgs` class often returns frozen arrays so this one cannot
be easily modified in-place here. We just overwrite it instead.

I also updated the `brew reinstall` command logic to match for consistency.

```console
$ brew upgrade gh --verbose --debug
...
/usr/local/Homebrew/Library/Homebrew/brew.rb (Formulary::FromAPILoader): loading gh
/usr/local/Homebrew/Library/Homebrew/brew.rb (Formulary::FromAPILoader): loading gh
Error: can't modify frozen Array: [#<Formula gh (stable) /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/g/gh.rb>]
/usr/local/Homebrew/Library/Homebrew/cmd/upgrade.rb:139:in `delete'
/usr/local/Homebrew/Library/Homebrew/cmd/upgrade.rb:139:in `run'
/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/3.3.0/gems/sorbet-runtime-0.5.11481/lib/types/private/methods/call_validation.rb:270:in `bind_call'
/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/3.3.0/gems/sorbet-runtime-0.5.11481/lib/types/private/methods/call_validation.rb:270:in `validate_call'
/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/3.3.0/gems/sorbet-runtime-0.5.11481/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'
/usr/local/Homebrew/Library/Homebrew/brew.rb:95:in `<main>'
```
This commit is contained in:
apainintheneck 2024-07-17 23:11:37 -07:00
parent 4aae003a1a
commit 75cfa21068
2 changed files with 5 additions and 3 deletions

View File

@ -109,7 +109,7 @@ module Homebrew
sig { override.void } sig { override.void }
def run def run
formulae, casks = T.cast( formulae, casks = T.cast(
args.named.to_formulae_and_casks(method: :resolve).partition { _1.is_a?(Formula) }, args.named.to_resolved_formulae_to_casks,
[T::Array[Formula], T::Array[Cask::Cask]], [T::Array[Formula], T::Array[Cask::Cask]],
) )
@ -126,7 +126,8 @@ module Homebrew
if Homebrew::Attestation.enabled? if Homebrew::Attestation.enabled?
if formulae.include?(Formula["gh"]) if formulae.include?(Formula["gh"])
formulae.unshift(T.must(formulae.delete(Formula["gh"]))) # Move `gh` to the front of the list so that it gets installed first.
formulae = [Formula["gh"]] | formulae
else else
Homebrew::Attestation.gh_executable Homebrew::Attestation.gh_executable
end end

View File

@ -136,7 +136,8 @@ module Homebrew
if Homebrew::Attestation.enabled? if Homebrew::Attestation.enabled?
if formulae.include?(Formula["gh"]) if formulae.include?(Formula["gh"])
formulae.unshift(formulae.delete(Formula["gh"])) # Move `gh` to the front of the list so that it gets installed first.
formulae = [Formula["gh"]] | formulae
else else
Homebrew::Attestation.gh_executable Homebrew::Attestation.gh_executable
end end