From 7c59e546fcb4af2ea0cb389d627ef1b3e1761ae7 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Thu, 26 Feb 2026 12:39:04 -0600 Subject: [PATCH] Add virtual environment and Python interpreter tracking --- python/ty/__main__.py | 31 +++++++++++++++++++++++++++++-- ruff | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/python/ty/__main__.py b/python/ty/__main__.py index 3ffb6434d..e29531f0a 100644 --- a/python/ty/__main__.py +++ b/python/ty/__main__.py @@ -6,21 +6,48 @@ from ty import find_ty_bin +def _detect_virtualenv() -> str: + """ + Find the virtual environment path for the current Python executable. + """ + + # If it's already set, then just use it + value = os.getenv("VIRTUAL_ENV") + if value: + return value + + # Otherwise, check if we're in a venv + venv_marker = os.path.join(sys.prefix, "pyvenv.cfg") + + if os.path.exists(venv_marker): + return sys.prefix + + return "" + + def _run() -> None: ty = find_ty_bin() + env = os.environ.copy() + venv = _detect_virtualenv() + if venv: + env.setdefault("VIRTUAL_ENV", venv) + + # Let `ty` know that it was spawned by this Python interpreter + env["TY__PARENT_INTERPRETER"] = sys.executable + if sys.platform == "win32": import subprocess # Avoid emitting a traceback on interrupt try: - completed_process = subprocess.run([ty, *sys.argv[1:]]) + completed_process = subprocess.run([ty, *sys.argv[1:]], env=env) except KeyboardInterrupt: sys.exit(2) sys.exit(completed_process.returncode) else: - os.execvp(ty, [ty, *sys.argv[1:]]) + os.execvpe(ty, [ty, *sys.argv[1:]], env=env) if __name__ == "__main__": diff --git a/ruff b/ruff index 0e19fc9a6..10c082f61 160000 --- a/ruff +++ b/ruff @@ -1 +1 @@ -Subproject commit 0e19fc9a61477e71abc4eb76f05a129b6b9ab873 +Subproject commit 10c082f616d8296df0cd3489a98db8c5d40628d1