bump-cask-pr: fix macOS host handling

`bump-cask-pr` was recently updated to add Linux support but the
change to the `replace_version_and_checksum` logic has broken the
command for casks that have on_system blocks that reference specific
macOS versions (e.g., `on_monterey :or_newer` in `logi-options+`).

The previous logic only simulated the arch, so the `current_os` value
on macOS was a specific version like `:sequoia`. The current logic
uses generic `:macos` values, which work for `on_macos` blocks but
don't work for blocks like `on_sequoia`, etc. This leads to an
`undefined method 'latest?' for nil` error, as `old_cask.version` is
`nil` in this scenario (i.e., none of the on_system blocks apply to
`:macos`, so `version` is never set).

This updates the method to only specify the OS in `system_options` if
the value doesn't align with the host (e.g., `:linux` on macOS),
which restores the previous behavior.

This also replaces `:macos` values with the newest macOS version
(e.g., `:sequoia`), so that `bump-cask-pr` on Linux can update casks
with on_system blocks that reference specific macOS versions. A
generic `:macos` value doesn't work with those on_system blocks, so
`version` is never called on Linux in that scenario but it works as
expected if we use the newest macOS value instead. This may not be
perfect but it brings `bump-cask-pr` a little closer to parity with
macOS on Linux.

Lastly, this skips `system_options` values where `old_cask` has no
version, as this means the cask doesn't apply to that OS/arch. We've
been seeing a related error on homebrew/cask autobump CI and this
guard should help to avoid it.
This commit is contained in:
Sam Ford 2025-04-03 00:23:27 -04:00
parent b250ca8055
commit 38410f5432
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D

View File

@ -190,15 +190,33 @@ module Homebrew
).returns(T::Array[[T.any(Regexp, String), T.any(Pathname, String)]])
}
def replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
# When blocks are absent, arch is not relevant. For consistency, we simulate the arm architecture.
host_os = Homebrew::SimulateSystem.current_os
host_is_macos = MacOSVersion::SYMBOLS.include?(host_os)
newest_macos = MacOSVersion::SYMBOLS.keys.first
# NOTE: We substitute the newest macOS (e.g. `:sequoia`) in place of
# `:macos` values (when used), as a generic `:macos` value won't apply
# to on_system blocks referencing macOS versions. We also omit the OS
# when the value aligns with the host.
system_options = if cask.on_system_blocks_exist?
OnSystem::BASE_OS_OPTIONS.product(OnSystem::ARCH_OPTIONS)
OnSystem::BASE_OS_OPTIONS.each_with_object([]) do |os, array|
OnSystem::ARCH_OPTIONS.each do |arch|
system_hash = { arch: }
system_hash[:os] = os if host_is_macos && os != :macos
system_hash[:os] = newest_macos if !host_is_macos && os == :macos
array << system_hash
end
end.uniq
else
[[:macos, :arm]]
# Architecture is only relevant if on_system blocks are present. When
# not present, we default to ARM for consistency.
system_hash = { arch: :arm }
system_hash[:os] = newest_macos unless host_is_macos
[system_hash]
end
system_options.each do |os, arch|
SimulateSystem.with(os:, arch:) do
system_options.each do |system_args|
SimulateSystem.with(**system_args) do
# Handle the cask being invalid for specific os/arch combinations
old_cask = begin
Cask::CaskLoader.load(cask.sourcefile_path)
@ -207,8 +225,10 @@ module Homebrew
end
next if old_cask.nil?
old_version = old_cask.version
bump_version = new_version.send(arch) || new_version.general
old_version = old_cask.version
next unless old_version
bump_version = new_version.send(system_args[:arch]) || new_version.general
old_version_regex = old_version.latest? ? ":latest" : %Q(["']#{Regexp.escape(old_version.to_s)}["'])
replacement_pairs << [/version\s+#{old_version_regex}/m,