[Bug] test_detect_file_encodings fails on Windows: cannot reopen an open NamedTemporaryFile
- Dominant language
- TypeScript
- Stars
- 156k
- Forks
- 24.6k
- Avg merge
- 22h 9m
- Merged PRs (30d)
- 610
Description
- [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542).
- [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general).
- [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.
### Dify version
`main` @ `b6824334cf` (source install, API 1.16.x)
### Cloud or Self Hosted
- [x] Self Hosted (Source)
### Steps to reproduce
On Windows, run:
```bash
uv run --project api pytest api/tests/unit_tests/core/rag/extractor/test_helpers.py::TestHelpers::test_detect_file_encodings
```
Failure:
```text
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\...\\Temp\\tmpxxx.txt'
```
### ✔️ Expected Behavior
The test writes a small text file and detects its encoding; `detect_file_encodings` reopens the path with `charset_normalizer.from_path`. Passes on Linux/macOS.
### ❌ Actual Behavior
The test keeps `tempfile.NamedTemporaryFile(mode="w+t")` open inside the `with` block and calls `detect_file_encodings(temp_path)` before exiting it. On Windows a still-open `NamedTemporaryFile` cannot be reopened by another handle (documented platform difference), so the production helper raises `PermissionError`. This is a test-only portability bug; the fix is to close the file before detection (`delete=False` + explicit `os.unlink` in a `finally`).
Contributor guide
Research direction
Open api/tests/unit_tests/core/rag/extractor/test_helpers.py and inspect TestHelpers::test_detect_file_encodings, focusing on the NamedTemporaryFile lifecycle. Run the reported uv pytest command first, then verify the test closes and cleans up its temporary file before detection. Done means the test passes on Windows without PermissionError and continues to pass on other platforms.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- operating-systems, testing-qa
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 84/100