From ba6e4608fd2a018d4d81d56f75bba6169ba05eb0 Mon Sep 17 00:00:00 2001 From: Sergey Kazenyuk Date: Mon, 27 Dec 2021 04:58:46 +0300 Subject: [PATCH 1/2] Handle exceptions raised in get_output/subprocess.Popen Otherwise plugin infinitely crashes and tries to load direnv config again and again. This happens, for example, when `direnv` is not found on $PATH. --- direnv.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/direnv.py b/direnv.py index cff432e..0123c2c 100644 --- a/direnv.py +++ b/direnv.py @@ -14,18 +14,19 @@ def get_output(cmd, cwd, env=None): - process = subprocess.Popen( - cmd, - cwd=cwd, - env=env, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - returncode = process.wait() - return ( - returncode, - process.stdout.read().decode(), - ANSI_ESCAPE_RE.sub('', process.stderr.read().decode()), - ) + try: + process = subprocess.Popen( + cmd, + cwd=cwd, + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + returncode = process.wait() + return (returncode, + process.stdout.read().decode(), + ANSI_ESCAPE_RE.sub('', process.stderr.read().decode()),) + except (OSError, subprocess.CalledProcessError) as e: + return (e.errno, '', str(e)) class Direnv(object): @@ -67,15 +68,14 @@ def _update_environment(self, file_path): env = {} env.update(os.environ) env.update({k: v for k, v in environment.items() if v is not None}) - with progressbar( - sublime.status_message, - "direnv: loading " + direnv_path + " %s"): + with progressbar(sublime.status_message, + "direnv: loading " + direnv_path + " %s"): returncode, stdout, stderr = get_output( ['direnv', 'export', 'json'], direnv_path, env) if returncode != 0: - sublime.status_message(stderr) + sublime.status_message("direnv load error: %s" % stderr) self._current_direnv_path = None return From 0e0028385ded16df58ef17ab56d0c2ed11f65955 Mon Sep 17 00:00:00 2001 From: Sergey Kazenyuk Date: Mon, 27 Dec 2021 05:08:40 +0300 Subject: [PATCH 2/2] Extract direnv command name constant --- direnv.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/direnv.py b/direnv.py index 0123c2c..26c45a6 100644 --- a/direnv.py +++ b/direnv.py @@ -11,7 +11,7 @@ ANSI_ESCAPE_RE = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') - +DIRENV_CMD = 'direnv' def get_output(cmd, cwd, env=None): try: @@ -71,7 +71,7 @@ def _update_environment(self, file_path): with progressbar(sublime.status_message, "direnv: loading " + direnv_path + " %s"): returncode, stdout, stderr = get_output( - ['direnv', 'export', 'json'], + [DIRENV_CMD, 'export', 'json'], direnv_path, env) if returncode != 0: @@ -114,7 +114,7 @@ def on_post_save(self): class DirenvAllow(sublime_plugin.TextCommand): def run(self, edit): returncode, stdout, stderr = get_output( - ['direnv', 'allow'], + [DIRENV_CMD, 'allow'], os.path.dirname(self.view.file_name())) if returncode != 0: sublime.status_message(stderr) @@ -125,7 +125,7 @@ def run(self, edit): class DirenvDeny(sublime_plugin.TextCommand): def run(self, edit): returncode, stdout, stderr = get_output( - ['direnv', 'deny'], + [DIRENV_CMD, 'deny'], os.path.dirname(self.view.file_name())) if returncode != 0: sublime.status_message(stderr)