FetchContent_MakeAvailable fails to clone a manually removed repository
Suppose an external dependency originating from some Git repository is specified:
FetchContent_Declare(
Foo
GIT_REPOSITORY ...
SOURCE_DIR ${CMAKE_SOURCE_DIR}/ext/foo
)
Now, in case for whatever reason the user manually removes ${CMAKE_SOURCE_DIR}/ext/foo
, running CMake results in an error along the following lines:
[ 11%] Performing update step for 'foo-populate'
CMake Error at .../tmp/foo-populate-gitupdate.cmake:25 (message):
Failed to get the hash for HEAD:
fatal: not a git repository: '.git'
make[2]: *** [CMakeFiles/foo-populate.dir/build.make:130: foo-populate-prefix/src/foo-populate-stamp/foo-populate-update] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/foo-populate.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
CMake Error at /usr/share/cmake/Modules/FetchContent.cmake:1616 (message):
Build step for dependency failed: 2
Call Stack (most recent call first):
/usr/share/cmake/Modules/FetchContent.cmake:1756:EVAL:2 (__FetchContent_directPopulate)
/usr/share/cmake/Modules/FetchContent.cmake:1756 (cmake_language)
/usr/share/cmake/Modules/FetchContent.cmake:1970 (FetchContent_Populate)
CMakeLists.txt:11 (FetchContent_MakeAvailable)
The reason for this is that the hook that checks whether a repository needs to be cloned (foo-populate-gitclone.cmake
) has some checks on meta-data in the builds directory to figure out whether it is needed to clone a repository, but fails to check whether the cloned repository is truly present on disk. So in case the meta data in the builds directory is not touched, and the local clone of the external dependency is manually removed, the Git clone hook decides that it is not needed to clone the repository.
To solve this, I suspect an additional check is needed in this Git clone hook up front to check whether the cloned repository exists on disk in conjunction with these meta data checks prior to deciding that a Git clone is not needed.