From 6578667197081f34e7b510f6a74e31acf829b979 Mon Sep 17 00:00:00 2001 From: Samuel Price Date: Sun, 19 Apr 2026 12:02:54 -0400 Subject: [PATCH] fix: resolve project-local sym-lib-table for schematics in sub-folders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When add_schematic_component is called with a schematic that lives in a sub-folder (e.g. sheets/rs485.kicad_sch), the handler derived the project path as schematic.parent — which in a hierarchical project is the sheets/ directory, not the project root. Any project-local symbol library declared in sym-lib-table (using ${KIPRJMOD}) is invisible to DynamicSymbolLoader from that directory, causing "Symbol 'X' not found in library 'Y'" even though the library file and sym-lib-table entry both exist. Fix: walk the ancestor chain from the schematic file upward until a directory is found that contains a sym-lib-table file or a .kicad_pro file, then use that as the project root for library resolution. Co-Authored-By: Claude Sonnet 4.6 --- python/kicad_interface.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python/kicad_interface.py b/python/kicad_interface.py index b358445..c1adcf4 100644 --- a/python/kicad_interface.py +++ b/python/kicad_interface.py @@ -718,9 +718,17 @@ class KiCADInterface: x = component.get("x", 0) y = component.get("y", 0) - # Derive project path from schematic path for project-local library resolution + # Derive project path from schematic path for project-local library resolution. + # Walk up from the schematic file to find the directory that owns the project + # (contains sym-lib-table or a .kicad_pro file). Schematics stored in a + # sub-folder (e.g. sheets/) would otherwise resolve to the wrong directory and + # miss any project-local sym-lib-table entries. schematic_file = Path(schematic_path) derived_project_path = schematic_file.parent + for ancestor in schematic_file.parents: + if (ancestor / "sym-lib-table").exists() or list(ancestor.glob("*.kicad_pro")): + derived_project_path = ancestor + break loader = DynamicSymbolLoader(project_path=derived_project_path) loader.add_component(