CMake ignores missing required package

I have a required dependency Bar. Even if Bar is not found, CMake finishes configuring without error.

CMakeLists.txt

cmake_minimum_required(VERSION 3.27)
project(Foo)

set(CMAKE_MODULE_PATH "${Foo_SOURCE_DIR}")
find_package(Bar REQUIRED)
message(NOTICE "${Bar_FOUND}")

FindBar.cmake

set(${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)
message(NOTICE "Set Bar not found")

CMake output:

$ cmake --fresh .
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/local/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Set Bar not found
FALSE
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/foo

Is this expected? Is FindPackageHandleStandardArgs needed to check Bar_FOUND? Does this behavior differ between Find Modules and config files?

Maybe CMake finds a different instance of Bar? Does configuring with the --debug-find provide any more information?

Using --debug-find confirms that CMake does not find the package.

-- Detecting CXX compile features
-- Detecting CXX compile features - done
Set Bar not found
CMake Debug Log at CMakeLists.txt:5 (find_package):
  find_package considered the following paths for FindBar.cmake:

    /usr/local/share/cmake-3.27/Modules/FindBar.cmake

  The file was found at

    /tmp/foo/FindBar.cmake

  The module is considered not found due to Bar_FOUND being FALSE.


FALSE
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/foo

Changing Bar to a different string throughout also does not help.

This is certainly interesting. I’ll look into it…

Ah. The “standard” behavior is actually implemented by a call to find_package_handle_standard_args (called FPHSA) for Find modules. I hadn’t expected that, but it seems that is how it works…