Skip to content
Latchkey

Ninja "missing and no known rule to make it" in CI

Ninja needs an input file to build a target, but that file does not exist and no rule in the build graph produces it. The referenced source was deleted, never generated, or the stale build.ninja predates a file rename.

What this error means

The build stops with "ninja: error: 'src/foo.cpp', needed by 'CMakeFiles/app.dir/src/foo.cpp.o', missing and no known rule to make it", pointing at an input Ninja cannot obtain.

ninja
ninja: error: 'src/generated/version.h', needed by
'CMakeFiles/app.dir/src/main.cpp.o', missing and no known rule to make it.

Diagnose it: configure step, not the build step

Most CMake failures in CI happen during configure, where a package or compiler feature is not found, and then surface later as a confusing compile or link error.

Terminal
cmake --version && cc --version
cmake -S . -B build --debug-find 2>&1 | grep -i "not found" | head -20
cmake -S . -B build -LAH | head -40   # cache variables actually in effect

Common causes

A referenced source was removed or renamed

CMakeLists.txt still lists a file that no longer exists, and the stale build graph expects it.

A generated file has no producing rule

A header meant to be generated is listed as an input, but the generator step is missing or ran out of order.

How to fix it

Reconfigure so the build graph matches the sources

  1. Fix the file list in CMakeLists.txt to match what exists on disk.
  2. Delete the build directory so Ninja regenerates build.ninja.
  3. Re-run configure and build against the corrected graph.
Terminal
rm -rf build
cmake -S . -B build -G Ninja
cmake --build build

Add the rule that generates the file

If the input is generated, declare an add_custom_command so Ninja knows how to produce it before it is consumed.

CMakeLists.txt
add_custom_command(
  OUTPUT ${CMAKE_BINARY_DIR}/version.h
  COMMAND ${CMAKE_COMMAND} -P gen_version.cmake)

How to prevent it

  • Reconfigure after adding, removing, or renaming source files.
  • Do out-of-source builds so a clean rebuild is a single directory delete.
  • Declare generated inputs with explicit OUTPUT rules.

Frequently asked questions

What causes Ninja "missing and no known rule to make it" in CI?
There are 2 common causes: a referenced source was removed or renamed and a generated file has no producing rule. CMakeLists.txt still lists a file that no longer exists, and the stale build graph expects it.
How do I fix Ninja "missing and no known rule to make it" in CI?
There are 2 fixes depending on which cause you have: reconfigure so the build graph matches the sources and add the rule that generates the file. Work through them in order, since the first is the most common.
What does Ninja "missing and no known rule to make it" in CI actually mean?
The build stops with "ninja: error: 'src/foo.cpp', needed by 'CMakeFiles/app.dir/src/foo.cpp.o', missing and no known rule to make it", pointing at an input Ninja cannot obtain.
How do I stop Ninja "missing and no known rule to make it" in CI happening again?
Reconfigure after adding, removing, or renaming source files. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card