Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(editor-files): preserve backslashes in managed block replacements (#4530)
ANO3457272d04dc7a14
diff --git a/packages/core/src/repowise/core/generation/editor_files/base.py b/packages/core/src/repowise/core/generation/editor_files/base.pyindex 3547e3ba2..8798d417e 100644--- a/packages/core/src/repowise/core/generation/editor_files/base.py+++ b/packages/core/src/repowise/core/generation/editor_files/base.py@@ -93,7 +93,7 @@ class BaseEditorFileGenerator(ABC):if marker_start in existing:# Replace the managed section in-placepattern = re.escape(marker_start) + r".*?" + re.escape(marker_end)- content = re.sub(pattern, wrapped, existing, flags=re.DOTALL)+ content = re.sub(pattern, lambda _match: wrapped, existing, flags=re.DOTALL)else:# Append managed section; preserve all existing contentcontent = existing.rstrip() + "\n\n" + wrapped + "\n"@@ -113,5 +113,5 @@ class BaseEditorFileGenerator(ABC):existing = target.read_text(encoding="utf-8")if marker_start in existing:pattern = re.escape(marker_start) + r".*?" + re.escape(marker_end)- return re.sub(pattern, wrapped, existing, flags=re.DOTALL)+ return re.sub(pattern, lambda _match: wrapped, existing, flags=re.DOTALL)return existing.rstrip() + "\n\n" + wrapped + "\n"diff --git a/tests/unit/generation/test_editor_file_base.py b/tests/unit/generation/test_editor_file_base.pyindex 826584c85..f7a473384 100644--- a/tests/unit/generation/test_editor_file_base.py+++ b/tests/unit/generation/test_editor_file_base.py@@ -412,6 +412,47 @@ def test_write_replaces_between_markers(gen, tmp_path):assert "test-repo" in new_content # new managed content present[email protected]("title", [r"C:\Users\x", r"C:\new\tools", r"\1", r"\g<0>"])[email protected]("existing_kind", ["marked", "unmarked", "absent"])[email protected]("method", ["write", "render_full"])+def test_managed_rewrite_preserves_literal_backslashes(+ any_gen, tmp_path, title, existing_kind, method+):+ from dataclasses import replace++ from repowise.core.generation.editor_files.data import DecisionSummary++ data = replace(+ _minimal_data(),+ decisions=[DecisionSummary(title=title, status="active", rationale="Keep literal text.")],+ )+ start = any_gen.MARKER_START_FMT.format(tag=any_gen.marker_tag)+ end = any_gen.MARKER_END_FMT.format(tag=any_gen.marker_tag)+ prefix = "# User notes\nKeep this.\n\n"+ suffix = "\n\nUser footer.\n"+ target = tmp_path / any_gen.filename+ rendered = any_gen.render(data)+ assert title in rendered+ wrapped = f"{start}\n{rendered}\n{end}"+ if existing_kind == "marked":+ target.write_text(f"{prefix}{start}\nold managed text\n{end}{suffix}", encoding="utf-8")+ expected = prefix + wrapped + suffix+ elif existing_kind == "unmarked":+ target.write_text(prefix, encoding="utf-8")+ expected = prefix.rstrip() + "\n\n" + wrapped + "\n"+ else:+ expected = any_gen.user_placeholder + "\n" + wrapped + "\n"++ result = getattr(any_gen, method)(tmp_path, data)+ actual = result.read_text(encoding="utf-8") if method == "write" else result+ assert actual == expected+ any_gen.write(tmp_path, data)+ assert target.read_text(encoding="utf-8") == expected+ assert any_gen.render_full(tmp_path, data) == expected+ any_gen.write(tmp_path, data)+ assert target.read_text(encoding="utf-8") == expected++def test_write_replaces_only_managed_section(gen, tmp_path):marker_start = gen.MARKER_START_FMT.format(tag=gen.marker_tag)marker_end = gen.MARKER_END_FMT.format(tag=gen.marker_tag)