Read Before You Truncate
A scripted in-place edit reads the whole file into a variable, runs its checks and replacements, and only then opens the file for writing.
Why
A file survives a scripted edit, uncommitted work included.
How To Apply
- Python:
s = open(p).read(), assert, transform, thenwith open(p, 'w') as f: f.write(new). - Never redirect a command's output onto a file the same command reads.
- A file with uncommitted work that isn't yours: snapshot it to the scratchpad before the first scripted edit.
- A new file written from a heredoc (
cat > f <<EOF) clobbers a file of the same name without a word: runset -o noclobberfirst, orlsthe target names, so a clash fails loud. open(p,'w').write(fix(open(p).read()))opens for writing first, which empties the file: the read sees nothing, the assert fails, and the file stays empty, taking its uncommitted work with it.