fix: resolve project-local sym-lib-table for schematics in sub-folders

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 <noreply@anthropic.com>
This commit is contained in:
Samuel Price
2026-04-19 12:02:54 -04:00
parent 3d996427ee
commit 6578667197

View File

@@ -718,9 +718,17 @@ class KiCADInterface:
x = component.get("x", 0) x = component.get("x", 0)
y = component.get("y", 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) schematic_file = Path(schematic_path)
derived_project_path = schematic_file.parent 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 = DynamicSymbolLoader(project_path=derived_project_path)
loader.add_component( loader.add_component(