From b54b022f73628360e109586013b6b22aacd92f5d Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 2 Nov 2020 12:56:59 +0000 Subject: [PATCH] Keg.for: handle non-existent path. Otherwise `path.realpath` will raise `Errno::ENOENT` rather than the expected `NotAKegError`. Fixes https://github.com/Homebrew/brew/issues/9015 --- Library/Homebrew/keg.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 902ed12f38572..23466dd3a0192 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -183,13 +183,15 @@ def self.find_some_installed_dependents(kegs) # if path is a file in a keg then this will return the containing Keg object def self.for(path) - path = path.realpath - until path.root? - return Keg.new(path) if path.parent.parent == HOMEBREW_CELLAR.realpath + original_path = path + if original_path.exist? && (path = original_path.realpath) + until path.root? + return Keg.new(path) if path.parent.parent == HOMEBREW_CELLAR.realpath - path = path.parent.realpath # realpath() prevents root? failing + path = path.parent.realpath # realpath() prevents root? failing + end end - raise NotAKegError, "#{path} is not inside a keg" + raise NotAKegError, "#{original_path} is not inside a keg" end def self.all