Skip to content

hatch test fails #2154

@degenaro

Description

@degenaro

Describe the bug

Intermittent pytest: not found in Hatch CI. Only seems to failing in github actions after merging of the PR with multiple pipelines running. Sometimes failing for PR alone (before merge), but not laptop.

1s
Run hatch test -py 3.10
  hatch test -py 3.10
  shell: /usr/bin/bash -e {0}
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.10.20/x64
    PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.10.20/x64/lib/pkgconfig
    Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.20/x64
    Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.20/x64
    Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.20/x64
    LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.10.20/x64/lib
────────────────────────────── hatch-test.py3.10 ───────────────────────────────
/bin/sh: 1: pytest: not found
Error: Process completed with exit code 127.

To Reproduce

Merge PR.

Expected behavior

hatch should not fail.

Screenshots / Logs (asked Claude)

Diagnosed intermittent failure as race condition or caching issue
The user says the pytest: not found error fails intermittently. This suggests a race condition or caching issue rather than a missing dependency.

Done
Intermittent pytest: not found in Hatch CI usually points to one of these causes:

  1. Hatch env cache corruption / partial cache restore

If you're caching the Hatch environment between CI runs, a partial or stale cache can produce an env missing its dependencies. Fix by keying the cache tightly:

yaml

  • uses: actions/cache@v3
    with:
    path: ~/.local/share/hatch
    key: hatch-${{ runner.os }}-${{ hashFiles('pyproject.toml', 'requirements*.txt') }}
    restore-keys: |
    hatch-${{ runner.os }}-
    Or just disable the cache to rule it out entirely.
  1. Hatch env creation race condition (parallel matrix jobs)

If multiple matrix jobs share a cache and try to write to it simultaneously, one can get a half-built env. Fix with:

yaml

  • run: hatch env create hatch-test.py3.10
  • run: hatch test -py 3.10
    The explicit env create step ensures the env is fully built before tests run.
  1. Hatch itself isn't fully installed yet when the command runs
    Intermittent on slow runners — fix by pinning and verifying:
    yaml- run: pip install hatch
  • run: hatch --version # sanity check
  • run: hatch test -py 3.10
  1. Network flakiness during env setup
    Hatch pulls pytest from PyPI on first run. If PyPI is slow/unreliable, the install can fail silently. Retry logic helps:
    yaml- name: Run tests (with retry)
    uses: nick-fields/retry@v2
    with:
    timeout_minutes: 10
    max_attempts: 3
    command: hatch test -py 3.10

Quickest diagnostic: Add hatch env show before the test run to log whether the env and its dependencies are actually present before pytest is invoked. That'll tell you if it's a creation problem vs. a path/activation problem.

Environment

  • OS: [e.g. iOS]
  • Python version:
  • Installed packages:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions