Skip to content

fix: clear build directory cache in pytest fixture to avoid stale references#3743

Merged
frostming merged 2 commits intopdm-project:mainfrom
leemars:main
Mar 4, 2026
Merged

fix: clear build directory cache in pytest fixture to avoid stale references#3743
frostming merged 2 commits intopdm-project:mainfrom
leemars:main

Conversation

@leemars
Copy link
Contributor

@leemars leemars commented Mar 4, 2026

Pull Request Checklist

  • A news fragment is added in news/ describing what is new.
  • Test cases added for changed code.

Problem

When executing pdm run test tests/cli/test_others.py::test_show_package_on_pypi, the test fails with:

FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/.../pdm-build-xxx/requests-2.32.5-py3-none-any.whl'

Root Cause

The issue occurs when multiple PDM commands are executed sequentially in the same test using the same Core instance:

  1. Command 1 (pdm show ipython):

    • create_temp_dir() creates temp directory → stored in _build_dir_cache
    • exit_stack.close() deletes the temp directory
    • Cache still holds reference to deleted directory
  2. Command 2 (pdm show requests):

    • Same process, new temp directory created
    • Cache now has reference to two deleted directories
  3. Command 3 (pdm show --name requests):

    • Tries to reuse cached directory from Command 1
    • FileNotFoundError - directory no longer exists ❌

The root cause is PreparedCandidate._build_dir_cache (a ClassVar[dict]) persisting across multiple command executions within the same test fixture.

Solution

Clear the cache in the pdm pytest fixture's cleanup phase after exit_stack.close():

if cleanup:
    core.exit_stack.close()
    # Clear the build directory cache to avoid stale references
    from pdm.models.candidates import PreparedCandidate
    PreparedCandidate._build_dir_cache.clear()

@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.88%. Comparing base (2c93d66) to head (30b8e99).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/pdm/utils.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3743      +/-   ##
==========================================
+ Coverage   85.86%   85.88%   +0.02%     
==========================================
  Files         118      118              
  Lines       12372    12374       +2     
  Branches     2060     2060              
==========================================
+ Hits        10623    10628       +5     
+ Misses       1176     1173       -3     
  Partials      573      573              
Flag Coverage Δ
unittests 85.74% <66.66%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Use object.__setattr__() to set read-only _key property in packaging < 26
- In packaging >= 26, the __replace__() method is used instead
- This allows the code to pass both mypy and ruff linters
- Added detailed comment explaining version-specific behavior

Fixes mypy error: Property "_key" defined in "Version" is read-only [misc]
@frostming frostming merged commit a9e5f64 into pdm-project:main Mar 4, 2026
25 of 26 checks passed
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