fix(init): use LocalHostName for SHORT_HOST on macos, fix zrecompile

Init on macos systems: determine SHORT_HOST from  scutil's
`LocalHostName` instead of `ComputerName`. This addresses an issue
with zcompdump not being written on computers with unicode characters
in the name.

From `man scutil`:

> Supported preferences include:
>
>   ComputerName   The user-friendly name for the system.
>   LocalHostName  The local (Bonjour) host name.
>   HostName       The name associated with hostname(1) and gethostname(3).

`ComputerName` often contains spaces and special characters,
while `LocalHostName` is generated from `ComputerName` (by default),
and normalized to look like a normal host name.

Example:

```sh-session
$ scutil --get ComputerName
Sergii’s Mac
$ scutil --get LocalHostName
Sergiis-Mac
```

When ComputerName is used, my zcompdump file looks like this:

```sh-session
$ ls ~/.zcompdump*
'/Users/sergii/.zcompdump-Sergii’s Mac-5.9'
```

Yes, it contains a unicode character:

```sh-session
$ print -N "’" | od -tc -tu1
0000000    ’  **  **  \0
          226 128 153   0
```

Because of that, zrecompile fails to write the compdump.

```sh-session
$ zrecompile -p .zcompdump-Sergii’s\ Mac-5.9
re-compiling .zcompdump-Sergii’s Mac-5.9.zwc: zrecompile:zcompile:133: can't write zwc file: .zcompdump-Sergii’s Mac-5.9.zwc
re-compiling .zcompdump-Sergii’s Mac-5.9.zwc: failed
$ cp .zcompdump-Sergii’s\ Mac-5.9 .zcompdump-stk
$ zrecompile -p .zcompdump-stk
re-compiling .zcompdump-stk.zwc: succeeded
```
This commit is contained in:
Sergii Tkachenko 2025-06-15 18:51:42 -07:00
commit 5738a17688

View file

@ -99,8 +99,8 @@ done
# Figure out the SHORT hostname
if [[ "$OSTYPE" = darwin* ]]; then
# macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST="${HOST/.*/}"
# macOS's $HOST changes with dhcp, etc. Use LocalHostName if possible.
SHORT_HOST=$(scutil --get LocalHostName 2>/dev/null) || SHORT_HOST="${HOST/.*/}"
else
SHORT_HOST="${HOST/.*/}"
fi