Skip to content

Commit

Permalink
Deprecate installing casks/formulae from paths.
Browse files Browse the repository at this point in the history
We've already disabled installing casks/formulae from URLs and we
regularly tell people not to install from paths so let's just deprecate
this behaviour entirely.

Even Homebrew developers do not need to work this way.
  • Loading branch information
MikeMcQuaid committed Sep 26, 2024
1 parent 05cde76 commit c2496b8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
9 changes: 7 additions & 2 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ def self.try_new(ref, warn: false)
end

return if %w[.rb .json].exclude?(path.extname)
return if path.to_s.include?("/Formula/")
return unless path.expand_path.exist?

return if Homebrew::EnvConfig.forbid_packages_from_paths? &&
!path.realpath.to_s.start_with?("#{Caskroom.path}/", "#{HOMEBREW_LIBRARY}/Taps/")
if !path.realpath.to_s.start_with?("#{Caskroom.path}/", "#{HOMEBREW_LIBRARY}/Taps/") && !ENV["HOMEBREW_TESTS"]
odeprecated "installing formulae from paths or URLs", "installing formulae from taps"
return if Homebrew::EnvConfig.forbid_packages_from_paths?
end

new(path)
end
Expand Down Expand Up @@ -195,6 +198,8 @@ def initialize(url)
def load(config:)
path.dirname.mkpath

odeprecated "installing casks from paths or URLs", "installing casks from taps"

if ALLOWED_URL_SCHEMES.exclude?(url.scheme)
raise UnsupportedInstallationMethod,
"Non-checksummed download of #{name} formula file from an arbitrary URL is unsupported! " \
Expand Down
10 changes: 8 additions & 2 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,10 @@ def self.try_new(ref, from: T.unsafe(nil), warn: false)

return unless path.expand_path.exist?

return if Homebrew::EnvConfig.forbid_packages_from_paths? &&
!path.realpath.to_s.start_with?("#{HOMEBREW_CELLAR}/", "#{HOMEBREW_LIBRARY}/Taps/")
if !path.realpath.to_s.start_with?("#{HOMEBREW_CELLAR}/", "#{HOMEBREW_LIBRARY}/Taps/") && !ENV["HOMEBREW_TESTS"]
odeprecated "installing formulae from paths or URLs", "installing formulae from taps"
return if Homebrew::EnvConfig.forbid_packages_from_paths?
end

options = if (tap = Tap.from_path(path))
# Only treat symlinks in taps as aliases.
Expand Down Expand Up @@ -701,6 +703,9 @@ class FromURILoader < FormulaLoader
.returns(T.nilable(T.attached_class))
}
def self.try_new(ref, from: T.unsafe(nil), warn: false)
unless ENV["HOMEBREW_TESTS"]
odeprecated "installing formulae from paths or URLs", "installing formulae from taps"
end
return if Homebrew::EnvConfig.forbid_packages_from_paths?

# Cache compiled regex
Expand Down Expand Up @@ -740,6 +745,7 @@ def load_file(flags:, ignore_errors:)
"`brew extract` or `brew create` and `brew tap-new` to create a formula file in a tap " \
"on GitHub instead."
end

HOMEBREW_CACHE_FORMULA.mkpath
FileUtils.rm_f(path)
Utils::Curl.curl_download url, to: path
Expand Down
10 changes: 5 additions & 5 deletions Library/Homebrew/test/cask/cask_loader/from_uri_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@
loader = described_class.new("https://brew.sh/foo.rb")
expect do
loader.load(config: nil)
end.to raise_error(UnsupportedInstallationMethod)
end.to raise_error(MethodDeprecatedError)
end

it "raises an error when given an ftp URL" do
loader = described_class.new("ftp://brew.sh/foo.rb")
expect do
loader.load(config: nil)
end.to raise_error(UnsupportedInstallationMethod)
end.to raise_error(MethodDeprecatedError)
end

it "raises an error when given an sftp URL" do
loader = described_class.new("sftp://brew.sh/foo.rb")
expect do
loader.load(config: nil)
end.to raise_error(UnsupportedInstallationMethod)
end.to raise_error(MethodDeprecatedError)
end

it "does not raise an error when given a file URL" do
it "raises an error when given a file URL" do
loader = described_class.new("file://#{TEST_FIXTURE_DIR}/cask/Casks/local-caffeine.rb")
expect do
loader.load(config: nil)
end.not_to raise_error(UnsupportedInstallationMethod)
end.to raise_error(MethodDeprecatedError)
end
end
end
8 changes: 1 addition & 7 deletions Library/Homebrew/test/cask/cask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,10 @@
expect(c.token).to eq("caffeine")
end

it "returns an instance of the Cask from a URL", :needs_utils_curl, :no_api do
c = Cask::CaskLoader.load("file://#{tap_path}/Casks/local-caffeine.rb")
expect(c).to be_a(described_class)
expect(c.token).to eq("local-caffeine")
end

it "raises an error when failing to download a Cask from a URL", :needs_utils_curl, :no_api do
expect do
Cask::CaskLoader.load("file://#{tap_path}/Casks/notacask.rb")
end.to raise_error(Cask::CaskUnavailableError)
end.to raise_error(MethodDeprecatedError)
end

it "returns an instance of the Cask from a relative file location" do
Expand Down

0 comments on commit c2496b8

Please sign in to comment.