# CMake presets issue

**URL:** <https://discourse.cmake.org/t/cmake-presets-issue/12700>\
**Category:** Development\
**Created:** [October 7, 2024, 9:08am UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700 "2024-10-07T09:08:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)\
**Post date:** [October 7, 2024, 9:08am UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/1 "2024-10-07T09:08:26Z")

</div>

Hi, I have the following CMakePresets.json:

```auto
{
    "version": 5,
    "configurePresets": [
        {
            "name": "default",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/build",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "${presetName}"
            }
        },
        {
            "name": "Debug",
            "inherits": "default"
        },
        {
            "name": "Release",
            "inherits": "default"
        }
    ],
    "buildPresets": [
        {
            "name": "default",
            "hidden": true,
            "configurePreset": "${presetName}"
        },
        {
            "name": "Debug",
            "inherits": "default"
        },
        {
            "name": "Release",
            "inherits": "default"
        }
    ]
}

```

However, when I run cmake --preset Debug I get the following error:

```auto
CMake Error: Could not read presets from C:/Users/martin/cmake_test:
Invalid "configurePreset": "Debug"

```

What am I doing wrong here?

Thanks.

---

<div class="post-metadata">

**Author:** ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)\
**Post date:** [October 7, 2024, 1:50pm UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/2 "2024-10-07T13:50:38Z")

</div>

This is quite strange. I see that if I manually expand the macros, it works just fine:

```
"buildPresets": [
    {
        "name": "Debug",
        "configurePreset": "Debug"
    },
    {
        "name": "Release",
        "configurePreset": "Release"
    }
]

```

Maybe this is a CMake bug? I’m using CMake 3.30.4. The same thing happens if I change the JSON schema version to 9.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 8, 2024, 6:11am UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/3 "2024-10-08T06:11:15Z")

</div>

### Yes, I can confirm, [macro expansion](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#macro-expansion) is magic and a night mare!

This does also not work:

```json
{
    "version": 9,
    "configurePresets": [
        {
            "name": "default",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/build",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "${presetName}"
            },
            "environment": {
                "PRESET_NAME": "${presetName}"
            }
        },
        {
            "name": "Debug",
            "inherits": "default"
        },
        {
            "name": "Release",
            "inherits": "default"
        }
    ],
    "buildPresets": [
        {
            "name": "default",
            "hidden": true,
            "configurePreset": "$env{PRESET_NAME}"
        },
        {
            "name": "Debug",
            "inherits": "default"
        },
        {
            "name": "Release",
            "inherits": "default"
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 8, 2024, 7:02am UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/4 "2024-10-08T07:02:57Z")

</div>

… and it should be possible to use the **DRY** principle: `Don’t repeat yourself`

```json
{
    "version": 9,
    "configurePresets": [
        {
            "name": "default",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/build",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "${presetName}"
            },
            "environment": {
                "PRESET_NAME": "${presetName}"
            }
        },
        {
            "name": "Debug",
            "inherits": "default"
        },
        {
            "name": "Release",
            "inherits": "default"
        }
    ],
    "buildPresets": [
        {
            "name": "Debug",
            "configurePreset": "Debug"
        },
        {
            "name": "Release",
            "configurePreset": "Release"
        }
    ],
    "testPresets": [
        {
            "name": "Debug",
            "configurePreset": "Debug"
        },
        {
            "name": "Release",
            "configurePreset": "Release"
        }

    ],
    "workflowPresets": [
        {
            "name": "Debug",
            "steps": [
                {
                    "type": "configure",
                    "name": "Debug"
                },
                {
                    "type": "build",
                    "name": "Debug"
                },
                {
                    "type": "test",
                    "name": "Debug"
                }
            ]
        },
        {
            "name": "Release",
            "steps": [
                {
                    "type": "configure",
                    "name": "Release"
                },
                {
                    "type": "build",
                    "name": "Release"
                },
                {
                    "type": "test",
                    "name": "Release"
                }
            ]
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)\
**Post date:** [October 8, 2024, 7:16am UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/5 "2024-10-08T07:16:23Z")

</div>

Hi @ClausKlein thanks for the response. I’ve changed this topic to a bug report/feature request.

---

<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:** [October 8, 2024, 9:27pm UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/6 "2024-10-08T21:27:40Z")

</div>

Macro expansion is only supported in the fields where the documentation explicitly says it is supported. If it is not mentioned in a particular field’s documentation, it is not supported.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 8, 2024, 9:39pm UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/7 "2024-10-08T21:39:52Z")

</div>

How would you solve the **DRY** problem ?

---

<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:** [October 8, 2024, 9:41pm UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/8 "2024-10-08T21:41:14Z")

</div>

Currently you can’t. It’s a well-known, much discussed problem. [https://gitlab.kitware.com/cmake/cmake/-/issues/22538](https://gitlab.kitware.com/cmake/cmake/-/issues/22538)

---

<div class="post-metadata">

**Author:** ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)\
**Post date:** [October 9, 2024, 7:31am UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/9 "2024-10-09T07:31:35Z")

</div>

Hi, thanks for the response. The interesting thing is that the error message actually shows that the macro was expanded at some point, i.e. it says `Invalid "configurePreset": "Debug"`. If the expansion wasn’t done at all, I would’ve expected something like `Invalid "configurePreset": "${presetname}"`. So the string `"Debug"` is there at one point, it’s just that it’s not being interpreted as such.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [October 11, 2024, 8:37pm UTC](https://discourse.cmake.org/t/cmake-presets-issue/12700/10 "2024-10-11T20:37:55Z")

</div>

I have a `cmake preset pattern` to work with only 2 `workflow presets` on just each `hostSystem`:

> <https://github.com/ClausKlein/cpp-lib-template/blob/develop/CMakePresets.json>
