Skip to content

Fix bash OSC 7 reporting $HOSTNAME instead of gethostname(2)#277

Merged
dakra merged 1 commit into
mainfrom
fix/bash-osc7-hostname
May 15, 2026
Merged

Fix bash OSC 7 reporting $HOSTNAME instead of gethostname(2)#277
dakra merged 1 commit into
mainfrom
fix/bash-osc7-hostname

Conversation

@dakra
Copy link
Copy Markdown
Owner

@dakra dakra commented May 15, 2026

Summary

  • Bash's shell integration (etc/shell/ghostel.bash) emitted $HOSTNAME in the OSC 7 directory-tracking sequence. $HOSTNAME is inherited from the parent environment — toolbox/container runtimes export it with a value that disagrees with gethostname(2). Emacs' system-name reads gethostname(2) directly, so the host strings didn't match, ghostel--update-directory misclassified the buffer as remote, and TRAMP fired on every cd. zsh ($HOST) and fish ((hostname)) read gethostname(2) directly and aren't affected.
  • Capture the real kernel hostname via ${var@P} on \H (bash 4.4+) — same value bash uses internally for the \H PS1 escape, sourced from gethostname(2) at startup and not overridable from the environment. Zero forks, no external binary dependency.
  • On bash <4.4 the @P transform is unavailable (it produces a bad substitution error and leaves the variable as the literal \H, which would actually break OSC 7 worse than the original bug). Guard with BASH_VERSINFO and fall back to $HOSTNAME — pre-tramp getting triggered inside local fedora-toolbox container #276 behavior for those users, no regression.
  • New regression test sources the integration in a bash --noprofile --norc subprocess with a poisoned HOSTNAME=…, calls __ghostel_osc7, and asserts the emitted host passes ghostel--local-host-p — the same predicate the production code applies in ghostel--update-directory.

Fixes #276.

Test plan

  • make -j4 all passes (382 tests).
  • New test ghostel-test-bash-osc7-ignores-env-hostname passes against the patched bash integration.
  • Same test fails against main's bash integration (confirms it exercises the bug, not just any assertion).
  • bash -n etc/shell/ghostel.bash clean on both modern bash (5.x) and /bin/bash 3.2.
  • Manual smoke under a polluted HOSTNAME: HOSTNAME=lying-fake bash -c 'source etc/shell/ghostel.bash; echo $__ghostel_host' reports the real kernel hostname on bash 4.4+.

Bash inherits $HOSTNAME from the environment.  Toolbox/container
runtimes export it with a value that disagrees with the kernel
hostname, so emacs' (system-name) — which reads gethostname(2) —
sees a mismatch, ghostel--update-directory classifies the buffer
as remote, and TRAMP fires on every cd.  zsh ($HOST) and fish
($(hostname)) read gethostname(2) directly and aren't affected.

Capture the real kernel hostname via ${var@P} on \H (bash 4.4+),
which expands the same value bash uses internally for the \H
prompt escape — sourced from gethostname(2) at startup and not
overridable from the environment.  On bash <4.4 the @p transform
is unavailable, so fall back to $HOSTNAME (pre-fix behavior).

Fixes #276.
@dakra dakra force-pushed the fix/bash-osc7-hostname branch from aaffd87 to be0f53f Compare May 15, 2026 11:18
@dakra dakra merged commit be0f53f into main May 15, 2026
22 checks passed
@dakra dakra deleted the fix/bash-osc7-hostname branch May 15, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tramp getting triggered inside local fedora-toolbox container

1 participant