KEMBAR78
fix: use composite cache key for to_keys_to_casks method by dynamicy · Pull Request #20771 · Homebrew/brew · GitHub
Skip to content

Conversation

dynamicy
Copy link
Contributor

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?
image image image

Fixed cache collisions that could happen when mixing formula and cask operations. Same functionality, just more reliable caching under the hood.

Issue 1: Incomplete Cache Key
The cache was only using the method parameter as the key, ignoring other parameters that affect the result.
Example of the bug:

# First call - caches result with only 'method' as key
method = :some_method
only = :formulae
ignore_unavailable = true
result1 = @to_keys_to_casks[method] ||= # ... computes and caches result

# Second call - WRONG! Returns cached result1 even though parameters differ
method = :some_method        # same
only = :casks               # different!
ignore_unavailable = false  # different!
result2 = @to_keys_to_casks[method]  # incorrectly returns result1

Issue 2: Unsafe Type Assumptions
The partition method splits arrays based on type checking, but makes unsafe assumptions:

# Assume to_formulae_and_casks returns:
[key1, key2, formula1, formula2, some_other_object]

# partition { |o| o.is_a?(Key) } results in:
[
  [key1, key2],                           # Array of Key objects
  [formula1, formula2, some_other_object] # Array of NON-Key objects
]

@dynamicy dynamicy force-pushed the fix/to-keys-to-casks-cache-key-collision branch 7 times, most recently from 23b4865 to cc0a465 Compare September 28, 2025 20:22
@dynamicy dynamicy force-pushed the fix/to-keys-to-casks-cache-key-collision branch from cc0a465 to 4c3d570 Compare September 28, 2025 20:41
@dynamicy dynamicy marked this pull request as ready for review September 28, 2025 20:59
Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch and great work, thanks, you rock @dynamicy!

@MikeMcQuaid MikeMcQuaid added this pull request to the merge queue Sep 29, 2025
Merged via the queue into Homebrew:main with commit 70e42d6 Sep 29, 2025
38 checks passed
@dynamicy dynamicy deleted the fix/to-keys-to-casks-cache-key-collision branch September 29, 2025 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants