1

I am in a situation where I want to do something is a target returns error. My code is:

pre-submit:
    cp test.csv test.csv.bk
    make test.csv
    make lint
    rm test.csv.bk

This run just fine. But there can be cases when make lint or make test.csv through an error and in that case I want to remove test.csv (if any) and rename test.csv.bk to test.csv

I couldn't find if we can catch error raised from target

Edit: lint looks like this:

.PHONY: lint
lint: | env
    ./env/bin/python lint.py
codingenious
  • 8,385
  • 12
  • 60
  • 90
  • 1
    could be easy for a shell script to check each `make` exit status and conditionally rm/mv files; replace some/all recipe lines with running that script – Milag Jul 21 '20 at 11:58
  • 1
    You should never, ever use raw `make` to run a sub-make. Always use `$(MAKE)` (or `${MAKE}` if you prefer, they are the same). – MadScientist Jul 21 '20 at 16:56
  • What does `make lint` look like? Got a strong feeling these is a much better solution in there somewhere, struggling to get out... :-) – Andreas Jul 22 '20 at 14:14
  • It calls a python file for csv file validation. – codingenious Jul 23 '20 at 10:49

1 Answers1

2

One way to do what you want is to use the shell:

pre-submit:
    cp test.csv test.csv.bk
    (make test.csv && make lint) || mv test.csv.bk test.csv

But you could also use Make itself:

pre-submit: test.csv.new lint
    mv test.csv test.csv.bk
    mv test.csv.new test.csv
Beta
  • 96,650
  • 16
  • 149
  • 150
  • One thing about the first example `pre-submit`: you're losing the failure code so this is probably not right. This makefile will report success even if the sub-make's failed. – MadScientist Jul 21 '20 at 16:56
  • And, you should use `mv -f` here :) – MadScientist Jul 21 '20 at 16:57
  • @MadScientist: Reporting failure in that case wasn't part of the requirements, but if it's desired it's easy to add with `(mv ... && false)`. Likewise I'm aware that `$(MAKE)` is better than `make`, but I like to keep examples simple while the main design choice is still up in the air. (And I'm not going to get drawn into a fight over `mv`, not on a beautiful day like this.) – Beta Jul 22 '20 at 20:07
  • Well, the original behavior would definitely fail if make failed, so implementing a solution that has feature-parity with the original question seems like it should take into account error handling. YMMV. – MadScientist Jul 23 '20 at 12:52
  • @MadScientist: You've lost me. There are certainly good reasons why `pre-submit` ought to return an error code if `test.csv` or `lint` does, but given that the whole point of the exercise was to devise code that would behave differently from OP's code, faithful reproduction of OP's code's behavior is not one of them. – Beta Jul 23 '20 at 22:42
  • The way I understood the question was, the OP wanted to write a recipe that invoked some sub-makes that might modify the `test.csv` file, and if the sub-makes failed they wanted to put back the original `test.csv` file but still propagate the failure out to the user. A kind of "cleanup on failure" operation. I agree that I am making an assumption and it would be much better if these requirements were stated explicitly in the question. – MadScientist Jul 24 '20 at 18:16