From b749cbcffdc07415005ea879fff59e0e94989855 Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Tue, 19 Aug 2025 16:18:15 +0800 Subject: [PATCH 1/3] diagnostic: fix cask env var output With `each`, we don't actually output the processed values. We should therefore be using `filter_map` instead. --- Library/Homebrew/diagnostic.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 0c2e7b2935..01aa5d7465 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -1068,7 +1068,7 @@ module Homebrew locale_variables = ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).sort - cask_environment_variables = (locale_variables + environment_variables).sort.each do |var| + cask_environment_variables = (locale_variables + environment_variables).sort.filter_map do |var| next unless ENV.key?(var) var = %Q(#{var}="#{ENV.fetch(var)}") From 31598ca8ef54a55117cd84d6e4578973d8c5f1cd Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Tue, 19 Aug 2025 16:29:06 +0800 Subject: [PATCH 2/3] diagnostic: improve tilde expansion logic This is a bit pedantic, but the tilde expansion only occurs if the tilde is at the beginning of the string (if we only take the current user into account) [1]. [1]: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06_01 --- Library/Homebrew/diagnostic.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 01aa5d7465..b3493664d9 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -84,7 +84,12 @@ module Homebrew sig { params(path: String).returns(String) } def user_tilde(path) - path.gsub(Dir.home, "~") + home = Dir.home + if path == home + "~" + else + path.gsub(%r{^#{home}/}, "~/") + end end sig { returns(T.nilable(String)) } From 1eb070e2575f908980db3cdae34ca78079990723 Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Tue, 19 Aug 2025 16:42:01 +0800 Subject: [PATCH 3/3] diagnostic: don't expand tildes in cask env vars We double quote them so they don't get expanded. Also, use `sh_quote` to quote the variable values properly. --- Library/Homebrew/diagnostic.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index b3493664d9..bdefcd9ea3 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -1076,8 +1076,7 @@ module Homebrew cask_environment_variables = (locale_variables + environment_variables).sort.filter_map do |var| next unless ENV.key?(var) - var = %Q(#{var}="#{ENV.fetch(var)}") - user_tilde(var) + %Q(#{var}="#{Utils::Shell.sh_quote(ENV.fetch(var))}") end add_info "Cask Environment Variables:", cask_environment_variables