fix(tests): set hermetic HOME at conftest import time for Windows xdist workers#1271
Merged
Conversation
…st workers The previous session-scoped autouse fixture in tests/unit/conftest.py fixed most Windows runner failures, but a single xdist worker (gw2) still hit 53 'RuntimeError: Could not determine home directory' on the windows-2025-vs2026 image -- some test or fixture resolved Path.home() before the autouse fixture's setup ran on that worker. Move the env mutation to module-level import time. Each xdist worker imports tests/unit/conftest.py once, before any fixture or collection runs, so HOME / USERPROFILE / HOMEDRIVE / HOMEPATH are guaranteed to be set before anything in the worker process can call Path.home(). Refs run https://github.com/microsoft/apm/actions/runs/25665840560 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens Windows unit-test execution under pytest-xdist by ensuring a hermetic HOME is established at tests/unit/conftest.py import time, eliminating a race where Path.home() could be evaluated before a session-scoped autouse fixture ran.
Changes:
- Move hermetic HOME env seeding from a session-scoped autouse fixture to module import time in
tests/unit/conftest.py. - Add a changelog entry describing the Windows xdist worker race fix.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/conftest.py | Sets HOME/USERPROFILE/HOMEDRIVE/HOMEPATH at import time using a temp directory to prevent Path.home() failures on Windows xdist workers. |
| CHANGELOG.md | Adds an Unreleased entry for the updated hermetic HOME approach. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 2
Comment on lines
+59
to
+60
| _TMP_HOME = Path(tempfile.mkdtemp(prefix="apm-unit-home-")) | ||
| _set_home_env(_TMP_HOME) |
| @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
|
|
|||
| - Pin `Path.home()` under unit tests via a session-scoped autouse conftest fixture, fixing 56 Windows runner failures on the new `windows-2025-vs2026` GitHub-hosted image where `USERPROFILE`/`HOMEDRIVE`+`HOMEPATH` are not seeded for pytest workers; also patch the `_check_and_notify_updates` import binding in the disabled-self-update test so it no longer races on the version-check cache. (#1270) | |||
danielmeppiel
added a commit
that referenced
this pull request
May 11, 2026
…2 cannot crash (#1272) * fix(tests): override Path.home() in root conftest so Windows xdist gw2 cannot crash PR #1271 set HOME / USERPROFILE / HOMEDRIVE / HOMEPATH at conftest import time, but a single xdist worker (gw2) on windows-2025-vs2026 still hit 46 'RuntimeError: Could not determine home directory' failures (run 25667668848). Whatever the cause -- worker subprocess spawning order, conftest discovery quirks, or some other code path calling Path.home() before the unit-conftest mutation took effect on that worker -- the env-mutation approach is not robust enough. Override Path.home() directly in tests/conftest.py (root). The override: - is applied at import time of the root conftest, which every xdist worker loads before any test in any directory runs; - returns Path(env['HOME']) when set, then the Windows trio, falling back to a hermetic tmp dir only when nothing is set; - never raises, so any production or test code path calling Path.home() during the test run gets a usable path even on runners that would otherwise leave the worker subprocess with an empty HOME / USERPROFILE. Per-test 'monkeypatch.setenv("HOME", ...)' (e.g. the scope-resolution tests under tests/unit/integration/) keeps working because the override reads from os.environ on every call. The previous tests/unit/conftest.py import-time mutation is now redundant -- delete it. Refs run https://github.com/microsoft/apm/actions/runs/25667668848 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * test: regression trap for Path.home() with cleared env (gw2 scenario) Locks in the contract: Path.home() must not raise even when every env var ntpath.expanduser / posixpath.expanduser consults is unset. This is exactly the windows-2025-vs2026 xdist worker case that produced the 56 / 53 / 46 failure cascades. Also asserts that per-test monkeypatch.setenv("HOME", ...) keeps working (so the scope-resolution tests under tests/unit/integration/ don't regress). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Daniel Meppiel <copilot-rework@github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CI/CD Pipeline run 25665840560 failed the Windows unit-test job with 53
RuntimeError: Could not determine home directoryfailures, all on a single xdist worker (gw2) on the newwindows-2025-vs2026GitHub-hosted image.PR #1270 added a session-scoped autouse fixture in
tests/unit/conftest.pythat fixed most workers. But ongw2something resolvedPath.home()before that fixture's setup ran, and the worker hit the same crash on every test that constructedRuntimeManager().Fix
Move the env mutation from a session-scoped autouse fixture to module-level import time in
tests/unit/conftest.py. Each xdist worker imports the conftest once, before any collection, fixture resolution, or test execution, soHOME/USERPROFILE/HOMEDRIVE/HOMEPATHare guaranteed to be set before anything in the worker process can callPath.home().Validation
uv run pytest tests/unit tests/test_console.py -n auto --dist worksteal-- 8293 passed locally.uv run --extra dev ruff check src/ tests/ && ruff format --check src/ tests/-- silent.Notes
This unblocks the v0.13.0 retag once #1268 and #1255 also land.
Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com