Skip to content

Add pcb export-kicad subcommand#771

Open
jeanmw wants to merge 6 commits into
diodeinc:mainfrom
jeanmw:cli-kicad-export
Open

Add pcb export-kicad subcommand#771
jeanmw wants to merge 6 commits into
diodeinc:mainfrom
jeanmw:cli-kicad-export

Conversation

@jeanmw

@jeanmw jeanmw commented May 1, 2026

Copy link
Copy Markdown

Summary

Implements pcb export-kicad, a new CLI subcommand that takes a .zen
file and writes a standalone, self-contained KiCad project directory.

  • New subcommand: pcb export-kicad <file.zen> -o <dir> [--offline] [--locked]
  • --offline and --locked plumb through to the shared resolve::resolve(...)
    pipeline used by pcb build/pcb bom/etc.
  • Refuses missing/non-.zen inputs with a clear error.
  • Fixes a temp-dir leak in pcb_layout::process_layout(use_temp_dir=true):
    the TempDir is now owned by the returned LayoutResult and is removed
    when the result drops, instead of being kept via TempDir::keep().

Closes #682.

Notes for reviewers

  • The exported directory contains layout.kicad_pro + layout.kicad_pcb
    (+ .kicad_prl). KiCad creates the .kicad_sch file itself the first
    time eeschema opens the project — same behavior as
    crates/pcb-layout/tests/layout_generation.rs.
  • LayoutResult gained a private _temp_dir: Option<TempDir> field. All
    existing callers pass use_temp_dir=false so they get None and
    behavior is unchanged. Only the new export_kicad path passes true.

Test plan

  • cargo test --workspace --locked --no-fail-fast
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo fmt --all -- --check
  • cargo run -p pcb -- fmt --check stdlib
  • crates/pcb/tests/export_kicad.rs:
    • test_export_kicad_rejects_missing_file
    • test_export_kicad_writes_project_directory
    • test_export_kicad_offline_locked_writes_project_directory (hermetic,
      seeded sandbox cache, network egress blocked)
  • Verified no pcb-layout-* directories leak into $TMPDIR after
    running the export tests.
  • Manual smoke test: opened the exported layout.kicad_pro in KiCad
    and confirmed it loads cleanly.

Note

Medium Risk
Adds a new CLI command that generates and copies full KiCad projects, and changes process_layout(use_temp_dir=true) to manage temp directory cleanup via LayoutResult, which could affect workflows that relied on the previous temp dir persistence unless persist_temp_dir() is used.

Overview
Adds a new pcb export-kicad subcommand that builds a .zen design, runs process_layout in a fresh temp layout, then copies the generated KiCad project files into a user-specified output directory (with --offline/--locked wired through resolution) and reports diagnostics.

Fixes a temp-dir leak/behavior in pcb_layout::process_layout(use_temp_dir=true) by having LayoutResult own the backing TempDir and delete it on drop, plus introduces LayoutResult::persist_temp_dir() and updates pcb layout --temp to call it before launching KiCad asynchronously.

Adds tests and snapshots covering export-kicad success, missing input handling, and offline/locked execution, and updates the CLI help snapshot to include the new command.

Reviewed by Cursor Bugbot for commit 05f5a8c. Bugbot is set up for automated code reviews on this repo. Configure here.

jeanmw added 5 commits April 30, 2026 15:32
process_layout(use_temp_dir=true) previously called TempDir::keep(),
leaving an orphaned pcb-layout-* directory under $TMPDIR after every
export. Move ownership of the TempDir onto the returned LayoutResult so
it cleans up when the caller drops the result, and remove the workaround
in export_kicad. Adds a hermetic test exercising --offline --locked.
Collapse two-line with_context() and trim a stray double-space comment
flagged by `cargo fmt --all -- --check`.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 200426f. Configure here.

Comment thread crates/pcb-layout/src/lib.rs
`pcb layout --temp` runs `process_layout(use_temp_dir=true)` and then
spawns KiCad asynchronously via `pcb_kicad::open_pcbnew`. With the
previous commit moving TempDir ownership onto LayoutResult, the temp
directory was being removed at function exit while pcbnew was still
loading the .kicad_pcb / .kicad_pro files from it.

Add `LayoutResult::persist_temp_dir(&mut self)` to opt out of cleanup
on drop, and call it in the `pcb layout --temp` path before spawning
KiCad. `pcb export-kicad` does not call it — it copies the project
out and lets the temp dir clean up normally.

Includes unit tests covering both branches (drop cleans, persist keeps).
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.

Allow exporting schematic to KiCAD in the CLI

1 participant