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
This commit is contained in:
Ruoyu Zhong 2025-08-19 16:29:06 +08:00
parent b749cbcffd
commit 31598ca8ef
No known key found for this signature in database

View File

@ -84,7 +84,12 @@ module Homebrew
sig { params(path: String).returns(String) } sig { params(path: String).returns(String) }
def user_tilde(path) def user_tilde(path)
path.gsub(Dir.home, "~") home = Dir.home
if path == home
"~"
else
path.gsub(%r{^#{home}/}, "~/")
end
end end
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }