@@ -18,24 +18,34 @@ def debug_manifest(source: YamlDeclarativeSource, args: list[str]) -> None:
1818
1919
2020def _register_components_from_file (filepath : str ) -> None :
21- """Load and register components from a Python file specified in the args."""
21+ """
22+ Dynamically load a Python file containing custom component definitions and register it
23+ under specific module names in sys.modules to ensure that these classes can be properly
24+ resolved during hydration of the manifest yaml file.
25+
26+ This is a somewhat hacky replacement for the file structure manipulation we do when building
27+ connector images to ensure the custom components can be imported.
28+ """
2229 import importlib .util
2330 import sys
2431 from pathlib import Path
25-
32+
2633 components_path = Path (filepath )
34+ if not components_path .exists ():
35+ raise FileNotFoundError (f"Components file not found: { components_path } " )
2736
2837 module_name = "components"
2938 sdm_module_name = "source_declarative_manifest.components"
3039
31- # Create module spec
3240 spec = importlib .util .spec_from_file_location (module_name , components_path )
3341 if spec is None or spec .loader is None :
3442 raise ImportError (f"Could not load module from { components_path } " )
3543
36- # Create module and execute code, registering the module before executing its code
37- # To avoid issues with dataclasses that look up the module
44+ # Create module and execute code
3845 module = importlib .util .module_from_spec (spec )
46+
47+ # Register then execute the module
48+ # we dual-register the module to mirror what is done elsewhere in the CDK
3949 sys .modules [module_name ] = module
4050 sys .modules [sdm_module_name ] = module
4151
0 commit comments