fix: skip non-regular files in scan_compiled_extensions#1002
fix: skip non-regular files in scan_compiled_extensions#1002mikedep333 wants to merge 1 commit intopython-wheel-build:mainfrom
Conversation
pathlib.Path.walk() lists symlinks to directories as filenames rather than dirnames. This causes scan_compiled_extensions to attempt to open() them, resulting in IsADirectoryError. Add an is_file() check to skip non-regular files before attempting to read their contents. Closes: python-wheel-build#1001 Signed-off-by: Mike DePaulo <mikedep333@redhat.com> Co-Authored-By: Claude Opus 4.6 <claude@anthropic.com>
📝 WalkthroughWalkthroughThe Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/fromager/sources.py (1)
862-863: Add a regression test for symlinked directories.This fix is solid, but current tests (see
tests/test_sources.py:304-313) only cover regular files, so this bug can regress silently.✅ Suggested test addition
diff --git a/tests/test_sources.py b/tests/test_sources.py @@ def test_scan_compiled_extensions( filename: str, content: bytes, hit: bool, tmp_path: pathlib.Path ) -> None: @@ else: assert matches == [] + + +def test_scan_compiled_extensions_skips_symlinked_directory(tmp_path: pathlib.Path) -> None: + target_dir = tmp_path / "real_dir" + target_dir.mkdir() + (target_dir / "module.so").write_bytes(b"\x7fELFdummy") + + link_path = tmp_path / "linked_dir" + try: + link_path.symlink_to(target_dir, target_is_directory=True) + except (OSError, NotImplementedError): + pytest.skip("Symlinks are not supported in this test environment") + + matches = sources.scan_compiled_extensions(tmp_path) + # Main assertion: no crash; and we only report real files reachable by walk logic. + assert isinstance(matches, list)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/fromager/sources.py` around lines 862 - 863, Tests currently only cover regular files so the check using filepath.is_file() in src/fromager/sources.py can regress for symlinked directories; add a regression test in tests/test_sources.py (near the existing test at lines ~304-313) that creates a symlinked directory pointing to a real directory with files and asserts the same behavior as for regular directories/files (i.e., the code should follow the symlink and include files under the target); ensure the test sets up and tears down the symlink and uses the same helper functions used by other tests to verify that the module handling (the code path involving filepath.is_file()) correctly processes symlinked directories.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/fromager/sources.py`:
- Around line 862-863: Tests currently only cover regular files so the check
using filepath.is_file() in src/fromager/sources.py can regress for symlinked
directories; add a regression test in tests/test_sources.py (near the existing
test at lines ~304-313) that creates a symlinked directory pointing to a real
directory with files and asserts the same behavior as for regular
directories/files (i.e., the code should follow the symlink and include files
under the target); ensure the test sets up and tears down the symlink and uses
the same helper functions used by other tests to verify that the module handling
(the code path involving filepath.is_file()) correctly processes symlinked
directories.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 7b534e36-8dce-4c1d-a71e-5d49025019be
📒 Files selected for processing (1)
src/fromager/sources.py
| if not filepath.is_file(): | ||
| continue |
There was a problem hiding this comment.
is_file use a syscall. I have written the code in a way that reduces the amount of syscalls and I/O activity.
Can you move right before filepath.open and add a comment why it is needed?
There was a problem hiding this comment.
I'll update this commit to improve vs #1004 , which was merged
|
Improved in #1011 |
Fixes:
Pull Request Description
What
pathlib.Path.walk() lists symlinks to directories as filenames rather than dirnames. This causes scan_compiled_extensions to attempt to open() them, resulting in IsADirectoryError. Add an is_file() check to skip non-regular files before attempting to read their contents.
Why
#1001