Path names longer than MAX_PATH fail on Windows, even if long paths support is enabled
I am having multiple issues due to including out-of-source dependencies in a CMake project. As CMake uses the absolute path of those, the final path length exceeds the Win32 260-character limit, even when the system is configured to support longer paths. Some file operations pass with such a long path, while others fail with seemingly unrelated messages.
Environment tested
- CMake version: 3.29
- Operating System: Windows 11 23H2 22631.3447
- System has long path support enabled, and verified working
- Generators: Ninja, MinGW Makefiles and Visual Studio 2022
Minimally reproducible example
- Create an empty CMake project
- Set the build folder to a long name during configuration
- I've used
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
, 255-character long.
- Configuration should fail with an error message of the form
TryCompile
withFailed to set working directory to <folder>: No such file or directory
.
Observations
- By debugging and pausing on the error, I observed that, during the execution,
cmSystemTools::FileIsDirectory
returns true, and there are files inside the folder. SoMkdir
and other operations worked there correctly, as expected. It always fails at the change of working directory. - Microsoft documents that, by adding
longPathAware
to the application manifest, we should be able to remove this limit for theSetCurrentDirectoryW
function. I have tried replacing_wchdir
withSetCurrentDirectoryW
oncmSystemTools::ChangeDirectory
, also adding the clause to thecmake.version.manifest
file, and even tried usingEncoding::ToWindowsExtendedPath
instead ofEncoding::ToWide
in conjunction with those, but with no success. - Some discussion in this StackOverflow thread seems to also have issues, though this GitHub issue from Microsoft indicates that it should work with those steps before. As I was not able to do so, I am opening this issue instead.
- Although
TryCompile
is not the only issue I encountered, it is the most easily reproducible, and should hopefully point out to a possible solution for the file moving/renaming issue caused by the same path length reason.