Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(server): decode file content as UTF-8 (#3249)

Cheruku Sri Charan Reddy6027d314b8fb
diff --git a/packages/server/src/repowise/server/routers/repos.py b/packages/server/src/repowise/server/routers/repos.py
index ddbbf369c..86f9fda18 100644
--- a/packages/server/src/repowise/server/routers/repos.py
+++ b/packages/server/src/repowise/server/routers/repos.py
@@ -894,7 +894,7 @@ async def get_file_content(
raise HTTPException(status_code=404, detail="File not found")
try:
- content = target.read_text(errors="replace")
+ content = target.read_text(encoding="utf-8", errors="replace")
except OSError as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
diff --git a/tests/unit/server/test_file_content_guard.py b/tests/unit/server/test_file_content_guard.py
index 55ff99904..c089a534b 100644
--- a/tests/unit/server/test_file_content_guard.py
+++ b/tests/unit/server/test_file_content_guard.py
@@ -44,6 +44,35 @@ async def test_serves_indexed_file(client: AsyncClient, app, tmp_path: Path) ->
assert resp.text == "print('hi')\n"
[email protected]("default_encoding", ["utf-8", "cp1252"])
+async def test_serves_utf8_content_independent_of_default_encoding(
+ client: AsyncClient, app, tmp_path: Path, monkeypatch, default_encoding: str
+) -> None:
+ repo = await create_test_repo(client, tmp_path)
+ target = Path(repo["local_path"]) / "main.py"
+ content = "# café — 日本語\n"
+ target.write_bytes(content.encode("utf-8"))
+ await _index_file(app.state.session_factory, repo["id"], "main.py")
+
+ original_read_text = Path.read_text
+
+ def read_text_with_default_encoding(
+ path: Path, encoding: str | None = None, errors: str | None = None
+ ) -> str:
+ if path == target and encoding is None:
+ encoding = default_encoding
+ return original_read_text(path, encoding=encoding, errors=errors)
+
+ monkeypatch.setattr(Path, "read_text", read_text_with_default_encoding)
+
+ resp = await client.get(
+ f"/api/repos/{repo['id']}/file-content", params={"file_path": "main.py"}
+ )
+ assert resp.status_code == 200
+ assert resp.text == content
+
+
@pytest.mark.asyncio
async def test_serves_indexed_dot_paths(client: AsyncClient, app, tmp_path: Path) -> None:
"""The traverser walks `.github` and friends, so those files stay readable.