From 0a76035afd2fb89826bb3a8517862b31342f20af Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Thu, 26 Oct 2023 11:11:57 -0700 Subject: [PATCH] Better error if plugin file in baseline not found Fixes: #718 --- detect_secrets/core/plugins/initialize.py | 12 +++++++++++- detect_secrets/core/scan.py | 8 ++++++-- detect_secrets/util/importlib.py | 4 +++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/detect_secrets/core/plugins/initialize.py b/detect_secrets/core/plugins/initialize.py index 6d8eeaeb9..b74eab3c3 100644 --- a/detect_secrets/core/plugins/initialize.py +++ b/detect_secrets/core/plugins/initialize.py @@ -31,7 +31,17 @@ def from_plugin_classname(classname: str) -> Plugin: """ :raises: TypeError """ - for plugin_type in get_mapping_from_secret_type_to_class().values(): + try: + plugin_types = get_mapping_from_secret_type_to_class().values() + except FileNotFoundError as e: + log.error(f'Error: Failed to load `{classname}` plugin: {e}') + log.error( + 'This error can occur when using a baseline that references a ' + 'custom plugin with a path that does not exist.', + ) + raise + + for plugin_type in plugin_types: if plugin_type.__name__ == classname: break else: diff --git a/detect_secrets/core/scan.py b/detect_secrets/core/scan.py index f84d53c3c..78d7b7c5c 100644 --- a/detect_secrets/core/scan.py +++ b/detect_secrets/core/scan.py @@ -138,8 +138,12 @@ def scan_line(line: str) -> Generator[PotentialSecret, None, None]: def scan_file(filename: str) -> Generator[PotentialSecret, None, None]: - if not get_plugins(): # pragma: no cover - log.error('No plugins to scan with!') + try: + if not get_plugins(): # pragma: no cover + log.error('No plugins to scan with!') + return + except FileNotFoundError as e: + log.error('Unable to load plugins!') return if _is_filtered_out(required_filter_parameters=['filename'], filename=filename): diff --git a/detect_secrets/util/importlib.py b/detect_secrets/util/importlib.py index f5e90005c..dfa9307a6 100644 --- a/detect_secrets/util/importlib.py +++ b/detect_secrets/util/importlib.py @@ -1,3 +1,4 @@ +import errno import importlib.util import os import pkgutil @@ -85,7 +86,8 @@ def import_file_as_module(filename: str, name: Optional[str] = None) -> ModuleTy for you. """ if not os.path.exists(filename): - raise FileNotFoundError + # Source: https://stackoverflow.com/a/36077407 + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename) if not name: # NOTE: After several trial and error attempts, I could not discern the importance