Skip to content

Commit fa0fe2f

Browse files
philzclaude
andcommitted
exe-ssh-config-generator: fix Include line trapped inside Host block
The ensure_include() function checked for the Include line's existence but not its position. If a user added a Host block above the Include line, it became scoped to that block (SSH Host blocks end at the next Host/Match, not at blank lines), so the included config was only processed when connecting to that specific host. Now ensure_include() verifies the Include appears before any Host/Match directive, and moves it to the top if it has drifted inside a block. Prompt: I ran exe-ssh-config-generator with two keys and "ssh phil-dev" wasn't picking up the generated config. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f198220 commit fa0fe2f

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

bin/exe-ssh-config-generator

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,31 @@ def ensure_include():
159159
with open(SSH_CONFIG, "r") as f:
160160
content = f.read()
161161

162-
if INCLUDE_LINE in content:
162+
lines = content.splitlines(True)
163+
# Find where the Include line is (if present) and where Host/Match blocks start.
164+
include_idx = None
165+
first_host_idx = None
166+
for i, line in enumerate(lines):
167+
stripped = line.strip()
168+
if stripped == INCLUDE_LINE:
169+
include_idx = i
170+
if first_host_idx is None and (stripped.startswith("Host ") or stripped.startswith("Match ")):
171+
first_host_idx = i
172+
173+
if include_idx is not None and (first_host_idx is None or include_idx < first_host_idx):
174+
# Include exists and is before any Host/Match block — nothing to do.
163175
return
164176

177+
if include_idx is not None:
178+
# Include exists but is inside a Host/Match block — remove it and re-prepend.
179+
del lines[include_idx]
180+
# Also remove a trailing blank line if one was left behind.
181+
if include_idx < len(lines) and lines[include_idx].strip() == "":
182+
del lines[include_idx]
183+
165184
# Prepend the include so it takes priority
166185
with open(SSH_CONFIG, "w") as f:
167-
f.write(INCLUDE_LINE + "\n" + content)
186+
f.write(INCLUDE_LINE + "\n\n" + "".join(lines))
168187
print(f" added include to {SSH_CONFIG}")
169188

170189

0 commit comments

Comments
 (0)