Skip to content

fix(tools): report deeply nested compile commands JSON as a usage error - #13884

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
xXPBSXx:tools/q4-check-compile-recursion
Nov 1, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
xXPBSXx:tools/q4-check-compile-recursion

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What

tools/check_compile.py reads compile_commands.json (or the output of ninja -t compdb) with json.loads in compilation_database, and main turns json.JSONDecodeError, OSError and ValueError into a usage error. A RecursionError is none of these, so a deeply nested file ends in a traceback and exit code 1 instead of the usage error with exit code 2 that the other malformed databases get. This catches RecursionError only around json.loads in compilation_database and raises the existing ValueError from it, so a RecursionError from anywhere else still propagates. A few lines in check_compile.py, plus two tests.

Tested

Python 3.12.10 on Windows and Python 3.12.3 on WSL (Ubuntu 24.04). test_excessively_nested_compilation_databases_are_rejected_without_a_traceback probes a nesting depth from sys.getrecursionlimit() upward until json.loads itself raises RecursionError (and fails if it never does), then checks exit code 2, no Traceback in stderr, and that the "expected an array of command objects" message is not the one shown. test_a_recursion_error_outside_the_database_parser_is_not_a_usage_error makes check() raise RecursionError and expects it to propagate out of main. On both systems each test passed 3 of 3 runs on 44ed68cca1; the first failed 3 of 3 runs with tools/check_compile.py equal to main, the second failed 3 of 3 runs with the except in main widened to RecursionError. After reformatting one line of the second test (48f7b2e), both tests passed once more on both systems and the second test failed once more against the widened except. All 9 tests of CheckCompileDatabaseTests passed on 44ed68cca1. tools/check_conventions.py --base upstream/main passes. Based on main e8c25dd3ba, and it merges with main 73f4f7e06e without a conflict (checked 2026-10-31 13:30 UTC). Not run: Python 3.13 or newer.

Overlap

I took the changed files of each of the 1611 open pull requests from git diff --name-only against its merge base (started 2026-10-31 at 08:20 UTC). Three change the touched files: #12874 and #9547 change tools/check_compile.py, #13239 changes tools/tests/test_check_compile.py. Checked on 2026-10-31 at 09:56 UTC with git merge-tree against main c3c45d7: this head alone merges cleanly, and so do its pairs with each of the three. Checked once more on 2026-10-31 at 11:00 UTC against main 602c0e417c: none of the 82 pull requests opened since 06:30 UTC changes these two files, and the pairs with #12874, #9547 (my own) and #13239 still merge cleanly.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

The one-line exception fixes the malformed-JSON traceback, and the regression test detects the bug on the base revision.

What I ran

Reviewed head 820e43ad7e68d35b048eefdf736825b1e4b61292. The focused checks used base 8f55414024c614b32548254e59e80bd3f0085231; a fresh merge preview against current GitHub main 4e2c5a72e6ad77abb8d001083439a0e9da9841f0 was clean. Environment: Linux x86_64, Python 3.11.2.

python3 -m py_compile tools/check_compile.py
python3 -m unittest tools.tests.test_check_compile
python3 tools/check_test_registration.py --scripts
git diff --check 8f55414024c614b32548254e59e80bd3f0085231...820e43ad7e68d35b048eefdf736825b1e4b61292
python3 tools/check_conventions.py --base 8f55414024c614b32548254e59e80bd3f0085231 --head 820e43ad7e68d35b048eefdf736825b1e4b61292 --title 'fix(tools): report deeply nested compile commands JSON as a usage error' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/pr-body.md

Results

Check Result
Python syntax Passed
tools.tests.test_check_compile Passed: 62 tests, 9 skipped
Script test registration Passed: 230 test scripts registered
Diff and conventions Passed
Merge preview Clean against current main 4e2c5a72e6ad77abb8d001083439a0e9da9841f0
Hosted checks Conventions and intake passed; labels and eligibility were queued at the final status check
Unedited unit-test output
............s...s..................s.......s..s...s..s...ss...
----------------------------------------------------------------------
Ran 62 tests in 9.652s

OK (skipped=9)

Negative control

Assertion Change to code under test Outcome
Deeply nested JSON exits with usage status 2 and no traceback Ran the new test against the base version without the RecursionError handler Failed as expected: the base exits 1 with a RecursionError traceback
Unedited negative-control output
F...........s...s..................s.......s..s...s..s...ss...
======================================================================
FAIL: test_excessively_nested_compilation_databases_are_rejected_without_a_traceback (tools.tests.test_check_compile.CheckCompileDatabaseTests.test_excessively_nested_compilation_databases_are_rejected_without_a_traceback)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/negative-control-base-57441e7e/tools/tests/test_check_compile.py", line 510, in test_excessively_nested_compilation_databases_are_rejected_without_a_traceback
    self.assertEqual(result.returncode, 2, result.stdout + result.stderr)
