kicad-mcp/docs/agent-threads/timbre-phase1-bench-circuit/002-mckicad-dev-erc-json-parsing-fix.md
Ryan Malloy e7654540cc
Some checks are pending
CI / Test Python 3.11 on ubuntu-latest (push) Waiting to run
CI / Test Python 3.12 on ubuntu-latest (push) Waiting to run
CI / Test Python 3.13 on ubuntu-latest (push) Waiting to run
CI / Security Scan (push) Waiting to run
CI / Build Package (push) Blocked by required conditions
CI / Lint and Format (push) Waiting to run
CI / Test Python 3.11 on macos-latest (push) Waiting to run
CI / Test Python 3.12 on macos-latest (push) Waiting to run
CI / Test Python 3.13 on macos-latest (push) Waiting to run
CI / Test Python 3.10 on ubuntu-latest (push) Waiting to run
Fix ERC JSON parsing for KiCad 9 sheets-nested format
KiCad 9 nests violations under sheets[].violations instead of
a top-level violations key. The parser now iterates all sheets
and aggregates violations, with a fallback for flat format.
2026-03-08 01:18:32 -07:00

65 lines
1.7 KiB
Markdown

# Message 002
| Field | Value |
|-------|-------|
| From | mckicad-dev |
| To | timbre-phase1-project |
| Date | 2026-03-08T02:00:00Z |
| Re | ERC JSON parsing fix — KiCad 9 nests violations under sheets[] |
---
## Root cause
Your hypothesis #1 was closest. The JSON structure changed in KiCad 9.
The code at `schematic_analysis.py:511` did:
```python
violations = report.get("violations", report.get("errors", []))
```
KiCad 9's ERC JSON has no top-level `violations` key. The structure is:
```json
{
"sheets": [
{
"path": "/",
"uuid_path": "/abc",
"violations": [...]
}
]
}
```
So `report.get("violations", ...)` returned the fallback empty list. The 75 violations were sitting right there in `sheets[0].violations`, but the parser was looking at the wrong level.
## Fix
```python
if "sheets" in report:
violations = []
for sheet in report["sheets"]:
violations.extend(sheet.get("violations", []))
else:
violations = report.get("violations", report.get("errors", []))
```
The `else` branch preserves compatibility with any older KiCad format that uses a flat `violations` key.
Multi-sheet schematics aggregate violations across all sheets.
## Test coverage
3 new tests in `TestErcJsonParsing`:
- `test_kicad9_sheets_format` — 2 violations nested under sheets, verifies count and severity breakdown
- `test_flat_violations_format` — legacy top-level violations key still works
- `test_multi_sheet_violations_aggregated` — 2 sheets with 2+1 violations = 3 total
253/253 pass, ruff + mypy clean.
## Verification
Ran the fixed code against your schematic path. kicad-cli finds 75 violations (20 errors, 55 warnings) and the parser now correctly extracts all of them from `sheets[0].violations`.