Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Library/Homebrew/bundle/brewfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ def self.path(dash_writes_to_stdout: false, global: false, file: nil)
else
raise "'HOMEBREW_BUNDLE_FILE' cannot be specified with '--global'" if env_bundle_file.present?

if user_config_home && File.exist?("#{user_config_home}/Brewfile")
"#{user_config_home}/Brewfile"
if user_config_home
xdg_path = "#{user_config_home}/Brewfile"
legacy_path = Bundle.exchange_uid_if_needed! { "#{Dir.home}/.Brewfile" }

# Prefer existing file to maintain backwards compatibility
if File.exist?(xdg_path) || !File.exist?(legacy_path)
xdg_path
else
legacy_path
end
else
Bundle.exchange_uid_if_needed! do
"#{Dir.home}/.Brewfile"
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/bundle/dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def self.brewfile_path(global: false, file: nil)
sig { params(file: Pathname, content: String).void }
def self.write_file(file, content)
Bundle.exchange_uid_if_needed! do
file.parent.mkpath
file.open("w") { |io| io.write content }
end
end
Expand Down
14 changes: 14 additions & 0 deletions Library/Homebrew/test/bundle/brewfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@
expect(path).to eq(expected_pathname)
end
end

context "when HOMEBREW_USER_CONFIG_HOME is set but neither Brewfile exists" do
let(:config_dir_brewfile_exist) { false }

before do
# Mock that both XDG and legacy Brewfiles don't exist
allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile").and_return(false)
allow(File).to receive(:exist?).with("#{Dir.home}/.Brewfile").and_return(false)
end

it "returns the XDG path for initial Brewfile creation" do
expect(path).to eq(Pathname.new("#{env_user_config_home_value}/Brewfile"))
end
end
end

context "when HOMEBREW_BUNDLE_FILE has a value" do
Expand Down
Loading