Repository navigation
Description
netspec doctor is the one subcommand whose stated purpose is "check that a KiCad engine can be found and run". It is also the only one that crashes when the answer is no.
Reproduction
kicad-netspec 0.7.0, installed with uv tool install kicad-netspec, on Debian 13 / Python 3.12. Run with a PATH that has no kicad-cli and no Flatpak:
$ mkdir -p /tmp/m/bin && ln -s "$(command -v python3)" /tmp/m/bin/python3 \
&& ln -s "$(command -v netspec)" /tmp/m/bin/netspec
$ PATH=/tmp/m/bin netspec doctor
Traceback (most recent call last):
File ".../bin/netspec", line 10, in <module>
sys.exit(main())
File ".../kicad_netspec/cli.py", line 53, in main
return int(args.handler(args))
File ".../kicad_netspec/cli.py", line 126, in _doctor
cli = find_kicad_cli()
File ".../kicad_netspec/oracle/discovery.py", line 85, in find_kicad_cli
raise KiCadNotFound(
$ echo $?
1
Same environment, the other subcommands behave as documented:
netspec check exit 4 handled, "This is an environment problem, not a finding about your design."
netspec gate exit 4 handled
netspec netlist exit 4 handled
netspec doctor exit 1 unhandled traceback
Expected
Exit 4 with the environment-fault message, the same as check / gate / netlist.
Cause
KiCadNotFound and EnvironmentError_ are siblings, not parent and child:
oracle/discovery.py:34 class KiCadNotFound(RuntimeError)
oracle/base.py:29 class EnvironmentError_(RuntimeError)
cli.py:54 except EnvironmentError_ as exc: -> return EXIT_ENVIRONMENT
>>> issubclass(KiCadNotFound, EnvironmentError_)
FalseThe other handlers survive because they wrap the failure themselves (cli.py:172, :256, :319 each raise EnvironmentError_(str(exc)) from exc). _doctor calls find_kicad_cli() directly at cli.py:126, so nothing converts it.
Why this one matters more than the exit code suggests
The contract's own distinction is between "could not tell" and "the design is wrong", and exit 4 is how that is communicated. A traceback and exit 1 is indistinguishable to a caller from a crash, and 1 is EXIT_VIOLATION — so a script that treats nonzero-but-not-4 as "a finding" reads a missing KiCad as a bad board. That is the precise confusion the exit codes exist to prevent, occurring in the diagnostic command.
It is also the first command anyone runs on a new machine, so it is the worst place for a traceback.
Suggested fix
Making KiCadNotFound subclass EnvironmentError_ fixes _doctor and any future call site that reaches discovery directly, rather than only the one path. Wrapping inside _doctor also works but leaves the next direct caller with the same bug.
Happy to send a PR if the subclass direction is the one you want.
Activity
fix(oracle): make KiCadNotFound an environment fault, not exit 1 (#21)
chore(release): 0.8.0 (#23)