# Failed recompaction error in add\_custom\_command

**URL:** https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762
**Category:** Code
**Tags:** os:windows, gen:ninja
**Created:** [April 30, 2024, 8:28am UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762 "2024-04-30T08:28:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ra-kal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ra-kal/32/4491_2.png) [@ra-kal](https://discourse.cmake.org/u/ra-kal)
#### Post date: [April 30, 2024, 8:28am UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/1 "2024-04-30T08:28:00Z")

</div>

Hi,

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

```auto
    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:

```auto
[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?

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [May 6, 2024, 8:04pm UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/2 "2024-05-06T20:04:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ra-kal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ra-kal/32/4491_2.png) [@ra-kal](https://discourse.cmake.org/u/ra-kal)
#### Post date: [May 14, 2024, 8:53am UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/3 "2024-05-14T08:53:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 18, 2024, 12:56pm UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/4 "2024-05-18T12:56:27Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![gblikas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gblikas/32/4970_2.png) [@gblikas](https://discourse.cmake.org/u/gblikas)
#### Post date: [September 27, 2024, 10:16pm UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/5 "2024-09-27T22:16:15Z")

</div>

I’m facing the same issue with

```auto
cmake --version
cmake version 3.30.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

```

and

```auto
ninja --version
1.12.1

```

My error is also occurring by doing the following,

```auto
add_custom_command(
    OUTPUT ${GRAPHVIZ_OUTPUT_FILE}
    COMMAND ${CMAKE_COMMAND} --graphviz=${GRAPHVIZ_OUTPUT_FILE} ${CMAKE_BINARY_DIR}
    COMMENT "Generating Graphviz .dot file"
)

```

I’ve tried cleaning my build directory, re-buildng from scratching, installing and uninstalling ninja and cmake but nothing seems to work.

Other important things:

- running windows 11 inside parallels
- installed latest graphviz via `Download_windows` zip:

```auto
def install_graphviz_windows():
    graphviz_url = "https://graphviz.gitlab.io/_pages/Download/Download_windows.html"
    subprocess.run(["winget", "install", "-e", "--id", "Graphviz.Graphviz"], check=True)

```

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 28, 2024, 2:03am UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/6 "2024-09-28T02:03:20Z")

</div>

You’re trying to re-run CMake during the build in a recursive way. I would recommend you don’t do that. In some build systems, that fails (I think Xcode rejects it now, it didn’t in the past).

In the next CMake feature release (3.31), CMake presets will gain support for a `graphviz` field in the configure presets. That’s a better way to incorporate running graphviz with your configuration step by default.

---

<div class="post-metadata">

### Author: ![gblikas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gblikas/32/4970_2.png) [@gblikas](https://discourse.cmake.org/u/gblikas)
#### Post date: [September 30, 2024, 7:09pm UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/7 "2024-09-30T19:09:17Z")

</div>

> In the next CMake feature release (3.31), CMake presets will gain support for a graphviz field in the configure presets. That’s a better way to incorporate running graphviz with your configuration step by default.

Gotcha; that sounds much needed!

> You’re trying to re-run CMake during the build in a recursive way. I would recommend you don’t do that. In some build systems, that fails (I think Xcode rejects it now, it didn’t in the past).

Isn’t the PID different? Why would that cause a recursive-like execution?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 30, 2024, 10:08pm UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/8 "2024-09-30T22:08:32Z")

</div>

In the Xcode case, I think it detects a recursive call (yes, including separate processes) and it fails with some kind of locking-related failure. I don’t recall the details, sorry.

---

<div class="post-metadata">

### Author: ![gblikas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gblikas/32/4970_2.png) [@gblikas](https://discourse.cmake.org/u/gblikas)
#### Post date: [October 3, 2024, 5:48pm UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/9 "2024-10-03T17:48:40Z")

</div>

Eagerly awaiting 3.31 then!

---

<div class="post-metadata">

### Author: ![gblikas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gblikas/32/4970_2.png) [@gblikas](https://discourse.cmake.org/u/gblikas)
#### Post date: [October 25, 2024, 10:05pm UTC](https://discourse.cmake.org/t/failed-recompaction-error-in-add-custom-command/10762/10 "2024-10-25T22:05:20Z")

</div>

Update: 3.31 is in Release Candidate state.
