Failed recompaction error in add_custom_command

Hi,

I’m using following code to automatically create a dependency graph during build

    add_custom_command(
        OUTPUT ${CMAKE_BINARY_DIR}/_graphviz
        COMMAND ${CMAKE_COMMAND} "--graphviz=graphviz/deps.dot" .
        COMMAND dot -Tsvg graphviz/deps.dot -o deps.svg
        COMMAND dot -Tpng graphviz/deps.dot -o deps.png
        COMMENT "Plotting dependencies graph to deps.svg"
        DEPENDS firmware
        WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
    add_custom_target(graphviz ALL
        DEPENDS ${CMAKE_BINARY_DIR}/_graphviz)

It cause the following error:

[build] FAILED: _graphviz C:/git/x/build/debug/_graphviz 
[build] C:\WINDOWS\system32\cmd.exe /C "cd /D C:\git\x\build\debug && C:\msys64\mingw64\bin\cmake.exe --graphviz=graphviz/deps.dot . && dot -Tsvg graphviz/deps.dot -o deps.svg && dot -Tpng graphviz/deps.dot -o deps.png"
[build] -- Configuring done (1.3s)
[build] -- Generating done (4.8s)
[build] CMake Error:
[build]   Running
[build] 
[build]    'C:/msys64/mingw64/bin/ninja.exe' '-C' 'C:/git/x/build/debug' '-t' 'recompact'
[build] 
[build]   failed with:
[build] 
[build]    ninja: error: failed recompaction: Permission denied
[build] 
[build] 
[build] 
[build] CMake Error:
[build]   Running
[build] 
[build]    'C:/msys64/mingw64/bin/ninja.exe' '-C' 'C:/git/x/build/debug' '-t' 'restat' 'build.ninja'
[build] 
[build]   failed with:
[build] 
[build]    ninja: error: failed recompaction: Permission denied
[build] 
[build] 
[build] 
[build] Generate graphviz: C:/git/x/build/debug/graphviz/deps.dot
[build] CMake Generate step failed.  Build files cannot be regenerated correctly.
[build] ninja: build stopped: subcommand failed.

The error is caused by the call of ${CMAKE_COMMAND}.

How to fix it?
Is there a better way to generate the dependency graph during configuration. e.g. implicit --graphviz option?

By searching on Google, I saw some issues with the same error.
Maybe you should give your environment (espacially your CMake release).

Yes, I have also seen forum posts regarding recompaction errors, but mainly some old related to logging, which should be fixed.

I’m developing on Windows 11, with MSYS/MINGW64 environment and I’m using VSCode. CMake version is 3.29.2

Besides this problem with the recompaction error, actually my goal is to create dependency graph during configuration. I have not found a possibility to enable graphviz when using profiles. So this is a workaround to call cmake recursively just for the purpose of the graph.

It seems like there’s something that is preventing ninja from performing recompaction. Has the build been interacted with as a different user at any point? Could something else be poking the tree somehow?