From a1d2c5226b598ed4cfbb0a67b943bb6ab1ae825c Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 21 Apr 2023 15:19:04 +0530 Subject: [PATCH 1/3] fix: Print failing command with CommandFailedError I've seen way too many issues being faced / misunderstandings due to the lack of "cmd" --- bench/utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/utils/__init__.py b/bench/utils/__init__.py index b9e0cced..3fe17ad2 100644 --- a/bench/utils/__init__.py +++ b/bench/utils/__init__.py @@ -155,7 +155,7 @@ def exec_cmd(cmd, cwd=".", env=None, _raise=True): if return_code: logger.warning(f"{cmd_log} executed with exit code {return_code}") if _raise: - raise CommandFailedError from subprocess.CalledProcessError(return_code, cmd) + raise CommandFailedError(cmd) from subprocess.CalledProcessError(return_code, cmd) return return_code From 817523943414345bdd161b50c263f634b7a8f70d Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 21 Apr 2023 15:48:33 +0530 Subject: [PATCH 2/3] fix: Hide irrelevant stacks during exc raise --- bench/cli.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/bench/cli.py b/bench/cli.py index 7f262407..8ced2ef1 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -118,6 +118,8 @@ def cli(): _opts = [x.opts + x.secondary_opts for x in bench_command.params] opts = {item for sublist in _opts for item in sublist} + setup_exception_handler() + # handle usages like `--use-feature='feat-x'` and `--use-feature 'feat-x'` if cmd_from_sys and cmd_from_sys.split("=", 1)[0].strip() in opts: bench_command() @@ -240,3 +242,17 @@ def setup_clear_cache(): return f(*args, **kwargs) os.chdir = _chdir + + +def setup_exception_handler(): + from traceback import format_exception + + def handle_exception(exc_type, exc_info, tb): + print("".join(generate_exc(exc_type, exc_info, tb))) + + def generate_exc(exc_type, exc_info, tb): + for t in format_exception(exc_type, exc_info, tb): + if "/click/" not in t: + yield t + + sys.excepthook = handle_exception From 94a1b8c3fb8bee513c02841f48a2d05ccaab6a53 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Wed, 10 May 2023 15:40:11 +0530 Subject: [PATCH 3/3] fix: Setup exception handler for CommandFailedError only * Match only certain module lib paths --- bench/cli.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/bench/cli.py b/bench/cli.py index 8ced2ef1..215d47cd 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -28,6 +28,8 @@ from bench.utils import ( get_cmd_from_sysargv, ) from bench.utils.bench import get_env_cmd +from importlib.util import find_spec + # these variables are used to show dynamic outputs on the terminal dynamic_feed = False @@ -38,6 +40,7 @@ bench.LOG_BUFFER = [] change_uid_msg = "You should not run this command as root" src = os.path.dirname(__file__) +SKIP_MODULE_TRACEBACK = ("click",) @contextmanager @@ -246,13 +249,22 @@ def setup_clear_cache(): def setup_exception_handler(): from traceback import format_exception + from bench.exceptions import CommandFailedError def handle_exception(exc_type, exc_info, tb): - print("".join(generate_exc(exc_type, exc_info, tb))) + if exc_type == CommandFailedError: + print("".join(generate_exc(exc_type, exc_info, tb))) + else: + sys.__excepthook__(exc_type, exc_info, tb) def generate_exc(exc_type, exc_info, tb): - for t in format_exception(exc_type, exc_info, tb): - if "/click/" not in t: - yield t + TB_SKIP = [ + os.path.dirname(find_spec(module).origin) for module in SKIP_MODULE_TRACEBACK + ] + + for tb_line in format_exception(exc_type, exc_info, tb): + for skip_module in TB_SKIP: + if skip_module not in tb_line: + yield tb_line sys.excepthook = handle_exception