Skip to content

Commit e405fa2

Browse files
committed
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.
1 parent 9d07d78 commit e405fa2

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

‎scripts/gen_meta.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def render_meta(*, protocol_version: int = 1) -> str:
5656
client_methods = data.get("clientMethods", {})
5757
protocol_methods = data.get("protocolMethods")
5858
version = data.get("version", 1)
59-
header_lines = [f"# Generated from {meta_json.relative_to(ROOT)}. Do not edit by hand."]
59+
header_lines = [f"# Generated from {meta_json.relative_to(ROOT).as_posix()}. Do not edit by hand."]
6060
if version_file.exists():
6161
ref = version_file.read_text("utf-8").strip()
6262
if ref:
@@ -74,6 +74,7 @@ def render_meta(*, protocol_version: int = 1) -> str:
7474
[sys.executable, "-m", "ruff", "format", "--stdin-filename", str(out_py), "-"],
7575
input=source,
7676
text=True,
77+
encoding="utf-8",
7778
capture_output=True,
7879
check=False,
7980
cwd=ROOT,

‎scripts/gen_schema.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _deserialize_field_specs(definition: dict[str, Any]) -> tuple[list[str], lis
176176

177177

178178
def _build_header(schema_json: Path, version_file: Path) -> str:
179-
lines = [f"# Generated from {schema_json.relative_to(ROOT)}. Do not edit by hand."]
179+
lines = [f"# Generated from {schema_json.relative_to(ROOT).as_posix()}. Do not edit by hand."]
180180
if version_file.exists() and (ref := version_file.read_text(encoding="utf-8").strip()):
181181
lines.append(f"# Schema ref: {ref}")
182182
return "\n".join(lines)
@@ -192,6 +192,7 @@ def _format_python(source: str, schema_out: Path) -> str:
192192
[sys.executable, "-m", "ruff", *arguments, "--stdin-filename", str(schema_out), "-"],
193193
input=source,
194194
text=True,
195+
encoding="utf-8",
195196
capture_output=True,
196197
check=False,
197198
cwd=ROOT,

0 commit comments

Comments
 (0)