Makefile .PHONY Command Reference
Mark task targets as phony so make always runs their recipes.
.PHONY tells make that a target is a task, not a file. Without it, a target named test silently does nothing if a file or directory called test exists.
What it does
Declaring a target as a prerequisite of .PHONY makes make skip the up-to-date file check and always run the recipe. This is essential for task targets like build, test, clean, and lint that do not produce a file of the same name.
Common flags and usage
- .PHONY: name1 name2 ...: declare one or more phony targets
- Apply to every non-file task: build, test, clean, lint, install
- Without it, a same-named file or directory makes the target a no-op
Example
shell
.PHONY: build test clean
test:
go test ./...
clean:
rm -rf dist/In CI
A missing .PHONY is a classic flaky pipeline: a test target appears to pass but make reported nothing to be done because a test/ directory exists. Mark every task target phony so CI steps always execute.
Key takeaways
- .PHONY forces make to run a target even if a like-named file exists.
- Mark every task target (build, test, clean, lint) phony.
- A missing .PHONY causes silent nothing to be done no-ops in CI.
Related guides
make Command ReferenceReference for make in CI/CD: how make reads a Makefile, builds the default or a named target, and which envir…
make Targets Command ReferenceReference for make targets in CI/CD: what a target is, how to run several in one invocation, and how to disco…
make -C (Directory) Command ReferenceReference for make -C in CI/CD: change into a directory before reading its Makefile, the clean way to build a…