From db478750473482413bc54b71a2145eaf086064b7 Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Sun, 12 Jan 2025 11:44:25 -0700 Subject: [PATCH] fix(parser-custom): handle relative parent directory paths to module file better The dynamic import originally would just replace "/" with "." to make the import module name more pythonic, however this would be problematic in monorepos which would use "../../misc/commit_parser.py" to locate the parser and so the resulting `sys.modules` entry would have numerous periods (.) as a prefix. This removes that possibility. Still always an issue if the imported module name matches an existing module but the likelihood is low. --- src/semantic_release/helpers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/semantic_release/helpers.py b/src/semantic_release/helpers.py index db82a97eb..83d700bfe 100644 --- a/src/semantic_release/helpers.py +++ b/src/semantic_release/helpers.py @@ -69,7 +69,6 @@ def dynamic_import(import_path: str) -> Any: Dynamically import an object from a conventionally formatted "module:attribute" string """ - log.debug("Trying to import %s", import_path) module_name, attr = import_path.split(":", maxsplit=1) # Check if the module is a file path, if it can be resolved and exists on disk then import as a file @@ -78,10 +77,11 @@ def dynamic_import(import_path: str) -> Any: module_path = ( module_filepath.stem if Path(module_name).is_absolute() - else str(Path(module_name).with_suffix("")).replace(os.sep, ".") + else str(Path(module_name).with_suffix("")).replace(os.sep, ".").lstrip(".") ) if module_path not in sys.modules: + log.debug("Loading '%s' from file '%s'", module_path, module_filepath) spec = importlib.util.spec_from_file_location( module_path, str(module_filepath) ) @@ -96,7 +96,9 @@ def dynamic_import(import_path: str) -> Any: # Otherwise, import as a module try: + log.debug("Importing module '%s'", module_name) module = importlib.import_module(module_name) + log.debug("Loading '%s' from module '%s'", attr, module_name) return getattr(module, attr) except TypeError as err: raise ImportError(