environment variable from preset during build

I have an issue where the environment setting in a config preset is not used when running ninja.

$ cmake --preset default && touch CMakeLists.txt && (cd build/default; ninja)
Preset environment variables:

  MY_ENVIRONMENT_VARIABLE="Test"

-- MY_ENVIRONMENT_VARIABLE: Test
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/kuka/cmake_env/build/default
[0/1] Re-running CMake...
-- MY_ENVIRONMENT_VARIABLE: 
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/kuka/cmake_env/build/default
ninja: no work to do.

I expected that the environment variable is set during the configuration triggered by ninja.
Is this a bug or expected behavior?

See my simple project:
cmake_env.zip (691 Bytes)

Note:

  • Setting the variable again in the build preset does not help
  • running cmake --build --preset default instead of ninja works, but I still think the generated build.ninja should know this.

Expected behavior. Environment variables that are set by CMake at configure time don’t persist to build time. This has been the case since long before CMakePresets.json existed.

1 Like

Seems fair. Did not think about it this way. Thanks.

Just realized I misread your question. The answer is still “expected behavior”, but not for the reason I stated above.

When you cd build/default; ninja, you’re running CMake a second time without the preset active, which means the environment variable is not set. The second time MY_ENVIRONMENT_VARIABLE: is printed, it’s being printed at configure time, not build time.