tl;dr during cmake command FetchContent_MakeAvailable, an underlying git checkout tag proces fails with fatal: detected dubious ownership in repository at ...; how can I have cmake set a git safe.directory before the git checkout process?
problem
Given the following cmake commands
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.17.0
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(googletest)
The cmake command FetchContent_MakeAvailable runs these processes
git clone --no-checkout --config advice.detachedHead=false https://github.com/google/googletest.git googletest-srccd googletest-srcgit checkout v1.17.0
However the git checkout process fails with error
Cloning into 'googletest-src'...
warning: templates not found in /usr/share/git-core/templates
fatal: detected dubious ownership in repository at '/mnt/c/Users/User/Project/build.Debug/_deps/googletest-src'
To add an exception for this directory, call:
git config --global --add safe.directory /mnt/c/Users/User/Project/build.Debug/_deps/googletest-src
CMake Error at build.Debug/CMakeFiles/fc-tmp/googletest/googletest-gitclone.cmake:61 (message):
Failed to checkout tag: 'v1.17.0'
Call Stack (most recent call first):
build.Debug/CMakeFiles/fc-tmp/googletest/download.cmake:7 (include)
/usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1753 (include)
/usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1706 (__FetchContent_doStepDirect)
/usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1607 (__FetchContent_populateDirect)
/usr/local/share/cmake-3.30/Modules/FetchContent.cmake:2145:EVAL:2 (__FetchContent_doPopulation)
/usr/local/share/cmake-3.30/Modules/FetchContent.cmake:2145 (cmake_language)
/usr/local/share/cmake-3.30/Modules/FetchContent.cmake:2384 (__FetchContent_Populate)
CMakeLists.txt:104 (FetchContent_MakeAvailable)
-- Configuring incomplete, errors occurred!
How can I instruct cmake to set a git safe directory before the git checkout command?
This is probably occurring because the path /mnt/c/Users/User/Project/build.Debug/_deps/googletest-src is a Windows NTFS disk. But the cmake process is running in Ubuntu 24 WSL.
fix attempt #1
I added the following cmake command to call git config --add safe.directory ...
execute_process(
COMMAND find_package(Git REQUIRED) config --global --verbose --add safe.directory "/mnt/c/Users/User/Project/build.Debug/_deps/googletest-src"
)
so the cmake commands were
FetchContent_Declare(...)
execute_process(...)
FetchContent_MakeAvailable(...)
but this did not fix the fatal error.
git config --add safe.directory ...beforeFetchContent_MakeAvailableshould work. If you want to make sure thatFetchContent_MakeAvailablewill select the specific directory for clone the repo (for marking it safe), then you could specify this directory withSOURCE_DIRparameter inFetchContent_Declarecall.find_package(Git REQUIRED)and thenexecute_process(COMMAND ${GIT_EXECUTABLE} config --global --verbose --add safe.directory "/mnt/c/Users/User/Project/build.Debug/_deps/googletest-src" )