From: Pieter Lenaerts <plenae@disroot.org>
Date: Sat, 27 Jun 2026 07:58:23 +0200
Subject: Add python path to separate process in tests

tests/issues/test_1027_win_unreachable_cleanup.py starts a separate
python process which doesn't find the source in the build dir.

Add the build dir to the python path for this process. This uses
GBP_BUILD_DIR, which is too debian specific to forward.

Forwarded: not-needed
---
 tests/issues/test_1027_win_unreachable_cleanup.py | 30 +++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/tests/issues/test_1027_win_unreachable_cleanup.py b/tests/issues/test_1027_win_unreachable_cleanup.py
index 63d6dd8..68cc513 100644
--- a/tests/issues/test_1027_win_unreachable_cleanup.py
+++ b/tests/issues/test_1027_win_unreachable_cleanup.py
@@ -8,6 +8,7 @@ stdio shutdown sequence that closes stdin first, allowing graceful exit.
 These tests verify the fix continues to work correctly across all platforms.
 """
 
+import os
 import sys
 import tempfile
 import textwrap
@@ -27,6 +28,21 @@ else:
     from tests.shared.test_win32_utils import escape_path_for_python
 
 
+def get_pythonpath_env() -> dict[str, str]:
+    """
+    Get environment with PYTHONPATH set for subprocesses.
+    This is needed in build environments where the package is not installed.
+    """
+    env = os.environ.copy()
+    pythonpath = env.get("PYTHONPATH", "")
+    # Add current directory if not already in PYTHONPATH
+    build_dir = env.get("GBP_BUILD_DIR", os.getcwd())
+    if build_dir not in pythonpath.split(":"):
+        pythonpath = f"{build_dir}:{pythonpath}" if pythonpath else build_dir
+    env["PYTHONPATH"] = pythonpath
+    return env
+
+
 @pytest.mark.anyio
 async def test_lifespan_cleanup_executed():
     """
@@ -89,7 +105,12 @@ async def test_lifespan_cleanup_executed():
 
     try:
         # Launch the MCP server
-        params = StdioServerParameters(command=sys.executable, args=[server_script])
+        # Add PYTHONPATH so subprocess can find the mcp package in build environments
+        params = StdioServerParameters(
+            command=sys.executable, 
+            args=[server_script],
+            env=get_pythonpath_env()
+        )
 
         async with stdio_client(params) as (read, write):
             async with ClientSession(read, write) as session:
@@ -203,8 +224,13 @@ async def test_stdin_close_triggers_cleanup():
     try:
         # This test manually manages the process to verify stdin-based shutdown
         # Start the server process
+        # Add PYTHONPATH so subprocess can find the mcp package in build environments
         process = await _create_platform_compatible_process(
-            command=sys.executable, args=[server_script], env=None, errlog=sys.stderr, cwd=None
+            command=sys.executable, 
+            args=[server_script], 
+            env=get_pythonpath_env(), 
+            errlog=sys.stderr, 
+            cwd=None
         )
 
         # Wait for server to start
