CMake "Flow control statements are not properly nested" in CI
By Daniel Zoghalchali·Latchkey
CMake requires every if(), foreach(), while(), function(), and macro() to be matched by its end* command. A missing or misordered closer leaves the parser with unbalanced blocks.
What this error means
Configure fails reporting flow control statements are not properly nested, naming the file and the unmatched construct, usually after editing a CMakeLists block.
cmake
CMake Error at CMakeLists.txt:24 (if):
Flow control statements are not properly nested.
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
How to fix it
Balance every block
Match each if/foreach/function with exactly one closer in order.
Indent blocks consistently so missing closers are easy to spot.
Close every CMake flow-control block with its matching end* command and keep blocks indented so imbalances are visible.
Frequently asked questions
How do I fix CMake "Flow control statements are not properly nested" in CI?
Balance every block. Match each if/foreach/function with exactly one closer in order.
What does CMake "Flow control statements are not properly nested" in CI actually mean?
Configure fails reporting flow control statements are not properly nested, naming the file and the unmatched construct, usually after editing a CMakeLists block.
How do I stop CMake "Flow control statements are not properly nested" in CI happening again?
Close every CMake flow-control block with its matching end* command and keep blocks indented so imbalances are visible.