Fix windows error with aiofile failing on starter projects

This commit is contained in:
Joey Yakimowich-Payne 2025-08-12 13:34:22 -06:00
commit 8130c06f5b

View file

@ -4,6 +4,7 @@ import io
import json import json
import re import re
import shutil import shutil
import sys
import zipfile import zipfile
from collections import defaultdict from collections import defaultdict
from copy import deepcopy from copy import deepcopy
@ -521,8 +522,12 @@ async def load_starter_projects(retries=3, delay=1) -> list[tuple[anyio.Path, di
async for file in folder.glob("*.json"): async for file in folder.glob("*.json"):
attempt = 0 attempt = 0
while attempt < retries: while attempt < retries:
async with async_open(str(file), "r", encoding="utf-8") as f: if sys.platform.startswith("win"):
content = await f.read() # Windows-specific: avoid aiofile which can raise WinError 87 in some environments
content = await file.read_text(encoding="utf-8")
else:
async with async_open(str(file), "r", encoding="utf-8") as f:
content = await f.read()
try: try:
project = orjson.loads(content) project = orjson.loads(content)
starter_projects.append((file, project)) starter_projects.append((file, project))
@ -631,8 +636,12 @@ def get_project_data(project):
async def update_project_file(project_path: anyio.Path, project: dict, updated_project_data) -> None: async def update_project_file(project_path: anyio.Path, project: dict, updated_project_data) -> None:
project["data"] = updated_project_data project["data"] = updated_project_data
async with async_open(str(project_path), "w", encoding="utf-8") as f: if sys.platform.startswith("win"):
await f.write(orjson.dumps(project, option=ORJSON_OPTIONS).decode()) # Windows-specific: avoid aiofile
await project_path.write_text(orjson.dumps(project, option=ORJSON_OPTIONS).decode(), encoding="utf-8")
else:
async with async_open(str(project_path), "w", encoding="utf-8") as f:
await f.write(orjson.dumps(project, option=ORJSON_OPTIONS).decode())
logger.debug(f"Updated starter project {project['name']} file") logger.debug(f"Updated starter project {project['name']} file")