makefiles writing to $@ should first write to $@.new

Started by Justin Pryzbyalmost 4 years ago5 messages
#1Justin Pryzby
pryzby@telsasoft.com

There are many Makefile rules like

foo: bar
./tool $< > $@

If the rule is interrupted (due to ^C or ENOSPC), foo can be 0 bytes or
partially written, but won't be rebuilt until someone runs distclean or debugs
it and removes the individual file, as I did for errcodes.h.

It'd be better if these did

./tool $< > $@.new
mv $@.new $@

--
Justin

#2Michael Paquier
michael@paquier.xyz
In reply to: Justin Pryzby (#1)
Re: makefiles writing to $@ should first write to $@.new

On Sun, Jan 23, 2022 at 09:23:05PM -0600, Justin Pryzby wrote:

If the rule is interrupted (due to ^C or ENOSPC), foo can be 0 bytes or
partially written, but won't be rebuilt until someone runs distclean or debugs
it and removes the individual file, as I did for errcodes.h.

Honestly, I am not sure that this worth bothering about. This comes
down to a balance between the code complexity and the likelihood of a
failure, and the odds are not in favor of the later IMO. Now, it
could be perhaps possible to make such a change simple enough while it
avoids a lot of technical debt, but we have a lot of custom rules
particularly in src/bin/, so changing all that or even require that in
future changes is not really appealing.
--
Michael

#3Julien Rouhaud
rjuju123@gmail.com
In reply to: Michael Paquier (#2)
Re: makefiles writing to $@ should first write to $@.new

Hi,

On Mon, Jan 24, 2022 at 12:41:49PM +0900, Michael Paquier wrote:

On Sun, Jan 23, 2022 at 09:23:05PM -0600, Justin Pryzby wrote:

If the rule is interrupted (due to ^C or ENOSPC), foo can be 0 bytes or
partially written, but won't be rebuilt until someone runs distclean or debugs
it and removes the individual file, as I did for errcodes.h.

Honestly, I am not sure that this worth bothering about. This comes
down to a balance between the code complexity and the likelihood of a
failure, and the odds are not in favor of the later IMO. Now, it
could be perhaps possible to make such a change simple enough while it
avoids a lot of technical debt, but we have a lot of custom rules
particularly in src/bin/, so changing all that or even require that in
future changes is not really appealing.

I agree, it doesn't seem worth it.

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Julien Rouhaud (#3)
Re: makefiles writing to $@ should first write to $@.new

Julien Rouhaud <rjuju123@gmail.com> writes:

On Mon, Jan 24, 2022 at 12:41:49PM +0900, Michael Paquier wrote:

Honestly, I am not sure that this worth bothering about. This comes
down to a balance between the code complexity and the likelihood of a
failure, and the odds are not in favor of the later IMO. Now, it
could be perhaps possible to make such a change simple enough while it
avoids a lot of technical debt, but we have a lot of custom rules
particularly in src/bin/, so changing all that or even require that in
future changes is not really appealing.

I agree, it doesn't seem worth it.

Agreed. Another reason to not bother about this is the likelihood
that it'd all be wasted effort as soon as we switch to meson.
If that project fails, I'd be open to revisiting this issue;
but I don't think we should spend time improving the Makefiles
right now.

regards, tom lane

#5Peter Eisentraut
peter.eisentraut@enterprisedb.com
In reply to: Justin Pryzby (#1)
Re: makefiles writing to $@ should first write to $@.new

On 24.01.22 04:23, Justin Pryzby wrote:

There are many Makefile rules like

foo: bar
./tool $< > $@

If the rule is interrupted (due to ^C or ENOSPC), foo can be 0 bytes or
partially written, but won't be rebuilt until someone runs distclean or debugs
it and removes the individual file, as I did for errcodes.h.

If a rule fails, make removes the target file. So I don't see how this
can happen unless you hard kill -9 make or something like that.