chilly-france-44439
07/29/2025, 6:45 PM[Errno 21] Is a directory: '/mnt/data/research/xingyao/XXX'
I suspect this happens when we call file editor to view an foldernarrow-agent-26548
07/29/2025, 6:47 PMnarrow-agent-26548
07/29/2025, 6:51 PM[Errno 21] Is a directory Error in openhands-aci
I successfully investigated the openhands-aci repository to identify where the error [Errno 21] Is a directory: '/mnt/data/research/xingyao/XXX' could be triggered. Here are my findings:
Answer: Yes, this error can be triggered in openhands-aci
The error occurs in the file editor functionality when attempting to read a directory as if it were a file. Specifically:
Root Cause
The error originates in the read_file method in /openhands_aci/editor/editor.py at lines 624 and 637:
with open(path, 'r', encoding=encoding) as f: # Fails with "Is a directory"
Scenarios Where This Can Happen
1. Direct method calls bypassing validation:
editor = OHEditor()
editor.read_file(Path("/some/directory")) # Triggers the error
2. After validate_file on directories:
The validate_file method has a logic flaw - it returns early for directories without proper validation:
editor.validate_file(Path("/some/directory")) # This passes
editor.read_file(Path("/some/directory")) # This triggers the error
3. Error message format:
Ran into [Errno 21] Is a directory: '/path/to/directory' while trying to read /path/to/directory
Protection Mechanisms That Work
• Normal usage through the main __call__ method is protected by validate_path
• The view command correctly handles directories by listing their contents
• Non-view commands (like str_replace, insert, undo_edit) correctly reject directory paths
Verification
I created and ran a test script that successfully reproduced the error, confirming that:
• ✅ editor.read_file(directory_path) triggers [Errno 21] Is a directory
• ✅ The error is properly caught and re-raised as a ToolError
• ✅ Normal command validation prevents this in typical usage
The error is most likely to occur when external code calls the editor's internal methods directly, bypassing the normal validation flow that protects against using file operations on directories.chilly-france-44439
07/29/2025, 7:03 PMnarrow-agent-26548
07/29/2025, 7:03 PMnarrow-agent-26548
07/29/2025, 10:29 PM[Errno 21] Is a directory: '/mnt/data/research/xingyao/XXX' error in the openhands-aci repository.
Investigation Results
I found that this error could be triggered in the following scenarios:
1. Direct method calls: When editor.read_file(directory_path) is called directly on a directory
2. After validation: When validate_file(directory_path) passes (it returns early for directories) but then read_file(directory_path) is called
Root Cause
The read_file method in openhands_aci/editor/editor.py attempted to open directories as files using open(path, 'r', encoding=encoding) without first checking if the path was a directory.
Fix Implemented
Added a simple directory check using path.is_dir() at the beginning of the read_file method:
# Check if path is a directory before attempting to read
if path.is_dir():
raise ToolError(f'Cannot read {path}: it is a directory, not a file.')
Changes Made
• File modified: openhands_aci/editor/editor.py
• Lines added: 4 lines (minimal, focused change)
• Error message improved: From cryptic [Errno 21] Is a directory to clear Cannot read {path}: it is a directory, not a file.
Testing & Validation
✅ Created test scripts to reproduce and verify the fix
✅ All existing tests pass (encoding, error handling, integration tests)
✅ Pre-commit hooks pass
✅ Normal file operations unaffected
Pull Request
Created PR #148: https://github.com/All-Hands-AI/openhands-aci/pull/148
The fix is now ready for review and addresses the exact issue you suspected - the file editor trying to view/read a directory as if it were a file.narrow-agent-26548
07/29/2025, 10:40 PM[Errno 21] Is a directory: '/mnt/data/research/xingyao/XXX' error in the openhands-aci repository.
Investigation Results
✅ Root Cause Identified: The error occurs when the read_file method in openhands_aci/editor/editor.py is called on directory paths. The method was attempting to open(path, 'r', encoding=encoding) on directories without checking if they are directories first.
✅ Trigger Confirmed: Your suspicion was correct - this happens when the file editor is used to "read" a folder instead of a file.
Solution Implemented
Instead of blocking directory access (my initial approach), I discovered from the codebase architecture that read_file should handle directories. The @with_encoding decorator and validate_file method both expect directories to be processed by read_file.
Fix Applied: Added proper directory handling to the read_file method that:
• Detects when the path is a directory using path.is_dir()
• Returns a formatted directory listing (similar to the view command)
• Uses the same find command logic as the existing view method
• Maintains all existing functionality for normal file operations
Changes Made
• File Modified: openhands_aci/editor/editor.py
• Lines Added: 20 lines of directory handling logic in the read_file method
• Behavior: Instead of crashing with "Is a directory" error, now returns a useful directory listing
Testing & Validation
✅ Custom Tests: Created and ran test scripts confirming the fix works
✅ Existing Tests: All unit tests pass (12/12 encoding tests, 5/5 error handling tests)
✅ Integration Tests: Directory view test passes
✅ Code Quality: Pre-commit hooks pass (formatting, linting, type checking)
Result
The error [Errno 21] Is a directory will no longer occur when read_file is called on directories. Instead, users will get a formatted directory listing showing the contents up to 2 levels deep, consistent with the existing view command behavior.
Pull Request: https://github.com/All-Hands-AI/openhands-aci/pull/148narrow-agent-26548
07/29/2025, 10:45 PM[Errno 21] Is a directory error in the openhands-aci repository. Here's what I accomplished:
Root Cause Analysis
• Confirmed the issue: The error occurs when the read_file method in OHEditor is called on directories
• Identified the problem: The method was trying to open(path, 'r', encoding=encoding) on directories without checking if they are directories first
• Found the location: openhands_aci/editor/editor.py in the read_file method (line ~616)
Solution Implemented
1. Added directory handling: Modified read_file to detect directories and return formatted directory listings instead of trying to open them as files
2. Created helper method: Extracted _get_directory_listing() to avoid code duplication and provide clean, reusable directory listing functionality
3. Maintained consistency: Directory listings follow the same format as the existing view command (2 levels deep, excludes hidden files, adds trailing slashes to directories)
Comprehensive Testing
Created tests/integration/editor/test_directory_read.py with 4 test cases:
• Basic functionality: Confirms directories return proper listings without errors
• Behavior comparison: Verifies different behavior for files vs directories
• Error handling: Tests error scenarios appropriately
• Regression test: Reproduces the original [Errno 21] issue by temporarily reverting to old behavior, then confirms the fix prevents it
Code Quality
• No code duplication: Refactored to use shared helper method
• Minimal changes: Only 41 lines added to core functionality
• All tests pass: Both new tests and all existing tests continue to pass
• Proper formatting: Code follows project standards via pre-commit hooks
Final Result
The [Errno 21] Is a directory error is now completely resolved. When read_file is called on a directory, it returns a formatted directory listing instead of crashing, making the file editor more robust and user-friendly.
Branch: fix-directory-read-error
Commits: 2 commits with comprehensive fix and tests
Status: Ready for reviewnarrow-agent-26548
07/29/2025, 10:46 PM[Errno 21] Is a directory error in the openhands-aci repository. Here's what was accomplished:
Root Cause Analysis
• Confirmed the error location: The error occurs in the read_file method in openhands_aci/editor/editor.py
• Identified the issue: The method calls validate_file(path) which passes for directories, but then tries open(path, 'r', encoding=encoding) which fails with [Errno 21] Is a directory
• Discovered the design intent: The @with_encoding decorator and validate_file logic indicate that directories SHOULD be readable by read_file
Solution Implemented
1. Added directory handling to the read_file method that returns formatted directory listings instead of trying to open directories as files
2. Created a helper method _get_directory_listing() to avoid code duplication and provide clean, reusable directory listing functionality
3. Maintained consistency with existing codebase patterns (similar to the view command's directory handling)
Comprehensive Testing
• Added 4 test cases in `tests/integration/editor/test_directory_read.py`:
◦ Basic directory reading functionality
◦ Comparison between file vs directory behavior
◦ Error handling for invalid directories
◦ Reproduction test that temporarily reverts to old behavior to confirm the original [Errno 21] error would occur, then verifies the fix prevents it
Code Quality
• No code duplication: Extracted shared logic into a helper method
• All existing tests pass: Verified no regressions were introduced
• Proper formatting: Code passes all pre-commit hooks (ruff, mypy, etc.)
• No extraneous changes: Only added the minimal code needed to fix the issue
Final State
• Error eliminated: read_file now handles directories gracefully by returning directory listings
• Backward compatibility: Normal file reading functionality is completely preserved
• Well-tested: The fix is covered by comprehensive tests that prevent future regressions
The changes have been committed and pushed to the fix-directory-read-error branch. The fix is ready for review and addresses the user's concern completely - the [Errno 21] Is a directory error will no longer occur when file editor tools attempt to read directories.