diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 8f5f77ca52..aead6a73af 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -165,6 +165,19 @@ async def compile_esphome( """Compile an ESPHome configuration and return the binary path.""" async def _compile(config_path: Path) -> Path: + # Create a unique PlatformIO directory for this test to avoid race conditions + platformio_dir = integration_test_dir / ".platformio" + platformio_dir.mkdir(parents=True, exist_ok=True) + + # Create cache directory as well + platformio_cache_dir = platformio_dir / ".cache" + platformio_cache_dir.mkdir(parents=True, exist_ok=True) + + # Set up environment with isolated PlatformIO directories + env = os.environ.copy() + env["PLATFORMIO_CORE_DIR"] = str(platformio_dir) + env["PLATFORMIO_CACHE_DIR"] = str(platformio_cache_dir) + # Retry compilation up to 3 times if we get a segfault max_retries = 3 for attempt in range(max_retries): @@ -179,6 +192,7 @@ async def compile_esphome( stdin=asyncio.subprocess.DEVNULL, # Start in a new process group to isolate signal handling start_new_session=True, + env=env, ) await proc.wait()