Skip to content

Make scripts/check.py run on Windows: bootstrap uv there and generate the same bytes on every OS - #88

Open
Ahmar004 wants to merge 5 commits into
prooflinedev:mainfrom
Ahmar004:fix/windows-gate
Open

Ahmar004 wants to merge 5 commits into
prooflinedev:mainfrom
Ahmar004:fix/windows-gate

Conversation

@Ahmar004

Copy link
Copy Markdown

Fixes #87

The problem in one sentence

python scripts/check.py, the check the README asks contributors to run, can't run on Windows. It has no Windows build of its pinned uv tool. Its "is the generated code up to date?" step also fails there, because on Windows the generator writes § as § and uses Windows line endings.

Why it happens, step by step

  1. No Windows uv. scripts/_uv.py pins checksums only for the macOS/Linux .tar.gz archives. On Windows it stops with Install uv 0.13.7 to run the gate on this bootstrap platform.
  2. Wrong text encoding. scripts/generate.py:88 reads openapi.public.json with read_text() and no encoding. On Windows, Python then uses the legacy code page (cp1252), not UTF-8. The two UTF-8 bytes of § become the two characters §, which land in a generated docstring (models/customer_review_detail.py).
  3. Windows line endings. A line ending is the invisible character that ends each line: LF on Linux and macOS, CRLF on Windows. On Windows, Python's text writes turn LF into CRLF, and ruff format keeps the ending it finds. So the generator's output is CRLF there.
  4. Byte-for-byte comparison. generate.py --check compares the output with the committed proofline/_generated byte for byte. The repo has no .gitattributes, so Git for Windows (default core.autocrlf=true) checks the LF files out as CRLF. The line endings match only by accident, the § file always differs, and the step fails with The generated Python API differs; run scripts/generate.py.

I first thought the cause was only the CRLF checkout. A clone with LF files still failed, and comparing the two outputs showed the encoding bug.

Who is affected

Windows contributors without WSL. They can't run the gate. If they run scripts/generate.py after an API change, they would commit § mojibake and CRLF files. Linux, macOS and CI are not affected today, and they behave the same after this PR.

What this PR changes

  • scripts/_uv.py: adds the checksums of the two official Windows archives (x86_64-pc-windows-msvc, aarch64-pc-windows-msvc), copied from the .sha256 files of the uv 0.13.7 release; I recomputed them from the downloaded zips and they match. Two small helpers:

    • release_target(system, machine) maps Windows and the AMD64/ARM64 names Windows reports.
    • unpack(content, target) reads uv.exe from the zip, or uv-<target>/uv from the tarball, exactly as before.

    The download, the checksum checks (before saving and again before use) and the version check are unchanged. On Windows the archive is .zip and the saved binary ends in .exe.

  • scripts/generate.py: every text read and write now says encoding="utf-8". ruff format runs with --config "format.line-ending = 'lf'", so the generated files are LF on every OS. --check compares text files with CRLF turned into LF (files with a NUL byte stay byte-exact), so a clone made before .gitattributes existed also passes. Git stores LF anyway, so this hides no real drift.

  • scripts/contracts.py: reads the same document and the Python sources as UTF-8 (the same latent bug; it passes today only because it reads IDs, not descriptions).

  • .gitattributes: * text=auto eol=lf. Windows clones now check out LF, like CI. No file in the repo is stored with CRLF, and Git still detects binaries itself (text=auto).

Why not the obvious alternatives

  • Only ignore CRLF in the --check comparison: not enough on its own. generate.py would still write mojibake on Windows. So the comparison ignores line endings (for old CRLF clones), and the generator itself is also fixed and tested.
  • Set PYTHONUTF8=1 in check.py: this fixes only the gate. People running scripts/generate.py directly would still get §. Explicit encodings also match what the script already tells the generator library (file_encoding="utf-8", generate.py:133).
  • "Install uv 0.13.7 yourself" on Windows: the gate pins and verifies uv on purpose, as a supply-chain check. Windows should get the same check.

How I tested it (Windows 11, Python 3.13.12, Git for Windows with core.autocrlf=true)

Before (upstream main 48c4489):

