Atomic append fails to create a missing file
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 70/100
Research direction
Start in _write_file_atomic, where the destination is read unconditionally when append=True before the temporary file is created. Run the reproduction in the issue with atomic=True and a missing parent directory and file, then compare with the atomic=False path. Done means the atomic append creates the missing file, a second append keeps earlier content, and the existing tests still pass.
Written by the indexing model from the issue text.
Description
Describe the bug
write_file(path, content, append=True, atomic=True) raises OSError: Invalid file path when the destination does not exist. The same call with atomic=False creates the file, consistent with append mode.
Reproduce
from pathlib import Path
from tempfile import TemporaryDirectory
import fsutil
with TemporaryDirectory() as directory:
path = str(Path(directory) / "new" / "file.txt")
fsutil.write_file(path, content="first", append=True, atomic=True)
Reproduced with Python 3.12.14 on Windows and python-fsutil 0.18.0 source at d3a77c552346dc72a081284a6fd017ecc1ba234f. No mocked filesystem or injected failure is required. This was found during a code review, rather than reported from a production application.
Expected behavior
Create the missing file, as non-atomic append does. A subsequent append should preserve its previous content.
Cause and scope
_write_file_atomic reads the destination unconditionally when append=True, before creating a temporary file. Reading a missing destination raises immediately.
This differs from #194: it does not concern errors after replacement or the Windows-specific FileNotFoundError cleanup handler. Those paths can remain unchanged.
AI assistance was used to investigate the source and prepare reproduction and tests. A focused fix is ready.
- Dominant language
- Python
- Stars
- 268
- Forks
- 19
- Avg merge
- 8d 13h
- Merged PRs (30d)
- 4
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fabiocaccamo/python-fsutil
-
Add `toml` files operation support.May be free again @fabiocaccamo claimed this 1322 days ago, and no pull request is open. Openenhancement
fabiocaccamo/python-fsutil#42 · 1 assignee ·
All issues in fabiocaccamo/python-fsutil
Similar issues
-
Path subtraction removes repeated suffix characters instead of one suffixPossibly taken @Gwerdonatus claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
joke2k/django-environ#671 ·
-
good first issue help wanted windows
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
-
test_pgbouncer deploys the server under test from Charmhub instead of the local build (track/1.31)Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
canonical/temporal-k8s-operator#195 · 1 comment ·
Maintainers usually reply within 4 days
-
`burr` fails to start when the Python path has a space in itPossibly taken @enekoreto claimed this today. Openstatus/needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 5 days
-
device support request
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
zigpy/zha-device-handlers#5461 ·
Maintainers usually reply within 4 days