AssertionError: 1 != 2 : Traceback (most recent call last):
  File "/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/negative-control-base-57441e7e/tools/check_compile.py", line 432, in <module>
    sys.exit(main())
             ^^^^^^
  File "/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/negative-control-base-57441e7e/tools/check_compile.py", line 418, in main
    checked, failed = check(root, args.build, files, args.jobs)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/negative-control-base-57441e7e/tools/check_compile.py", line 368, in check
    commands = index(build, root)
               ^^^^^^^^^^^^^^^^^^
  File "/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/negative-control-base-57441e7e/tools/check_compile.py", line 130, in index
    for entry in entries(build):
                 ^^^^^^^^^^^^^^
  File "/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/negative-control-base-57441e7e/tools/check_compile.py", line 106, in entries
    return compilation_database(manifest.read_text(encoding="utf-8-sig"), manifest)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-820e43ad7e68/negative-control-base-57441e7e/tools/check_compile.py", line 73, in compilation_database
    database = json.loads(text)
               ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while decoding a JSON array from a unicode string


----------------------------------------------------------------------
Ran 62 tests in 10.328s

FAILED (failures=1, skipped=9)

Sources

Not verified

The author reports Windows and WSL runs, which I did not independently reproduce. The GitHub eligibility and label checks had not completed at review time. No full AnyPS5 build was needed or run for this Python-only change.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@xXPBSXx Reviewed at 820e43ad7e. The new nested-JSON test passes, and it fails against the main version of the tool. Please narrow the new RecursionError handling to json.loads inside compilation_database, converting that decoder failure to the existing usage-error path.

The current catch covers repository discovery, changed-file discovery and all of check(). In a direct probe, a RecursionError raised by check() propagates on main but becomes SystemExit(2) with a usage message on this head. That would misclassify an internal recursion bug as invalid input. Please keep the deep-database regression and add a check that an unrelated recursion failure is not absorbed by the parser-error handler.

…N is decoded

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked — new head, looks good

The narrower parser handler fixes the earlier concern, and the added regression confirms unrelated recursion errors still propagate.

What I ran

Reviewed head 48f7b2e9334d2f4e48bc1d32a048256dc23c0c8c against base 8f55414024c614b32548254e59e80bd3f0085231; the merge preview is clean and both SHAs remained unchanged through the precheck. Environment: Linux x86_64, Python 3.11.2.

python3 -m py_compile tools/check_compile.py
python3 -m unittest tools.tests.test_check_compile
python3 tools/check_test_registration.py --scripts
python3 tools/check_conventions.py --base 8f55414024c614b32548254e59e80bd3f0085231 --head 48f7b2e9334d2f4e48bc1d32a048256dc23c0c8c --title 'fix(tools): report deeply nested compile commands JSON as a usage error' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13884-48f7b2e9334d/pr-body.md

Results

Check Result
Python syntax Passed
tools.tests.test_check_compile Passed: 63 tests, 9 skipped
Script test registration Passed: 230 scripts registered
Diff and conventions Passed
Negative control on base Passed as a control: the new deep-JSON regression fails on base with the original RecursionError traceback
Test-quality helper Unavailable in the isolated checkout; this did not affect the test run
Hosted CI Ubuntu relinker, ASan relinker, eligibility, conventions, and labels passed; Windows/Linux product jobs were skipped; Windows relinker and two macOS relinker jobs were pending at the latest status check
Unedited focused test output
.............s...s..................s.......s..s...s..s...ss...
----------------------------------------------------------------------
Ran 63 tests in 10.109s

OK (skipped=9)

Findings

Blocking

None found.

Non-blocking

The test-quality helper could not run because tools/pr_test_quality.py was absent from the isolated checkout. Windows/Linux product jobs were skipped; they are not needed to validate this Python-only change.

Sources

@xXPBSXx

xXPBSXx commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, you are right: the except in main also covered repository discovery, changed-file discovery and all of check(), so an unrelated RecursionError there would have been reported as a usage error. New head 48f7b2e: the RecursionError is now caught only around json.loads in compilation_database, which raises the existing ValueError ("compilation database is nested too deeply"), and the except in main is back to what it is on main. The deep-database test is kept, and a new test, test_a_recursion_error_outside_the_database_parser_is_not_a_usage_error, makes check() raise RecursionError and expects it to propagate out of main.

On Windows (Python 3.12.10) and on WSL (Python 3.12.3), both tests passed 3 of 3 runs on 44ed68cca1, the deep-database test failed 3 of 3 runs with tools/check_compile.py as on main, and the new test failed 3 of 3 runs against the previous head of this PR (820e43a). After reformatting one line of the new test (48f7b2e), both tests passed once more on both systems and the new test failed once more against the widened except; all 9 tests of CheckCompileDatabaseTests passed on both systems on 44ed68cca1. The final head 48f7b2e merges cleanly with main 73f4f7e06e, alone and in pairs with #12874, #9547 and #13239 (checked 2026-10-31 at 13:30 UTC).

AI-assisted (Claude Code).

@adriadam10 adriadam10 self-assigned this Nov 1, 2026
@adriadam10
adriadam10 merged commit 2954b52 into boykopovar:main Nov 1, 2026
14 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the narrowed RecursionError handling around json.loads in compilation_database (it now raises the existing ValueError, exit 2), and that both new tests pin it, including propagation of an unrelated RecursionError; merged.

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.

4 participants