FetchContent: Quickly hits MAXPATH limit on Windows
Problem
The name of a FetchContent
project is used four times in the file-path that is generated by the populate step of FetchContent
, which quickly exceeds the 260 characters limit for paths on Windows.
In general, that should not be a problem, because modern Windows allows to exceeds that limit (by setting a registry key). However, the used programs must support it. And sadly, ninja
does not do it and bails out if a path exceeds the 260 character limit for file-paths.
Solution
Do not use the name of the FetchContent
project multiple times in the generated file-path and possibly use CMAKE_OBJECT_PATH_MAX
.
Example to demonstrate the problem
Create the following CMakeLists.txt
file and configure it using the Ninja
configurator in a Visual Studio Developer Command-Prompt:
...
cmake_minimum_required( VERSION 3.25 )
project( Maxpath_FetchContent_Problem )
include( FetchContent )
FetchContent_Declare( quite_long_name_for_fetchcontent_project
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
)
FetchContent_MakeAvailable( quite_long_name_for_fetchcontent_project )
This results in:
C:\Users\dbahadir\workspace\BUILD@Maxpath_FetchContent_Problem>cmake -G "Ninja Multi-Config" --toolchain ..\toolchains\x86_64-windows-msvc.clang.llvm-only.cmake -S ..\Maxpath_FetchContent_Problem -B .
-- The C compiler identification is Clang 15.0.1 with GNU-like command-line
-- The CXX compiler identification is Clang 15.0.1 with GNU-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/Llvm/x64/bin/clang.exe - 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: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/Llvm/x64/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
ninja: error: Stat(C:/Users/dbahadir/workspace/BUILD@Maxpath_FetchContent_Problem/_deps/quite_long_name_for_fetchcontent_project-subbuild/quite_long_name_for_fetchcontent_project-populate-prefix/src/quite_long_name_for_fetchcontent_project-populate-stamp/Debug/quite_long_name_for_fetchcontent_project-populate-done): Filename longer than 260 characters
CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.26/Modules/FetchContent.cmake:1622 (message):
Build step for quite_long_name_for_fetchcontent_project failed: 1
Call Stack (most recent call first):
C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.26/Modules/FetchContent.cmake:1762:EVAL:2 (__FetchContent_directPopulate)
C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.26/Modules/FetchContent.cmake:1762 (cmake_language)
C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.26/Modules/FetchContent.cmake:1976 (FetchContent_Populate)
CMakeLists.txt:19 (FetchContent_MakeAvailable)
-- Configuring incomplete, errors occurred!
The error even gets worse if you increase the length of the name given to FetchContent
, because then somehow retrieving the source code does not even work.