From bd77fef25ea1acaa4967e4d41abf4b1039d82765 Mon Sep 17 00:00:00 2001 From: Jun-Fei Cherng Date: Wed, 5 Jun 2024 14:38:05 +0800 Subject: [PATCH] fix: local venv is not used under some circumstances E.g., 1. There is an ".python-version" file but no "pyenv" is installed. 2. "pyenv which python" will raises an exception. 3. `python_path` is an empty string and `None` is returned. But actually we want to test all direct subdirs later and shouldn't return `None` here. Signed-off-by: Jun-Fei Cherng --- plugin.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/plugin.py b/plugin.py index 1090cde..605bae7 100644 --- a/plugin.py +++ b/plugin.py @@ -222,21 +222,25 @@ def binary_from_python_path(path: str | Path) -> Path | None: continue print(f"{cls.name()}: INFO: {config_file} detected. Run subprocess command: {command}") try: - stdout, stderr = subprocess.Popen( - command, - cwd=workspace_folder, - shell=True, - startupinfo=get_default_startupinfo(), - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, - ).communicate() + stdout, stderr = map( + str.rstrip, + subprocess.Popen( + command, + cwd=workspace_folder, + shell=True, + startupinfo=get_default_startupinfo(), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ).communicate(), + ) if stderr: - print(f"{cls.name()}: INFO: subprocess stderr: {stderr.strip()}") - python_path = stdout.strip() + print(f"{cls.name()}: INFO: subprocess stderr: {stderr}") + python_path = stdout if post_processing: python_path = post_processing(python_path) - return Path(python_path) if python_path else None + if python_path: + return Path(python_path) except FileNotFoundError: print(f"{cls.name()}: WARN: subprocess failed with file not found: {command[0]}") except PermissionError as e: