|
6 | 6 | from ty import find_ty_bin |
7 | 7 |
|
8 | 8 |
|
| 9 | +def _detect_virtualenv() -> str: |
| 10 | + """ |
| 11 | + Find the virtual environment path for the current Python executable. |
| 12 | + """ |
| 13 | + |
| 14 | + # If it's already set, then just use it |
| 15 | + value = os.getenv("VIRTUAL_ENV") |
| 16 | + if value: |
| 17 | + return value |
| 18 | + |
| 19 | + # Otherwise, check if we're in a venv |
| 20 | + venv_marker = os.path.join(sys.prefix, "pyvenv.cfg") |
| 21 | + |
| 22 | + if os.path.exists(venv_marker): |
| 23 | + return sys.prefix |
| 24 | + |
| 25 | + return "" |
| 26 | + |
| 27 | + |
9 | 28 | def _run() -> None: |
10 | 29 | ty = find_ty_bin() |
11 | 30 |
|
| 31 | + env = os.environ.copy() |
| 32 | + venv = _detect_virtualenv() |
| 33 | + if venv: |
| 34 | + env.setdefault("VIRTUAL_ENV", venv) |
| 35 | + |
| 36 | + # Let `ty` know that it was spawned by this Python interpreter |
| 37 | + env["TY__PARENT_INTERPRETER"] = sys.executable |
| 38 | + |
12 | 39 | if sys.platform == "win32": |
13 | 40 | import subprocess |
14 | 41 |
|
15 | 42 | # Avoid emitting a traceback on interrupt |
16 | 43 | try: |
17 | | - completed_process = subprocess.run([ty, *sys.argv[1:]]) |
| 44 | + completed_process = subprocess.run([ty, *sys.argv[1:]], env=env) |
18 | 45 | except KeyboardInterrupt: |
19 | 46 | sys.exit(2) |
20 | 47 |
|
21 | 48 | sys.exit(completed_process.returncode) |
22 | 49 | else: |
23 | | - os.execvp(ty, [ty, *sys.argv[1:]]) |
| 50 | + os.execvpe(ty, [ty, *sys.argv[1:]], env=env) |
24 | 51 |
|
25 | 52 |
|
26 | 53 | if __name__ == "__main__": |
|
0 commit comments