4 ms·
Tell HN: TIL: GNU Make .DELETE_ON_ERROR
If you have a make recipe which creates the target file, or modifies the timestamp of the existing out-or-date target, and that recipe fails, make will not delete the target. Thus it looks up-to-date, even though the recipe failed.
The manual says: "So generally the right thing to do is to delete the target file if the recipe fails after beginning to change the file. make will do this if .DELETE_ON_ERROR appears as a target. This is almost always what you want make to do, but it is not historical practice; so for compatibility, you must explicitly request it."
So, hidden in the manual is the recommendation that, it's good practice to have
.DELETE_ON_ERROR:
somewhere in every GNU Makefile.
An example where this is relevant:
foo: bar
generate bar > foo # redirection is used
redirection will create the file or touch the timestamp of the existing one before the generate command is run. What if bar has syntax errors and generate fails? The foo file was touched and so looks updated. If you run "make foo" again, Make reports that foo is up-to-date.
With .DELETE_ON_ERROR, foo will be deleted.
If foo exists and its timestamp is not changed, then it's left alone.
If a recipe creates or touches the target, and fails due to Make being interrupted by a signal, in that case make deletes the target without any option having to be specified.
- Someone 3y agoSo, is there any shell that can do the oftentimes right thing, and delete that file for you if it created it and ‘generate’ fails? Maybe even redirect to a temp file, and rename that on success, delete it on failure (that probably makes the number of variations on this theme too high) That would make this work from other tools that run shell commands (other makes, cron, etc) (Bonus points if it also can optionally revert >>, but that’s trickier, as that doesn’t have to be to a file)
- foul 3y agoIt's nice to have, but always pen a .PHONY `clean' target to clarify what to kill before trying again after a big failure.
- rurban 3y agoThat's why you do foo: bar generate bar > tmp && mv tmp foo instead
- 10000truths 3y agoOn Linux, you can use O_TMPFILE to ensure that the temporary file won't linger around if an error occurs: foo: bar generate bar | python3 -c "import os,sys;f=os.open('.',os.O_RDWR|os.O_TMPFILE);os.write(f,sys.stdin.buffer.read());os.link(f'/proc/self/fd/{f}','./foo')"
- admax88qqq 3y agoLol. Files were a mistake.
- throwaway67743 3y agoThis is everything that's wrong with modern software. Do not introduce a pointless dependency like python because of a lack of knowledge about the build system of choice. Just use something more appropriate (that isn't python).
- 10000truths 3y agoYou're missing the point. The important part isn't the Python, it's the opening of a file with O_TMPFILE, writing to it, and linking it into the filesystem in order to guarantee an atomic result without risking leaking temporary files. If your requirements forbid the use of Python for whatever reason, you are welcome to substitute it with any other program that can perform the aforementioned operations.
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- schemescape 3y agoIs this a common practice? I ask because the approach makes sense, but I don’t recall ever seeing it before.