From 4c97a795a9f46490bca9599a202e96de870346fe Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Tue, 25 Mar 2025 13:47:44 +0000 Subject: [PATCH] `brew bundle env`: sort and filter output. - Sort output by key so it's more readable. - Skip exporting empty values because shell scripts treat them as unset. - Skip exporting non-Homebrew things that were already set in the old environment to avoid massive duplication and higher chances of e.g. bad escapes breaking things. --- Library/Homebrew/bundle/commands/exec.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/bundle/commands/exec.rb b/Library/Homebrew/bundle/commands/exec.rb index b2c39f17d3..0089f50ef3 100644 --- a/Library/Homebrew/bundle/commands/exec.rb +++ b/Library/Homebrew/bundle/commands/exec.rb @@ -51,6 +51,10 @@ module Homebrew # Cleanup Homebrew's global environment HOMEBREW_ENV_CLEANUP.each { |key| ENV.delete(key) } + # Store the old environment so we can check if things were already set + # before we start mutating it. + old_env = ENV.to_h + # Setup Homebrew's ENV extensions ENV.activate_extensions! raise UsageError, "No command to execute was specified!" if args.blank? @@ -141,7 +145,13 @@ module Homebrew raise "command was not found in your PATH: #{command}" if command.exclude?("/") && which(command).nil? if subcommand == "env" - ENV.each do |key, value| + ENV.sort.each do |key, value| + # No need to export empty values. + next if value.blank? + + # Skip exporting non-Homebrew things that were already set in the old environment. + next if !key.start_with?("HOMEBREW_") && old_env.key?(key) && old_env[key] == value + puts "export #{key}=\"#{Utils::Shell.sh_quote(value)}\"" end return