Skip to content

fix: handle empty or undefined connections arrays without crashing#658

Open
holistis wants to merge 5 commits into
tscircuit:mainfrom
holistis:fix/empty-connections-crash
Open

fix: handle empty or undefined connections arrays without crashing#658
holistis wants to merge 5 commits into
tscircuit:mainfrom
holistis:fix/empty-connections-crash

Conversation

@holistis

Copy link
Copy Markdown

Summary

Fixes #657. Solvers threw TypeError: ... is not iterable or Error: Unexpected numItems value: 0 when chips, directConnections, or netConnections were empty or undefined.

  • Normalize chips, directConnections, and netConnections to [] in cloneAndCorrectInputProblem — the single pipeline entry point, so all downstream solvers receive valid arrays
  • Add ?? [] fallbacks in getConnectivityMapsFromInputProblem as defense-in-depth (this function can also be called independently)
  • Guard ChipObstacleSpatialIndex against chips.length === 0 — Flatbush throws when constructed with 0 items; skip index construction and return early
  • Type spatialIndex as Flatbush | null (was Flatbush) and guard getChipsInBounds against null

Test plan

  • bun test tests/repros/repro-empty-connections.test.ts — 3 new regression tests (empty arrays, undefined connections, all fields undefined)
  • bun test tests/examples/ — 50 existing example tests still pass
  • bun test tests/repros/ — all existing repro tests still pass
  • bunx tsc --noEmit — no type errors

@vercel

vercel Bot commented Jul 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
schematic-trace-solver Ready Ready Preview, Comment Jul 11, 2026 11:45pm

Request Review

Solvers crashed with TypeError when chips, directConnections, or
netConnections were empty or undefined.

- Normalize all three arrays to [] in cloneAndCorrectInputProblem (pipeline entry point)
- Add ?? [] fallbacks in getConnectivityMapsFromInputProblem (defense-in-depth)
- Guard ChipObstacleSpatialIndex against chips.length === 0 (Flatbush requires gt 0 items)
- Type spatialIndex as Flatbush | null; guard getChipsInBounds against null index

Fixes tscircuit#657
holistis and others added 2 commits July 12, 2026 00:11
…tions

Add ?? [] to all four for-loops in MspConnectionPairSolver constructor
so the solver is safe when called directly outside the pipeline.
Add availableNetLabelOrientations ??= {} in cloneAndCorrectInputProblem
to prevent TypeError in label solvers when field is omitted.
Extend regression tests: direct MspConnectionPairSolver test and
missing availableNetLabelOrientations test (5 tests total, all pass).
…blem

Make chips, directConnections, netConnections, and availableNetLabelOrientations
optional in the InputProblem interface so callers that omit them get a compile-
time safe type, not a runtime crash.

Add ?? [] / ?? {} fallback guards in every sub-solver that iterates or indexes
these fields directly (AvailableNetOrientationSolver, NetLabelPlacementSolver,
SchematicTraceSingleLineSolver, SchematicTraceSingleLineSolver2,
visualizeInputProblem, TraceAnchoredNetLabelOverlapSolver, candidates,
GuidelinesSolver, correctPinsInsideChip, expandChipsToFitPins, and more).

Add tests/solvers/SchematicTracePipelineSolver/empty-connections.test.ts
covering empty arrays and undefined (cast as any) for both connections fields.

All 109 tests pass; bunx tsc --noEmit reports 0 errors.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

[BUG] Router crashes on empty connections array

1 participant