1

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

  1. git clone --no-checkout --config advice.detachedHead=false https://github.com/google/googletest.git googletest-src
  2. cd googletest-src
  3. git 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.

2
  • 1
    Calling git config --add safe.directory ... before FetchContent_MakeAvailable should work. If you want to make sure that FetchContent_MakeAvailable will select the specific directory for clone the repo (for marking it safe), then you could specify this directory with SOURCE_DIR parameter in FetchContent_Declare call. Commented Jun 8 at 7:33
  • 1
    Note, that CMake functions invocations cannot be nested (and in CMake a function never returns a value in a common sense). So if you want to find git executable and run that executable you should do that in separate commands. E.g. find_package(Git REQUIRED) and then execute_process(COMMAND ${GIT_EXECUTABLE} config --global --verbose --add safe.directory "/mnt/c/Users/User/Project/build.Debug/_deps/googletest-src" ) Commented Jun 8 at 7:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.