Skip to content

Builder.build() masks plugin import failures as "template not found" #73

Description

@BenWibking

Bug: Builder.build() masks plugin import failures as "template not found"

Severity

Medium

Summary

Builder.build() catches any ImportError from __import__(...) and reports that the template does not exist. This also catches genuine plugin import/runtime dependency errors, leading to misleading diagnostics.

Impact

  • Real plugin bugs appear as false "template not found" errors.
  • Debugging broken plugin dependencies is harder.

Affected Code

Root Cause

Broad except ImportError conflates module-not-found and import-time failure inside plugin.

Proposed Fix

Resolve plugin module existence up front using importlib.util.find_spec. If it exists, import errors should propagate normally.

Full Patch

diff --git a/src/jaff/builder.py b/src/jaff/builder.py
index 1f12f42..8b5df3f 100644
--- a/src/jaff/builder.py
+++ b/src/jaff/builder.py
@@ -1,3 +1,4 @@
+import importlib.util
 import os
 import sys
@@ -21,15 +22,15 @@ class Builder:
 
         # import module based on the template name
-        try:
-            module = __import__(f"jaff.plugins.{template}.plugin", fromlist=["main"])
-        except ImportError as e:
+        module_name = f"jaff.plugins.{template}.plugin"
+        if importlib.util.find_spec(module_name) is None:
             print(f"Error: Template '{template}' not found. Available templates are:")
             for template in os.listdir(
                 os.path.join(os.path.dirname(__file__), "templates", "preprocessor")
             ):
                 print(template)
             sys.exit(1)
+
+        module = __import__(module_name, fromlist=["main"])
 
         # call the main function of the module to preprocess the files
         # the definition of the main function is in the plugin folder

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-code-auditIssues found by AI code audit

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions