Interview questions and model answers
"Why does make test say 'up to date' and not run my tests?"
Because there's a file or directory called test — make treats targets as files, sees it exists with no newer prerequisites, and skips the recipe. I'd add test to .PHONY so it always runs, and generally declare every task-style target phony.
"What's the difference between = and :=?"
= is recursively expanded — evaluated every time the variable is used, so $(shell ...) runs repeatedly and you can get recursion errors. := expands once at definition. For things like GIT_SHA := $(shell git rev-parse --short HEAD) I use :=. ?= sets a default only if the variable isn't already set by the environment or command line, which is how CI overrides values.
"Why doesn't cd work across recipe lines?"
Each recipe line runs in a separate shell, so the directory change is lost when that shell exits. Chain commands with && on one line, use backslash continuations, or .ONESHELL. Similarly, a $ meant for the shell must be written $$.
"Parallel make -j breaks the build but serial works. Why?"
There's an undeclared dependency — in serial mode targets happened to run in file order. With -j, independent targets run concurrently, exposing the missing edge. I'd find which target needs another's output and add it as a prerequisite (or an order-only prerequisite for directories).
"How would you design a Makefile for a team's service?"
A self-documenting help as the default goal, phony task targets (dev, lint, test, build, docker-build, docker-push, scan, deploy, clean), real file targets for expensive build outputs, overridable defaults with ?=, strict bash shell flags, guards that require ENV and refuse to default to prod, no secrets — credentials from the environment or IAM role — and the same targets called from CI. For reproducibility, tools pinned or run in a toolchain container.
"Why use $(MAKE) instead of make in recipes?"
$(MAKE) propagates the original invocation's flags — -j with the jobserver, -n for dry runs, -k — to the sub-make, and make treats such lines specially so dry runs still recurse correctly.
"Makefile or bash script?"
A bash script is fine for a linear sequence. A Makefile gives named entry points, dependencies between tasks, incremental rebuilds, parallelism and a discoverable help. In practice I combine them: the Makefile is the interface; complex logic lives in scripts in scripts/ that targets call.