CMake presets issue

Hi, I have the following CMakePresets.json:

{
    "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:

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

What am I doing wrong here?

Thanks.

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.

Yes, I can confirm, macro expansion is magic and a night mare!

This does also not work:

{
    "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"
        }
    ]
}

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

{
    "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"
                }
            ]
        }
    ]
}

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

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.

How would you solve the DRY problem ?

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

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.

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