According to getpwnam(3):
An application that wants to determine its user's home directory
should inspect the value of HOME (rather than the value
getpwuid(getuid())->pw_dir) since this allows the user to modify
their notion of "the home directory" during a login session.
This is important for systems where many users share the same UID, and for test systems that change HOME to avoid
interferencewith the user’s real home directory. It matches what most applications do, as well as what glibc does for
glob("~",GLOB_TILDE, …) and wordexp("~", …).
There was some previous discussion of this in 2016, where although there were some questions about the use case, there
seemedto be general support for the concept:
https://www.postgresql.org/message-id/flat/CAEH6cQqbdbXoUHJBbX9ixwfjFFsUC-a8hFntKcci%3DdiWgBb3fQ%40mail.gmail.com
Regardless of whether one thinks modifying HOME is a good idea, if we happen to find ourselves in that case, we should
respectthe modified HOME, so that when the user creates (say) a ~/.pgpass file, we’ll look for it at the same place the
user’seditor created it. getenv() also skips the overhead of reading /etc/passwd as an added bonus.
The way I ran into this issue myself was in a test suite that runs on GitHub Actions, which automatically sets
HOME=/github/home.
Anders