Skip to content

fix: deduplicate Directory nodes on re-collection to preserve fixture identity (backport #14635) - #14974

Closed
wahajahmed010 wants to merge 1 commit into
pytest-dev:9.1.xfrom
wahajahmed010:fix/14964-backport-directory-dedup
Closed

wahajahmed010 wants to merge 1 commit into
pytest-dev:9.1.xfrom
wahajahmed010:fix/14964-backport-directory-dedup

Conversation

@wahajahmed010

Copy link
Copy Markdown

Summary

Backport of #14635 to the 9.1.x branch to fix #14964.

When a test file is passed directly on the command line (e.g. pytest tests/a.py test_x.py tests/b.py), Session.collect re-collects the parent Directory with handle_dupes=False, which previously created fresh child Directory nodes. Since fixture registration in 9.1.x+ uses node identity for matching, fixtures registered against the original Directory instance became invisible to items collected under the new one, causing conftest fixtures to be silently missing.

Reproduction (from #14964)

pytest tests/a.py test_x.py tests/b.py

Expected: test_a and test_b both fail during setup because of the autouse=True guard fixture in tests/conftest.py.
Observed on 9.1.1: only test_a errors; test_b passes — the conftest fixtures were missing from its fixture closure.

Fix

This is the backport of commit fc8f56b from main (#14635):

When _collect_one_node re-collects a parent Directory that is already in _collection_cache, reuse the previously-seen Directory children by path. Module/File nodes are still recreated to preserve --keep-duplicates behavior.

Changelog

Added changelog/14964.bugfix.rst per the contribution guidelines.

Fixes #14964

… identity

Backport of pytest-dev#14635 to the 9.1.x branch to fix pytest-dev#14964.

When Session re-collects a parent Directory (handle_dupes=False for file
CLI args), fresh child nodes were created. Since fixture registration
uses node identity for matching, the new Directory children didn't match
fixtures registered with the original instances.

Reuse previously-seen Directory children (by path) when re-collecting a
parent. Module/File nodes are still recreated to preserve
--keep-duplicates behavior.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 5, 2026
@RonnyPfannschmidt

Copy link
Copy Markdown
Member

Hideous untested clone of #14968

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants