Skip to content

Handle directory-open errors through on_error during tree walks - #149

Open
shkyyy18 wants to merge 1 commit into
cpburnz:masterfrom
shkyyy18:fix/walk-scandir-errors
Open

shkyyy18 wants to merge 1 commit into
cpburnz:masterfrom
shkyyy18:fix/walk-scandir-errors

Conversation

@shkyyy18

@shkyyy18 shkyyy18 commented Oct 1, 2026

Copy link
Copy Markdown

Summary

iter_tree_entries() and iter_tree_files() document on_error as the filesystem-error handler (and None as ignoring filesystem errors), but an error opening a directory with os.scandir() bypasses the handler. A directory removed before scanning or an unreadable child therefore aborts the entire walk, even when the caller supplies a handler to log and continue.

from pathspec.util import iter_tree_files

errors = []
# Use a path that does not exist:
list(iter_tree_files("missing-directory", on_error=errors.append))
# Before: FileNotFoundError; errors stays empty.
# After: []; errors contains the FileNotFoundError.

Catch errors from opening each directory in both walkers, remove that directory from the active-ancestor memo, call the handler when present, and return from that subtree. Exceptions raised by the handler still propagate. Successful scanning and recursive-loop detection are unchanged. This is intentionally limited to opening a directory, not a general rewrite of entry inspection or iteration.

Tests

  • Added missing-root tests for ignore, record, and re-raise behavior in both walkers.
  • Added synthetic permission-denied child tests for both walkers; healthy siblings remain discoverable. These inject PermissionError at os.scandir, rather than relying on platform-specific permission manipulation.
  • Before the fix: all 10 new subcases error. After: both new test methods and all 10 subcases pass.
  • Python 3.12 / Windows: python -m unittest discover -t . -s tests/ is OK (225 test methods; 398 skipped cases/subcases). Unmodified baseline: 223 methods, same 398 skips. Optional re2/hyperscan backends and unsupported symlink cases are skipped; no full backend/platform matrix or docs-build claim.
  • git diff --check passes. Updated changelog source and generated changelog together.

AI assistance disclosure: this patch and its regression tests were developed with OpenAI Codex assistance; the behavior above was reproduced and tested locally.

This branch has not been deployed

No deployments
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.

1 participant