Skip to content

fix(tests): set hermetic HOME at conftest import time for Windows xdist workers#1271

Merged
danielmeppiel merged 1 commit into
mainfrom
fix/windows-home-import-time
May 11, 2026
Merged

fix(tests): set hermetic HOME at conftest import time for Windows xdist workers#1271
danielmeppiel merged 1 commit into
mainfrom
fix/windows-home-import-time

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

Problem

CI/CD Pipeline run 25665840560 failed the Windows unit-test job with 53 RuntimeError: Could not determine home directory failures, all on a single xdist worker (gw2) on the new windows-2025-vs2026 GitHub-hosted image.

PR #1270 added a session-scoped autouse fixture in tests/unit/conftest.py that fixed most workers. But on gw2 something resolved Path.home() before that fixture's setup ran, and the worker hit the same crash on every test that constructed RuntimeManager().

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, so HOME / USERPROFILE / HOMEDRIVE / HOMEPATH are guaranteed to be set before anything in the worker process can call Path.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

…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>
Copilot AI review requested due to automatic review settings May 11, 2026 11:33
@danielmeppiel danielmeppiel merged commit 0133e8e into main May 11, 2026
19 checks passed
@danielmeppiel danielmeppiel deleted the fix/windows-home-import-time branch May 11, 2026 11:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread tests/unit/conftest.py
Comment on lines +59 to +60
_TMP_HOME = Path(tempfile.mkdtemp(prefix="apm-unit-home-"))
_set_home_env(_TMP_HOME)
Comment thread CHANGELOG.md
@@ -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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants