From e405fa20e99cbfb91b2e5fb716dd7c1657df08e6 Mon Sep 17 00:00:00 2001 From: PerryLink Date: Mon, 21 Sep 2026 21:58:10 +0800 Subject: [PATCH] fix: make the schema codegen host-independent The generated modules are checked in, and `make gen-check` asserts that regenerating them reproduces the committed artifact. Two host dependencies broke that: - `gen_schema.py:179` and `gen_meta.py:59` interpolate a `Path` into the generated header, so the path uses `os.sep` and a non-POSIX host emits `schema\schema.json` where the committed artifact says `schema/schema.json`. - both generators run ruff via `subprocess.run(..., text=True)` without an encoding, so the child's UTF-8 output is decoded with `locale.getpreferredencoding(False)`. When ruff fails, the decode fails first and the message becomes "stream did not contain valid UTF-8", discarding the actual reason -- and hiding the header mismatch, since that error is raised before the artifact comparison. `as_posix()` emits `/` on every host, and `encoding="utf-8"` is what both files already pass to every read_text/write_text they perform. tests/test_gen_all.py goes from 1 failed / 20 passed to 21 passed; reverting only these two scripts restores the failure. --- scripts/gen_meta.py | 3 ++- scripts/gen_schema.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/gen_meta.py b/scripts/gen_meta.py index e871856..b0e31cd 100644 --- a/scripts/gen_meta.py +++ b/scripts/gen_meta.py @@ -56,7 +56,7 @@ def render_meta(*, protocol_version: int = 1) -> str: client_methods = data.get("clientMethods", {}) protocol_methods = data.get("protocolMethods") version = data.get("version", 1) - header_lines = [f"# Generated from {meta_json.relative_to(ROOT)}. Do not edit by hand."] + header_lines = [f"# Generated from {meta_json.relative_to(ROOT).as_posix()}. Do not edit by hand."] if version_file.exists(): ref = version_file.read_text("utf-8").strip() if ref: @@ -74,6 +74,7 @@ def render_meta(*, protocol_version: int = 1) -> str: [sys.executable, "-m", "ruff", "format", "--stdin-filename", str(out_py), "-"], input=source, text=True, + encoding="utf-8", capture_output=True, check=False, cwd=ROOT, diff --git a/scripts/gen_schema.py b/scripts/gen_schema.py index dc1c4d0..6c9f0ab 100644 --- a/scripts/gen_schema.py +++ b/scripts/gen_schema.py @@ -176,7 +176,7 @@ def _deserialize_field_specs(definition: dict[str, Any]) -> tuple[list[str], lis def _build_header(schema_json: Path, version_file: Path) -> str: - lines = [f"# Generated from {schema_json.relative_to(ROOT)}. Do not edit by hand."] + lines = [f"# Generated from {schema_json.relative_to(ROOT).as_posix()}. Do not edit by hand."] if version_file.exists() and (ref := version_file.read_text(encoding="utf-8").strip()): lines.append(f"# Schema ref: {ref}") return "\n".join(lines) @@ -192,6 +192,7 @@ def _format_python(source: str, schema_out: Path) -> str: [sys.executable, "-m", "ruff", *arguments, "--stdin-filename", str(schema_out), "-"], input=source, text=True, + encoding="utf-8", capture_output=True, check=False, cwd=ROOT,