Skip to content

Commit 6eea7c4

Browse files
print stacktraces during import errors (#10906)
[ci skip-rust] ### Problem Pants only prints *what* failed to import upon an `ImportError`, not *where*.
1 parent 478110e commit 6eea7c4

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/python/pants/base/exception_sink.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ class ExceptionSink:
110110
# Where to log stacktraces to in a SIGUSR2 handler.
111111
_interactive_output_stream = None
112112

113-
# An instance of `SignalHandler` which is invoked to handle a static set of specific
114-
# nonfatal signals (these signal handlers are allowed to make pants exit, but unlike SIGSEGV they
115-
# don't need to exit immediately).
113+
# An instance of `SignalHandler` which is invoked to handle a static set of specific nonfatal
114+
# signals (these signal handlers are allowed to make pants exit, but unlike SIGSEGV they don't
115+
# need to exit immediately).
116116
_signal_handler: SignalHandler = SignalHandler(pantsd_instance=False)
117117

118118
# These persistent open file descriptors are kept so the signal handler can do almost no work
119119
# (and lets faulthandler figure out signal safety).
120120
_pid_specific_error_fileobj = None
121121
_shared_error_fileobj = None
122122

123+
# Set in methods on SignalHandler and exposed to the engine rust code.
123124
_signal_sent: Optional[int] = None
124125

125126
def __new__(cls, *args, **kwargs):
@@ -360,7 +361,8 @@ def log_exception(cls, exc_class=None, exc=None, tb=None, add_newline=False):
360361

361362
extra_err_msg = None
362363
try:
363-
# Always output the unhandled exception details into a log file, including the traceback.
364+
# Always output the unhandled exception details into a log file, including the
365+
# traceback.
364366
exception_log_entry = cls._format_unhandled_exception_log(
365367
exc, tb, add_newline, should_print_backtrace=True
366368
)
@@ -369,8 +371,13 @@ def log_exception(cls, exc_class=None, exc=None, tb=None, add_newline=False):
369371
extra_err_msg = "Additional error logging unhandled exception {}: {}".format(exc, e)
370372
logger.error(extra_err_msg)
371373

372-
# Generate an unhandled exception report fit to be printed to the terminal.
373-
logger.exception(exc)
374+
# The rust logger implementation will have its own stacktrace, but at import time, we want
375+
# to be able to see any stacktrace to know where the error is being raised, so we reproduce
376+
# it here.
377+
exception_log_entry = cls._format_unhandled_exception_log(
378+
exc, tb, add_newline, should_print_backtrace=True
379+
)
380+
logger.exception(exception_log_entry)
374381

375382
@classmethod
376383
def _handle_signal_gracefully(cls, signum, signame, traceback_lines):

0 commit comments

Comments
 (0)