|
5 | 5 | and only allows imports from specific paths (like /Workspace). We need to add |
6 | 6 | editable install paths to this allowlist. |
7 | 7 |
|
8 | | -Additionally, we patch builtins.__import__ itself to ensure editable paths work |
9 | | -even when called through the autoreload hook's original import reference. |
| 8 | +Environment Variables: |
| 9 | + DBX_PATCH_DEBUG_IMPORTS: Set to '1', 'true', or 'yes' to enable extremely verbose |
| 10 | + import tracing by patching builtins.__import__. This logs every single import |
| 11 | + that happens in the Python process and should only be used for debugging |
| 12 | + import-related issues with editable installs. |
| 13 | +
|
| 14 | +Example: |
| 15 | + import os |
| 16 | + os.environ['DBX_PATCH_DEBUG_IMPORTS'] = '1' |
| 17 | + from dbx_patch import patch_dbx |
| 18 | + patch_dbx() |
10 | 19 | """ |
11 | 20 |
|
12 | 21 | import builtins |
@@ -103,13 +112,21 @@ def patch(self) -> PatchResult: |
103 | 112 | """ |
104 | 113 | logger = self._get_logger() |
105 | 114 |
|
106 | | - # Patch builtins.__import__ for debug logging if in debug mode |
107 | | - if logger and logger._logger.isEnabledFor(logging.DEBUG) and not self._import_patch_applied: |
108 | | - logger.info("Debug logging enabled - patching builtins.__import__ for debug logging...") |
| 115 | + # Patch builtins.__import__ for debug logging ONLY if explicitly enabled via env var |
| 116 | + # This is extremely verbose and should only be used for debugging import issues |
| 117 | + import os |
| 118 | + |
| 119 | + if ( |
| 120 | + os.environ.get("DBX_PATCH_DEBUG_IMPORTS", "").lower() in ("1", "true", "yes") |
| 121 | + and not self._import_patch_applied |
| 122 | + ): |
| 123 | + if logger: |
| 124 | + logger.info("DBX_PATCH_DEBUG_IMPORTS enabled - patching builtins.__import__ for import tracing...") |
109 | 125 | self._original_builtins_import = builtins.__import__ |
110 | 126 | builtins.__import__ = self._patched_builtins_import # type: ignore[assignment] |
111 | 127 | self._import_patch_applied = True |
112 | | - logger.info("builtins.__import__ patched for debug logging") |
| 128 | + if logger: |
| 129 | + logger.info("builtins.__import__ patched for import tracing (this will be very verbose!)") |
113 | 130 |
|
114 | 131 | if self._is_applied: |
115 | 132 | if logger: |
|
0 commit comments