How recipes execute
One shell per line
Each recipe line runs in its own shell process. State doesn't carry between lines:
bad:
cd app
npm test # runs in the ORIGINAL directory — cd was lost
good:
cd app && npm ci && npm test
also-good:
cd app; \
npm ci; \
npm test # backslash-continued: one shell
Or make every recipe a single shell with .ONESHELL: (all lines of a recipe go to one shell — then add set -e behaviour via .SHELLFLAGS).
Shell and error behaviour
- Default shell is
/bin/sh(dash on Ubuntu — no bash-isms like[[ ]]or arrays). SetSHELL := bashand.SHELLFLAGS := -eu -o pipefail -cfor safer recipes. - make stops at the first recipe line that exits non-zero ("Error 1"). But within one line like
cmd1; cmd2, a failure ofcmd1is hidden unless the shell has-e— another reason for.SHELLFLAGS. .DELETE_ON_ERROR:deletes a half-written target file if its recipe fails, so a corrupt artifact isn't treated as up to date next time.
Prefixes
| Prefix | Effect |
|---|---|
@ |
Don't echo the command before running it |
- |
Ignore errors from this line |
+ |
Run even under make -n (used for $(MAKE) calls) |
$ in recipes
Make expands $... before the shell sees it. To pass a literal $ to the shell, double it:
list:
@for f in charts/*; do echo "linting $$f"; helm lint $$f; done
@echo "home is $$HOME, app is $(APP)"
$(APP) is a make variable; $$f and $$HOME reach the shell as $f and $HOME.
Recursive make
SERVICES := orders payments catalog
.PHONY: test-all $(SERVICES)
test-all: $(SERVICES)
$(SERVICES):
$(MAKE) -C services/$@ test
Always use $(MAKE), not make: it passes flags like -j, -n, -k and the jobserver to the sub-make. --no-print-directory (or MAKEFLAGS += --no-print-directory) silences "Entering directory" noise.
Parallel builds
make -j8 runs independent targets concurrently. If a build works serially but fails in parallel, a dependency is missing — two targets need an order that you never declared (for example, docker-build assumes generate ran first but doesn't list it). Fix by declaring the prerequisite, or mark a file .NOTPARALLEL: as a last resort.
include
include common.mk # shared targets/variables across repos
-include .env # optional include (no error if missing) — loads KEY=VALUE lines as make vars
Teams sometimes vendor a shared common.mk from a platform repo so every service gets the same help, lint, docker-* targets.