spirv_asm: validate --out upfront, accept UTF-8 BOM, and write atomically #2264

Merged
kirigayakazima merged 1 commit from upstream-pr/2264 into main 2026-11-28 15:06:22 +00:00
kirigayakazima commented 2026-11-26 11:44:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-26T11:44:47Z.


Claimed in the coordination issue as: SPIRV-ASM-CLI-BOM-DIR-GUARD-AND-ATOMIC-WRITE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191092674)

What and why

tools/spirv_asm.py previously:

  • Read the .spvasm input before validating --out, causing full assembly before rejecting empty paths, directories, or missing parent directories.
  • Opened .spvasm with "utf-8" instead of "utf-8-sig", rejecting UTF-8 BOM inputs with unknown opcode '\ufeff...'.
  • Wrote binary .spv output directly in place with open(args.out, "wb"), risking truncated or corrupted existing outputs if a write failed partway.

This adds _check_out_path upfront in main(), reads source files with utf-8-sig (keeping newline=""), publishes assembled SPIR-V atomically with tempfile + file_mode.published_mode + replace_retry.replace, and adds unit/CLI tests covering all three behaviors.

Tested

  • python -I tests/test_spirv_asm.py -v: 41 tests run (40 passed; 1 skipped: spirv-as not installed)
  • Detached upstream/main baseline negative control: failed on the new CLI tests before the fix
  • python -I tests/test_ci_registration.py (16 passed), python -I tests/test_text_write_line_endings.py (3 passed), python -I scripts/check_no_game_data.py, python -I -m compileall -q, and git diff --cached --check

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5509421862 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2264). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-26T11:44:47Z. --- Claimed in the coordination issue as: SPIRV-ASM-CLI-BOM-DIR-GUARD-AND-ATOMIC-WRITE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191092674) ### What and why `tools/spirv_asm.py` previously: - Read the `.spvasm` input before validating `--out`, causing full assembly before rejecting empty paths, directories, or missing parent directories. - Opened `.spvasm` with `"utf-8"` instead of `"utf-8-sig"`, rejecting UTF-8 BOM inputs with `unknown opcode '\ufeff...'`. - Wrote binary `.spv` output directly in place with `open(args.out, "wb")`, risking truncated or corrupted existing outputs if a write failed partway. This adds `_check_out_path` upfront in `main()`, reads source files with `utf-8-sig` (keeping `newline=""`), publishes assembled SPIR-V atomically with `tempfile` + `file_mode.published_mode` + `replace_retry.replace`, and adds unit/CLI tests covering all three behaviors. ### Tested - `python -I tests/test_spirv_asm.py -v`: 41 tests run (40 passed; 1 skipped: `spirv-as not installed`) - Detached `upstream/main` baseline negative control: failed on the new CLI tests before the fix - `python -I tests/test_ci_registration.py` (16 passed), `python -I tests/test_text_write_line_endings.py` (3 passed), `python -I scripts/check_no_game_data.py`, `python -I -m compileall -q`, and `git diff --cached --check` GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
real-rusher commented 2026-11-26 12:23:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by real-rusher on 2026-11-26T12:23:34Z.


Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_asm.py: 41 tests OK. The PR's test file against main's tool: 2 failures and 1 error, so the new tests fail on main and pass with the change. compileall clean.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7191941971 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2264#issuecomment-7191941971). Originally posted by [real-rusher](https://github.com/real-rusher) on 2026-11-26T12:23:34Z. --- Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_asm.py: 41 tests OK. The PR's test file against main's tool: 2 failures and 1 error, so the new tests fail on main and pass with the change. compileall clean. AI-assisted: yes (Claude Code)
tiguan26 commented 2026-11-27 04:37:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:37:08Z.

Review: APPROVED


Approving 9569d1806.

--out is checked (not empty, not a directory, parent exists) before the source is read, the source is read BOM-tolerant, and the module is published through a temp file and replace_retry.

Merged onto main d1dff7949 in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_spirv_asm.py: 41 passed (main: 38). AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083542156 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2264#pullrequestreview-6083542156). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T04:37:08Z. **Review:** APPROVED --- Approving 9569d1806. `--out` is checked (not empty, not a directory, parent exists) before the source is read, the source is read BOM-tolerant, and the module is published through a temp file and `replace_retry`. Merged onto `main` `d1dff7949` in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_spirv_asm.py`: 41 passed (main: 38). AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 10:35:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T10:35:12Z.


#2264 | head 9569d18065 (merged cleanly with main 6c7f79343) | python -I tests/test_spirv_asm.py | red on main: FAILED (failures=2, errors=1, skipped=2) (unknown opcode '\ufeffOpCapability' on UTF-8 BOM input, assemble running before --out validation, and non-atomic direct output write) | green on PR+main: 41 tests OK (skipped=2) | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited _check_out_path (upfront empty/directory/missing-parent checks before reading or assembling inputs), utf-8-sig source decoding, and _write_atomic_bytes (NamedTemporaryFile + published_mode + replace_retry.replace with finally temp cleanup) in tools/spirv_asm.py. Also passed tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), tests/test_runner_calls_all_tests.py (3 OK), and scripts/check_no_game_data.py.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237992477 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2264#issuecomment-7237992477). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T10:35:12Z. --- #2264 | head 9569d180657b0e50a25eaec9ecca17969d086af0 (merged cleanly with main 6c7f79343) | `python -I tests/test_spirv_asm.py` | red on main: `FAILED (failures=2, errors=1, skipped=2)` (`unknown opcode '\ufeffOpCapability'` on UTF-8 BOM input, `assemble` running before `--out` validation, and non-atomic direct output write) | green on PR+main: `41 tests OK (skipped=2)` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `_check_out_path` (upfront empty/directory/missing-parent checks before reading or assembling inputs), `utf-8-sig` source decoding, and `_write_atomic_bytes` (`NamedTemporaryFile` + `published_mode` + `replace_retry.replace` with `finally` temp cleanup) in `tools/spirv_asm.py`. Also passed `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 15:06:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T15:06:18Z.


Red/green: pytest tests/test_spirv_asm.py fails on main (3 failed, 38 passed, 1 skipped, 250 subtests passed) and passes on head 9569d18065 merged with main (41 passed, 1 skipped, 250 subtests passed).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7241203467 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2264#issuecomment-7241203467). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T15:06:18Z. --- Red/green: `pytest tests/test_spirv_asm.py` fails on main (3 failed, 38 passed, 1 skipped, 250 subtests passed) and passes on head 9569d180657b merged with main (41 passed, 1 skipped, 250 subtests passed). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2264
No description provided.