fix(install): keep a symlinked instructions file on uninstall

When removing the graphify section leaves an instructions file empty,
uninstall deletes it. If the file is a symlink, only the link was deleted
and the graphify section stayed in the file it points to. Now uninstall
writes through the symlink instead, so the target is cleaned and the
link stays.

Applies to .claude/CLAUDE.md, CLAUDE.md, GEMINI.md, AGENTS.md,
CODEBUDDY.md and .github/copilot-instructions.md.

(cherry picked from commit cf260b5163)
This commit is contained in:
berce authored and safishamsi committed 2026-09-30 20:41:10 +01:00
1 parent c5c4ee6bfd
commit b30a40a171
2 files changed
+80 -6

No files matched your search

+12 -6
View File
@@ -419,7 +419,8 @@ def _remove_claude_skill_registration(project_dir: Path) -> None:
cleaned = _remove_marker_section(content, _SKILL_REGISTRATION_MARKER, boundary_prefix="# ")
if cleaned is None:
return
if cleaned:
# A symlink stays: writing through it cleans the file it points to.
if cleaned or claude_md.is_symlink():
# newline="" so the rest of the file's own line endings are never
# translated on write (#3668, same CRLF issue as the insert side).
claude_md.write_text(cleaned + "\n", encoding="utf-8", newline="")
@@ -1046,7 +1047,8 @@ def gemini_uninstall(project_dir: Path | None = None, *, project: bool = False,
if cleaned is None:
print("graphify section not found in GEMINI.md - nothing to do")
return
if cleaned:
# A symlink stays: writing through it cleans the file it points to.
if cleaned or target.is_symlink():
target.write_text(cleaned + "\n", encoding="utf-8")
print(f"graphify section removed from {target.resolve()}")
else:
@@ -1145,7 +1147,8 @@ def vscode_uninstall(project_dir: Path | None = None) -> None:
cleaned = _remove_marker_section(content, _VSCODE_INSTRUCTIONS_MARKER)
if cleaned is None:
return
if cleaned:
# A symlink stays: writing through it cleans the file it points to.
if cleaned or instructions.is_symlink():
instructions.write_text(cleaned + "\n", encoding="utf-8")
print(f" graphify section removed from {instructions}")
else:
@@ -1904,7 +1907,8 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None:
_uninstall_kilo_plugin(project_dir or Path("."))
return
if cleaned:
# A symlink stays: writing through it cleans the file it points to.
if cleaned or target.is_symlink():
target.write_text(cleaned + "\n", encoding="utf-8")
print(f"graphify section removed from {target.resolve()}")
else:
@@ -2136,7 +2140,8 @@ def _strip_graphify_md_section(target: Path) -> bool:
cleaned = _remove_marker_section(content, _CLAUDE_MD_MARKER)
if cleaned is None:
return False
if cleaned:
# A symlink stays: writing through it cleans the file it points to.
if cleaned or target.is_symlink():
target.write_text(cleaned + "\n", encoding="utf-8")
print(f"graphify section removed from {target.resolve()}")
else:
@@ -2230,7 +2235,8 @@ def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = Fals
print("graphify section not found in CODEBUDDY.md - nothing to do")
return
if cleaned:
# A symlink stays: writing through it cleans the file it points to.
if cleaned or target.is_symlink():
target.write_text(cleaned + "\n", encoding="utf-8")
print(f"graphify section removed from {target.resolve()}")
else:
+68
View File
@@ -0,0 +1,68 @@
"""Uninstall through a symlinked instructions file.
When removing graphify's section leaves an instructions file empty, uninstall
deletes the file. If that path is a symlink, deleting it removed only the link
and left graphify's section in the file the link points to.
"""
from pathlib import Path
import pytest
from graphify.install import (
_AGENTS_MD_MARKER,
_CLAUDE_MD_MARKER,
_CODEBUDDY_MD_MARKER,
_GEMINI_MD_MARKER,
_SKILL_REGISTRATION_MARKER,
_VSCODE_INSTRUCTIONS_MARKER,
_agents_uninstall,
_remove_claude_skill_registration,
claude_uninstall,
codebuddy_uninstall,
gemini_uninstall,
vscode_uninstall,
)
# (instructions file relative to the project, graphify's heading, uninstall call)
CASES = [
(".claude/CLAUDE.md", _SKILL_REGISTRATION_MARKER, _remove_claude_skill_registration),
("CLAUDE.md", _CLAUDE_MD_MARKER, claude_uninstall),
("GEMINI.md", _GEMINI_MD_MARKER, gemini_uninstall),
(".github/copilot-instructions.md", _VSCODE_INSTRUCTIONS_MARKER, vscode_uninstall),
("AGENTS.md", _AGENTS_MD_MARKER, _agents_uninstall),
("CODEBUDDY.md", _CODEBUDDY_MD_MARKER, codebuddy_uninstall),
]
IDS = [rel for rel, _, _ in CASES]
def _section(marker: str) -> str:
return f"{marker}\n\ngraphify rules\n"
@pytest.mark.parametrize("rel,marker,uninstall", CASES, ids=IDS)
def test_uninstall_empties_symlink_target_and_keeps_link(requires_symlinks, tmp_path, rel, marker, uninstall):
project = tmp_path / "project"
target = tmp_path / "dotfiles" / "rules.md"
target.parent.mkdir()
target.write_text(_section(marker), encoding="utf-8")
link = project / rel
link.parent.mkdir(parents=True)
link.symlink_to(target)
uninstall(project)
assert link.is_symlink(), "the user's symlink was deleted"
assert link.resolve() == target.resolve()
assert marker not in target.read_text(encoding="utf-8"), "graphify's section is still in the link target"
@pytest.mark.parametrize("rel,marker,uninstall", CASES, ids=IDS)
def test_uninstall_still_deletes_a_regular_file_left_empty(tmp_path, rel, marker, uninstall):
project = tmp_path / "project"
path = project / rel
path.parent.mkdir(parents=True)
path.write_text(_section(marker), encoding="utf-8")
uninstall(project)
assert not path.exists()