Skip to content

first_light, sandbox, shader_library: read index.json as utf-8-sig (BUILD-2182) - #6467

Open
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/3-index-reader-bom
Open

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/3-index-reader-bom

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Three index readers still opened their index.json as strict utf-8, so an index re-saved by PowerShell 5.1
Set-Content or a Windows editor with a UTF-8 BOM was rejected as invalid JSON:

  • tools/first_light.py:195 — translate() ends the run with Unexpected UTF-8 BOM instead of drawing the pair.
  • tools/sandbox.py:53 — pick_pair() catches the JSONDecodeError as a ValueError and reports "no matching
    pair", hiding a perfectly good pair.
  • tools/shader_library.py:196 — carve_digests() stops the whole library pass with a traceback.

Each read site now uses encoding="utf-8-sig". Writes are unchanged, so files are still written without a BOM.

Tests

Each commit carries its own synthetic BOM test; no game files, no GPU, no built harness.

python -I tests/test_sandbox_pick.py
python -I tests/test_shader_library.py
python -I tests/test_first_light_bom_index.py

Before (tests added, fix not applied):

ERROR: test_index_with_utf8_bom ... json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig)
ERROR: test_carve_index_with_utf8_bom ... json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig)
ERROR: test_translate_reads_an_index_with_a_bom ... json.decoder.JSONDecodeError: Unexpected UTF-8 BOM

After:

Ran 7 tests in 0.046s   OK
Ran 10 tests in 0.110s  OK
Ran 1 test in 0.012s    OK

python -m compileall -q tools/first_light.py tools/sandbox.py tools/shader_library.py is clean. No GPU is needed.

Notes

  • Only the three read sites named in BUILD-2182 change; other BOM sites are covered by their own items and branches.
  • first_light.translate() shells out to three other tools, so its test stubs first_light.run and writes the BOM
    spirv/index.json itself rather than building a shader by hand.
  • Three commits, one per tool, so each is reviewable on its own.

AI-assisted.

pick_pair opened the index as strict utf-8, so an index re-saved by PowerShell or a Windows editor (BOM) raised a JSONDecodeError; the ValueError handler swallowed it and pick_pair reported no matching pair. tests/test_sandbox_pick.py writes the BOM by hand.
… is accepted

carve_digests() opened <folder>/index.json as strict utf-8, so a carve index re-saved with a BOM stopped the whole library pass with a JSONDecodeError instead of naming the carved shaders. tests/test_shader_library.py builds a synthetic carve folder with a BOM index.
translate() opened <work>/first_light/spirv/index.json as strict utf-8, so an index left with a BOM ended the run with 'Unexpected UTF-8 BOM' instead of drawing the pair. The new test stubs the three external tools and writes the BOM index itself.
@AsgerCode

Copy link
Copy Markdown

Tested 3f1a1f3 on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): fail, the first_light read that changes never sees a BOM on a real run, and the first_light reads of the carve index.json that can see one still use strict utf-8

What I ran:

  • python -I tests/test_first_light_bom_index.py: 1 test, OK. tests/test_sandbox_pick.py: 7, OK. tests/test_shader_library.py: 10, OK.
  • Other tests that import these tools: test_first_light.py 46 OK (8 skipped), test_first_light_bad_json.py 9, test_first_light_harness_paths.py 4, test_first_light_mesh_draw.py 3, test_first_light_stale_mesh.py 1, all OK.
  • Negative control (merge base's three tools, new tests): each new test errors (Unexpected UTF-8 BOM for first_light and shader_library; pick_pair returns None for sandbox).
  • Merge with current main: clean (169 commits ahead; main changed tools/shader_library.py in shader_library: tolerate missing program/blob in compute pipelines #6902). On the merged tree the tests above pass, and test_shader_library.py runs 11.

Every JSON read in the three tools:

Read File This PR
sandbox.pick_pair <shaders>/spirv/index.json utf-8-sig
shader_library.carve_digests <carve>/index.json utf-8-sig
first_light.translate (line 195) <work>/first_light/spirv/index.json utf-8-sig
first_light.choose (line 148) <work>/shaders/dxbc/index.json (carve index) still utf-8
first_light.load_json (line 869, --list and --check) carve index, manifest.json still utf-8

The problem:

  • translate() calls reset_run_folders(), which deletes first_light/spirv. Then rdna2spirv.py writes a new spirv/index.json with write_index (encoding="utf-8", no BOM). So line 195 never reads a file that a user saved, and the failure the description gives for translate() cannot occur on a real run. The new test reaches it only because it replaces first_light.run and writes the BOM file itself.
  • The index.json in first_light that a user can re-save is the carve index. Two reads of it stay strict.

Repro: a work folder <tmp>/w with shaders/dxbc/index.json = \ufeff + [{"name":"VS_A","file":"a_vs.dxbc","stage":"vs"},{"name":"PS_B","file":"b_ps.dxbc","stage":"ps"}] and two empty .dxbc files:

  • python -I tools/first_light.py --game <tmp>/g --work <tmp>/w --no-fit --vs VS_A --ps PS_B: exit 1, traceback in choose(): json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0).
  • python -I tools/first_light.py --list --game <tmp>/g --work <tmp>/w: exit 1, the carve index <tmp>\w\shaders\dxbc\index.json is not valid JSON (Unexpected UTF-8 BOM (decode using utf-8-sig) at line 1 column 1).
  • The same two commands with the first_light.py from first_light, sandbox, shader_library: read a carve index.json saved with a UTF-8 BOM #6450: --list exits 0 and prints vs VS_A / ps PS_B. The draw gets past choose() and stops later in translate() at fssm_unpack.py, because my .dxbc files are empty.

Suggested fix: use utf-8-sig in choose() and load_json() too, and test with a BOM in the carve index, not in spirv/index.json. #6450 already makes exactly these changes.

Overlap with other open PRs:

AI-assisted (Claude Code).

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested 3f1a1f32 on Linux, CPython 3.14, CPU only, synthetic inputs, merged onto main dde82f27: pass

  • python -I tests/test_first_light_bom_index.py: Ran 1 test, OK. With only the non-test files reverted to main: FAILED (errors=1), so the new tests exercise the change.
  • python -I tests/test_sandbox_pick.py: Ran 7 tests, OK. With only the non-test files reverted to main: FAILED (errors=1), so the new tests exercise the change.
  • python -I tests/test_shader_library.py: Ran 11 tests, OK. With only the non-test files reverted to main: FAILED (errors=1), so the new tests exercise the change.
  • compileall and scripts/check_no_game_data.py on the changed files: clean.

#6450 makes the same change and also covers first_light.choose() and first_light.load_json(), which this PR leaves reading as utf-8.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting; claimed in #8047.

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.

3 participants