Step Result
py scripts/check.py (only winget's uv 0.13.6 on PATH) Install uv 0.13.7 to run the gate on this bootstrap platform., exit 1
same, with uv 0.13.7 on PATH The generated Python API differs; run scripts/generate.py., exit 1
fresh output vs. the normal (CRLF) clone 1 of 212 files differs: (§55) vs (§55)
fresh output vs. an LF clone 211 of 212 differ: 210 by line endings only, plus the § file

New tests, written first, RED on main:

  • tests/test_generate.py::test_generation_does_not_depend_on_the_locale_encoding runs generate.py --check with -X utf8=0, LC_ALL=C and PYTHONCOERCECLOCALE=0. That forces a non-UTF-8 default encoding: ASCII on Linux, cp1252 on Windows, so CI catches a regression too. On main it fails with "differs".
  • tests/test_uv.py: the four platform mappings have a pinned archive, uv.exe is read from a Windows zip, and the Unix tarball path still works. On main they fail (no Windows target, no unpack).

Mutation check: I removed encoding="utf-8" from generate.py:88, and separately removed the line-ending = 'lf' option. Each makes the new test fail, and the file was restored byte for byte.

After (this branch):

  • New tests: 14 passed (after the 4 Proofline rounds). Full gate at 4913069: exit 0, 233 tests.
  • A fresh clone with core.autocrlf=true: all 255 text files check out as LF, and py scripts/check.py exits 0 on the first run. It downloads uv-x86_64-pc-windows-msvc.zip, verifies the checksum and extracts uv-0.13.7-x86_64-pc-windows-msvc.exe. Then generation, contracts, Ruff, format, mypy, 226 tests, the wheel and sdist builds and both install smoke tests all pass: Python SDK generation, HTTP contracts, wheel and source installs passed.
  • Linux and macOS: the tarball path is the same code, moved into unpack() and covered by test_unix_archive_still_yields_its_uv_executable. The fork's Ubuntu CI runs the full gate, including the new tests.

Known, separate: in about 2 of 5 full Windows runs, one existing test fails: tests/test_http.py::test_body_stream_deadline_does_not_turn_authority_into_timeout[403-sync]. It fails with ProoflineError: Proofline request failed (HTTP 0) after a Windows ConnectionResetError (WinError 10054). That's about 1 in 5 when run alone. This PR doesn't touch the test or the code it tests. I'll look at it separately; it may be a real Windows behaviour of the sync client.

Existing Windows clones (CRLF files from before .gitattributes) pass too. I converted all 212 generated files to CRLF and generate.py --check exited 0.

Linux: the fork's Ubuntu CI (check) passes the full gate with the new tests.


Proofline review log

Fork review PR: Ahmar004#1. 4 rounds (the cap): 12 findings, 8 fixed, 4 disagreed (with reasons below). CI green on every round. A 5th review starts automatically on 4913069; it isn't answered (cap).

Round Finding What I made of it Outcome
1 --check still fails in a clone made before .gitattributes (CRLF files) Correct: .gitattributes doesn't rewrite existing files. Git stores LF anyway, so comparing with line endings normalized hides nothing. Fixed in 19d891f: text compared CRLF→LF, binary exact; 2 new tests; 212-file CRLF copy passes
1 Bootstrap tests don't run executable(), so a .tar.gz/no-.exe mistake would pass Correct. Fixed in 19d891f: executable() tested for both Windows targets with a fake download; mutation-checked
2 Normalizing every NUL-free file could hide drift in a binary without NUL The generator writes only text (211 .py + LICENSE); a NUL byte is Git's own binary test; an allow-list would need syncing with the templates. Disagreed, left as is
2 CRLF test calls text(), not files() Correct. Fixed in c7c8fe2: compares files() on LF vs CRLF folders (+ binary stays exact)
2 Ubuntu CI can't notice removal of the LF option Correct: Linux writes LF anyway. Fixed in c7c8fe2: format_python() helper, test feeds it a CRLF file
2 No test that a wrong-checksum Windows zip is refused Correct: the checksum is the security boundary. Fixed in c7c8fe2: download (never saved) and cache (never extracted) cases
3 Generation could skip format_python() and tests would still pass Only a Windows working copy would get CRLF; .gitattributes makes Git store LF, so it can't reach the repo; catching it on Linux needs faked text writes. Disagreed, left as is
3 Windows test doesn't check that the version check sees the installed exe Correct, two lines. Fixed in 3ededd9: matching() called once with the returned uv.exe
3 Cached-checksum test doesn't prove nothing is unpacked first Correct, one line. Fixed in 3ededd9: unpack fails the test if called
4 A CRLF LICENSE template could give a CRLF LICENSE without failing --check Same as round 3: only a Windows working copy; Git stores LF. Disagreed, left as is
4 Windows tests always pass the version check, so ignoring its result would pass Correct. Fixed in 4913069: wrong version → SystemExit; mutation-checked
4 Windows checksum pins aren't checked against the release A test repeating the digests would change together with them. Checked independently: equal to the release's .sha256 files and my own hashes; the real bootstrap accepted the official zip. Disagreed, left as is

On Windows the gate could not start: scripts/_uv.py pinned uv only for
the macOS and Linux tarballs, so it exited with "Install uv 0.13.7 to
run the gate on this bootstrap platform."

With uv installed by hand, generate.py --check still failed ("The
generated Python API differs"). The generator read openapi.public.json
with read_text() and no encoding, so on Windows Python used the legacy
code page (cp1252) and the UTF-8 "§" became "§" in a docstring. Its
text writes also used Windows line endings (CRLF), which ruff format
keeps. The committed files only matched because Git for Windows checks
them out as CRLF too.

Now:
- _uv.py pins the official x86_64 and aarch64 Windows zips of uv 0.13.7
  and reads uv.exe from them, with the same checksum and version checks.
  release_target() and unpack() hold the platform mapping and archive
  reading; the tarball path is unchanged.
- generate.py and contracts.py read and write text as UTF-8, and the
  generator's ruff format pass uses LF line endings.
- .gitattributes (* text=auto eol=lf) makes Windows clones check out LF
  files, like CI.

Tests: tests/test_generate.py runs generate.py --check under a
non-UTF-8 default encoding (ASCII on Linux, cp1252 on Windows), and
tests/test_uv.py covers the platform mapping and both archive types.
Both fail on main and pass now. On Windows, a fresh clone with
core.autocrlf=true now passes the whole gate.
…s bootstrap

Proofline review, round 1:

- .gitattributes only changes how files are checked out from now on. A
  Windows clone made before it keeps CRLF files, so --check still said
  "differs" there, although Git stores the same LF content. files() now
  compares text with CRLF turned into LF (files containing a NUL byte,
  i.e. binary files, stay byte-exact). Line-ending drift can't be
  committed anyway: .gitattributes makes Git store LF.
- Because the check now ignores line endings, a new test checks the
  generator's own output for CRLF, so the LF option stays covered.
- test_uv.py only tested the helpers, so executable() could have asked
  for a .tar.gz or saved a file without .exe on Windows and still passed.
  A new test runs executable() for both Windows targets with a fake
  download and checks the .zip URL, the .exe path and the bytes.

Each of the four guarded lines was mutated and made its test fail.
Proofline review, round 2:

- The CRLF test called text() directly, so files() could stop using it
  and the test would still pass. It now compares files() on an LF folder
  and a CRLF folder, and checks that a file with a NUL byte is still
  compared byte for byte.
- The LF test ran the whole generator, but on Linux the generator already
  writes LF, so CI could not notice if the LF option were removed. The
  ruff format step is now format_python(), and the test feeds it a CRLF
  file, so it fails on any OS without the option.
- Nothing checked that a Windows zip with a wrong checksum is refused.
  A new test covers a fresh download (refused and never saved) and a
  cached archive (refused and never extracted).

Each guarded line was mutated and made its test fail.
…rap tests

Proofline review, round 3:

- The Windows bootstrap test accepted any version check. It now records
  which executable matching() was asked about and expects exactly the
  returned uv.exe.
- The wrong-checksum test now fails if unpack() runs, so an archive
  can't be unpacked before its checksum is checked.

Both were mutation-checked: skipping the version check, or unpacking a
cached archive before verifying it, now fails a test.
Proofline review, round 4: the Windows tests always made the version
check succeed, so executable() could ignore its result and still pass.
A new test serves a zip with a valid checksum, makes the version check
fail, and expects "The verified uv executable has an unexpected
version." Ignoring the result (mutation) now fails the test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scripts/check.py can't run on Windows, and generate.py corrupts non-ASCII text there

1 participant