fix(tests): override Path.home() in root conftest so Windows xdist gw2 cannot crash#1272
Merged
Conversation
…2 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>
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>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the test harness against Windows pytest-xdist worker subprocesses that can crash when pathlib.Path.home() cannot resolve a home directory (notably on the windows-2025-vs2026 runner), by applying a deterministic, hermetic Path.home() behavior at the earliest shared hook (root tests/conftest.py).
Changes:
- Override
Path.home()in the roottests/conftest.pyat import time, backed byHOME/Windows-home env vars with a tmp fallback. - Remove the now-redundant
tests/unit/conftest.pyimport-time HOME mutation. - Add a
CHANGELOG.mdUnreleased fix entry for the test stability change.
Show a summary per file
| File | Description |
|---|---|
tests/unit/conftest.py |
Deletes redundant unit-only import-time HOME mutation. |
tests/conftest.py |
Adds import-time hermetic HOME env setup and overrides Path.home() for all test runs/workers. |
CHANGELOG.md |
Documents the Windows test-flake fix under Unreleased -> Fixed. |
Copilot's findings
- Files reviewed: 4/4 changed files
- Comments generated: 3
|
|
||
| import pytest | ||
|
|
||
| _TMP_HOME = Path(tempfile.mkdtemp(prefix="apm-test-home-")) |
| _ensure_home_env(_TMP_HOME) | ||
|
|
||
|
|
||
| def _hermetic_home(_cls=Path) -> Path: |
Comment on lines
+51
to
+58
| Honors HOME / USERPROFILE / HOMEDRIVE+HOMEPATH so per-test | ||
| `monkeypatch.setenv("HOME", ...)` (or its Windows-trio equivalent) | ||
| keeps working. Falls back to a hermetic tmp dir only when the env | ||
| is empty -- which is the windows-2025-vs2026 xdist worker case. | ||
| """ | ||
| home = os.environ.get("HOME") | ||
| if not home and os.name == "nt": | ||
| home = os.environ.get("USERPROFILE") |
danielmeppiel
added a commit
that referenced
this pull request
May 11, 2026
…1276) * fix(tests): wrap Path.expanduser() to never raise on Windows runner Companion to #1272. After the Path.home() override landed, the windows-2025-vs2026 runner still red-marked two tests: tests/unit/install/test_user_scope_rejection_reason.py ::test_user_scope_accepts_tilde_local_path[~/pkg] ::test_user_scope_accepts_tilde_local_path[~/sub/pkg] These hit a different code path: production code in install.package_resolution.user_scope_rejection_reason calls Path('~/pkg').expanduser() to detect that the path is absolute. ntpath.expanduser raises RuntimeError('Could not determine home directory.') when both USERPROFILE and HOMEPATH are absent -- exactly the windows-2025-vs2026 worker env state. Fix in tests/conftest.py (root, loaded by every xdist worker before any test): - Wrap Path.expanduser. On RuntimeError, fall back to _TMP_HOME (the same hermetic dir Path.home() uses). The expanded path is still absolute, so production code's .is_absolute() check keeps behaving correctly. Regression trap added in tests/unit/test_path_home_override.py: clears HOME/USERPROFILE/HOMEDRIVE/HOMEPATH and asserts Path('~/pkg').expanduser() returns an absolute path without raising. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * chore: correct PR number in CHANGELOG entry 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 25667668848 -- the run that fired after #1271 merged -- still failed the Windows unit-test job with 46
RuntimeError: Could not determine home directoryfailures, all on xdist workergw2on the newwindows-2025-vs2026GitHub-hosted image.The session-scoped autouse fixture (PR #1270) and the import-time env mutation in
tests/unit/conftest.py(PR #1271) both turned out to be insufficient: ongw2, something resolvesPath.home()before the unit conftest's setup takes effect on that worker.Fix
Override
Path.home()itself intests/conftest.py(root). Properties:Path(env['HOME'])when set, then the Windows trio (USERPROFILE/HOMEDRIVE+HOMEPATH), and only falls back to a hermetic tmp dir when nothing is set. So per-testmonkeypatch.setenv("HOME", ...)keeps working (verified locally with thetests/unit/integration/test_scope_*suite).Path.home()during the test run gets a usable path even on runners that would otherwise leave the worker subprocess with an emptyUSERPROFILE.The previous
tests/unit/conftest.pyimport-time mutation is now redundant -- deleted.Validation
uv run pytest tests/unit tests/test_console.py -n auto --dist worksteal-- 8293 passed locally (incl. the scope-resolution tests that exercisemonkeypatch.setenv("HOME")).uv run --extra dev ruff check src/ tests/ && ruff format --check src/ tests/-- silent.Notes
This is required to unblock the v0.13.0 retag.
Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com