Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions metagpt/utils/dependency_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ async def load(self):
if not self._filename.exists():
return
json_data = await aread(self._filename)
json_data = re.sub(r"\\+", "/", json_data) # Compatible with windows path
self._dependencies = json.loads(json_data)
dependencies = json.loads(json_data)
# Normalize separators after decoding so JSON escapes like \uXXXX and \" stay intact.
self._dependencies = {
re.sub(r"\\+", "/", filename): [re.sub(r"\\+", "/", path) for path in paths]
for filename, paths in dependencies.items()
}

@handle_exception
async def save(self):
Expand Down
52 changes: 52 additions & 0 deletions tests/metagpt/utils/test_dependency_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,57 @@ class Input(BaseModel):
assert not file.exists


@pytest.mark.asyncio
async def test_dependency_file_unicode_and_quoted_paths_roundtrip(tmp_path):
"""Unicode/quoted filenames must survive save/load without corrupting JSON escapes."""
targets = [
("src/主程序.py", {"docs/设计.md"}),
("src/café.py", {"docs/naïve.md"}),
("src/🚀.py", {"docs/🎯.md"}),
('src/say "hi".py', {'docs/quote "x".md'}),
]

store = DependencyFile(tmp_path)
for filename, deps in targets:
await store.update(filename=filename, dependencies=deps)

reloaded = DependencyFile(tmp_path)
await reloaded.load()
for filename, deps in targets:
assert await reloaded.get(filename, persist=False) == deps


@pytest.mark.asyncio
async def test_dependency_file_load_preserves_unrelated_entry_after_update(tmp_path):
"""A second update must not persist a corrupted first entry back to disk."""
store = DependencyFile(tmp_path)
await store.update("src/主程序.py", {"docs/设计.md"})
await store.update("other.txt", {"dep.txt"})

reloaded = DependencyFile(tmp_path)
assert await reloaded.get("src/主程序.py") == {"docs/设计.md"}
assert await reloaded.get("other.txt") == {"dep.txt"}


@pytest.mark.asyncio
async def test_dependency_file_normalizes_legacy_windows_separators(tmp_path):
"""Existing files that used backslash path separators still load."""
(tmp_path / ".dependencies.json").write_text(
'{"src\\\\main.py": ["docs\\\\design.md"]}',
encoding="utf-8",
)
store = DependencyFile(tmp_path)
await store.load()
assert await store.get("src/main.py", persist=False) == {"docs/design.md"}


@pytest.mark.asyncio
async def test_dependency_file_deletes_unicode_entry(tmp_path):
store = DependencyFile(tmp_path)
await store.update("src/主程序.py", {"docs/设计.md"})
await store.update("src/主程序.py", dependencies=None)
assert await store.get("src/主程序.py") == set()


if __name__ == "__main__":
pytest.main([__file__, "-s"])
Loading