From 61976e0672e015f3a974e5fa6644975d08118a7d Mon Sep 17 00:00:00 2001
From: Mike McQuaid <mike@mikemcquaid.com>
Date: Wed, 30 Mar 2022 17:25:00 +0100
Subject: [PATCH] caskroom: add, use any_casks_installed?.

Add a new method for the two places we're checking if we have any
casks installed. This is much more performance and user friendly than
attempting to load every cask.

Fixes #13027
---
 Library/Homebrew/cask/caskroom.rb | 11 +++++++++--
 Library/Homebrew/cmd/doctor.rb    |  2 +-
 Library/Homebrew/cmd/list.rb      |  2 +-
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/Library/Homebrew/cask/caskroom.rb b/Library/Homebrew/cask/caskroom.rb
index ded8b2e81c964..a6012c19c221c 100644
--- a/Library/Homebrew/cask/caskroom.rb
+++ b/Library/Homebrew/cask/caskroom.rb
@@ -12,7 +12,14 @@ module Caskroom
 
     sig { returns(Pathname) }
     def self.path
-      @path ||= HOMEBREW_PREFIX.join("Caskroom")
+      @path ||= HOMEBREW_PREFIX/"Caskroom"
+    end
+
+    sig { returns(T::Boolean) }
+    def self.any_casks_installed?
+      return false unless path.exist?
+
+      path.children.select(&:directory?).any?
     end
 
     sig { void }
@@ -36,7 +43,7 @@ def self.ensure_caskroom_exists
     def self.casks(config: nil)
       return [] unless path.exist?
 
-      Pathname.glob(path.join("*")).sort.select(&:directory?).map do |path|
+      path.children.select(&:directory?).sort.map do |path|
         token = path.basename.to_s
 
         if (tap_path = CaskLoader.tap_paths(token).first)
diff --git a/Library/Homebrew/cmd/doctor.rb b/Library/Homebrew/cmd/doctor.rb
index 25fe3d7201670..fc7907a3fe169 100644
--- a/Library/Homebrew/cmd/doctor.rb
+++ b/Library/Homebrew/cmd/doctor.rb
@@ -48,7 +48,7 @@ def doctor
         check_missing_deps
       ]
       methods = (checks.all - slow_checks) + slow_checks
-      methods -= checks.cask_checks if Cask::Caskroom.casks.blank?
+      methods -= checks.cask_checks unless Cask::Caskroom.any_casks_installed?
     else
       methods = args.named
     end
diff --git a/Library/Homebrew/cmd/list.rb b/Library/Homebrew/cmd/list.rb
index 631ed7471b937..4766dd6b0296a 100644
--- a/Library/Homebrew/cmd/list.rb
+++ b/Library/Homebrew/cmd/list.rb
@@ -113,7 +113,7 @@ def list
         ohai "Formulae" if $stdout.tty? && !args.formula?
         safe_system "ls", *ls_args, HOMEBREW_CELLAR
       end
-      if !args.formula? && Cask::Caskroom.casks.any?
+      if !args.formula? && Cask::Caskroom.any_casks_installed?
         if $stdout.tty? && !args.cask?
           puts
           ohai "Casks"