Skip to content

Symlinked directories not collected since pytest 6.1.0 #41

@rowan-stein

Description

@rowan-stein

User Request

Symlinked directories not collected since pytest 6.1.0

When there is a symlink to a directory in a test directory, it is just skipped over, but it should be followed and collected as usual.

This regressed in b473e51 (included in 6.1.0). For some reason a follow_symlinks=False was added in there; it does not match the previous behavior and should be removed.

PR for this is coming up.

Active code branch for implementation: pytest-dev__pytest-7982

Research Specification

  • Regression source: src/_pytest/pathlib.py visit() introduced follow_symlinks=False, preventing descent into symlinked directories.
  • Expected behavior: follow symlinked directories during collection (pre-6.1.0 behavior), for both Dir and Package collectors.

Change Details

  • File: src/_pytest/pathlib.py
    • Function: visit(path, recurse)
    • Current (branch pytest-dev__pytest-7982):
      • entries = sorted(os.scandir(path), key=lambda entry: entry.name)
      • if entry.is_dir(follow_symlinks=False) and recurse(entry):
    • Change:
      • Replace follow_symlinks=False with follow_symlinks=True.
    • Rationale: disabling symlink follow caused silent skipping of symlinked directories; restoring default behavior aligns with older versions and current Dir/Package collectors.

Compatibility & Risks

  • Windows symlink support: use symlink_or_skip in tests to skip when unsupported.
  • Broken symlinks: remain uncollected; behavior unchanged.
  • Symlink loops: following symlinks can introduce cycles; if necessary, in future consider a visited set of (st_dev, st_ino) to avoid recursion loops.

Test Plan

Add tests ensuring symlinked directories are collected:

  1. test_collect_symlinked_dir_as_dircollector: create real_sub/test_in_real.py, symlink link_sub -> real_sub; run pytest -q, assert tests under both real_sub and link_sub are collected.
  2. test_collect_symlinked_dir_as_packagecollector: package structure with __init__.py and symlinked link_sub to real_sub; assert tests collected under both.

Reproduction and Observed Failure (current branch)

  • DirCollector case: only real_sub tests are collected; link_sub contents are skipped. No stack trace; silent skip. -vv shows traversal of real_sub but not link_sub.
  • PackageCollector case: same behavior; only real_sub collected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions