Skip to content

Commit 2aa1add

Browse files
Ralph Agentclaude
andcommitted
🐛 Fix _write_env_file data loss issue
Preserve custom environment variables when running `onboard env`: - Load existing .env before overwriting - Identify custom keys not in .env.example - Append under "# Custom variables" section Fixes critical data loss bug where user-added env vars were silently deleted. Addresses Greptile review feedback on lines 335-356. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c51732a commit 2aa1add

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

onboard.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,12 @@ def _prompt_env_value(key: str, default: str, current_value: str) -> str:
333333

334334

335335
def _write_env_file(entries: list[dict[str, str]], values: dict[str, str]) -> int:
336-
"""Write .env file preserving group structure. Returns count of skipped keys."""
336+
"""Write .env file preserving group structure and custom vars. Returns count of skipped keys."""
337+
# Load existing env and identify custom variables not in .env.example
338+
existing = _load_existing_env()
339+
tracked_keys = {entry["key"] for entry in entries}
340+
custom_vars = {k: v for k, v in existing.items() if k not in tracked_keys}
341+
337342
lines: list[str] = []
338343
current_group = ""
339344
skipped = 0
@@ -352,6 +357,13 @@ def _write_env_file(entries: list[dict[str, str]], values: dict[str, str]) -> in
352357
lines.append(f"# {key}={entry['default']}")
353358
skipped += 1
354359

360+
# Preserve custom variables not in .env.example
361+
if custom_vars:
362+
lines.append("")
363+
lines.append("# Custom variables")
364+
for key, value in custom_vars.items():
365+
lines.append(f"{key}={value}")
366+
355367
(PROJECT_ROOT / ".env").write_text("\n".join(lines) + "\n")
356368
return skipped
357369

0 commit comments

Comments
 (0)