"write error: stdout" when calling "make" from Makefile

This is my Makefile:

SHELL=/bin/bash
.SHELLFLAGS = -e -o pipefail -c 

env:
    make -version | head -1

This is what I'm getting on Ubuntu 20.04.3:

make -version | head -1
GNU Make 4.2.1
make[1]: write error: stdout
make: *** [Makefile:4: env] Error 1

What's wrong?


I don't see this problem with GNU make 4.3, so I guess something was changed there to allow this to work.

To fix it you can try changing your rule to this:

env:
        make -version | cat | head -1

The error is because the head program closes its stdin when it's read the first line, then make complains because it fails to write to its stdout (because it was closed).

By introducing the cat here, the cat reads all the input from make without closing the pipe so make doesn't get any error.

PS. You should always, always use the variable $(MAKE) when invoking a sub-make, never the hardcoded make.