fix(wiki): unlink the stale twin on case-sensitive filesystems, don't rename over the fresh file

Follow-up to #4798. Its case-only-relabel sweep ran the two-step rename
unconditionally whenever the on-disk name differed from the produced name
by case. That is right on a case-INSENSITIVE filesystem (APFS, NTFS)
where the two are the same inode, but on a case-SENSITIVE filesystem
(ext4, most Linux CI and production hosts) the run already wrote the
correct content to a distinct file, so renaming the stale lower-case twin
over it silently restored the old body: no missing file, no dangling
link, just wrong content. #4508 never existed on case-sensitive hosts, so
the PR turned a non-bug into a silent corruption there.

Guard the rename with target.exists() and not target.samefile(old): a
distinct inode is a stale twin and is unlinked; the same inode takes the
rename. Adds a content-level regression test (the PR's existence/link
tests pass on both filesystems even with the bug, so they could not catch
this); it fails on a case-sensitive host without the guard.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
safishamsiandClaude Opus 4.8 committed 2026-10-27 18:00:50 +00:00
1 parent c65d46d507
commit ef51a3837a
2 files changed
+37 -7

No files matched your search

+15 -7
View File
@@ -462,12 +462,20 @@ def to_wiki(
if wanted is None:
old_article.unlink()
elif old_article.name != wanted:
# Case-only relabel: move the entry so the new case reaches disk and
# the link in index.md resolves. A two-step rename through a temporary
# name is required because a case-insensitive filesystem considers
# source and destination the same file.
temp = out / f".{wanted}.case-tmp"
old_article.rename(temp)
temp.rename(out / wanted)
# Case-only relabel. On a case-SENSITIVE filesystem (ext4, most Linux
# CI and production hosts) this run already wrote the correct content
# to `out / wanted` as a DISTINCT file, so the lower-case `old_article`
# is just a stale twin: unlink it, never rename it over the fresh file
# (doing so would silently restore the stale body). Only on a
# case-INSENSITIVE filesystem (APFS, NTFS) are the two the same inode,
# where a two-step rename through a temporary name is needed to make
# the new case reach disk so the index.md link resolves.
target = out / wanted
if target.exists() and not target.samefile(old_article):
old_article.unlink()
else:
temp = out / f".{wanted}.case-tmp"
old_article.rename(temp)
temp.rename(target)
return count
+22
View File
@@ -68,6 +68,28 @@ def test_case_only_relabel_leaves_no_dangling_link(tmp_path):
assert missing == [], missing
def test_case_only_relabel_keeps_the_new_content_not_the_stale_twin(tmp_path):
"""The surviving article must carry the NEW label's body, not the stale one.
On a case-sensitive filesystem (ext4, most Linux CI) the relabel writes the
correct `# Groups` article to a distinct `Groups.md`, while `groups.md` lingers
with the old `# groups` heading. Renaming the stale twin over the fresh file
(instead of unlinking it) silently restores the old body: no missing file, no
dangling link, just wrong content. The other tests pass on both filesystems
even with that bug, so this content check is the one that catches it on
case-sensitive hosts.
"""
g = _graph()
to_wiki(g, {0: ["a", "b"]}, tmp_path, community_labels={0: "groups"})
to_wiki(g, {0: ["a", "b"]}, tmp_path, community_labels={0: "Groups"})
articles = [p for p in tmp_path.glob("*.md") if p.name != "index.md"]
assert len(articles) == 1, [p.name for p in articles]
body = articles[0].read_text(encoding="utf-8")
assert "# Groups" in body, body
assert "# groups" not in body, body
def test_a_genuinely_absent_article_is_still_swept(tmp_path):
"""The folded comparison must not keep unrelated stale articles alive."""
g = _graph()