vscode-cmake-tools icon indicating copy to clipboard operation
vscode-cmake-tools copied to clipboard

Provide a way to set environment variables when using cmake-presets

Open sterin opened this issue 3 years ago • 8 comments

For example, a preset that uses vcpkg requires a path to vcpkg.cmake, whose location may vary across machines. As the documentation suggests, this can be done by using $env{VCPKG_ROOT}.

Using the command line it is very easy to choose a vcpkg installation on a specific machine, or switch between vcpkg installations (to debug new versions of packages) by prepending the environment variable to the command line.

However, from inside vscode, there's no easy way to set or modify this variable. This makes it difficult to work with presets with CMake-Tools.

Please add a setting for preset environment variables or enable the existing cmake.environment when using presets.

sterin avatar May 03 '21 13:05 sterin

A preset definition supports the "environment" keyword. Let us know if it is not working properly.

andreeis avatar May 03 '21 16:05 andreeis

The environment keyword is used to set environment variables set by the preset to CMakeLists.txt. I would like a way to set the content of the environment passed to the preset.

Say I have a configuration preset with:

"cacheVariables": {
   "CMAKE_TOOLCHAIN_FILE": {
      "value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
       "type": "FILEPATH"
    }
 },

From the command line I can easily switch between vcpkg installations by just changing the command line:

VCPKG_ROOT=/path/to/first/vcpkg/install cmake --preset ...

or

VCPKG_ROOT=/path/to/second/vcpkg/install cmake --preset ...

But to do that from vscode, unless I am missing something very obvious, I need to close vscode, change the environment variable, and then start vscode again.

sterin avatar May 03 '21 22:05 sterin

You can set environment variables via the environment map in a Configure Preset. Example:

"environment": {
        "VCPKG_ROOT": "/path/to/vcpkg/install"
}

If you want to easily switch between vcpkg installs, I recommend defining 2 Configure Presets. The first Configure Preset can specify one vcpkg root. The second Configure Preset can inherit from the first Configure Preset and overwrite the VCPKG_ROOT env var.

More info on the environment map is here.

esweet431 avatar May 03 '21 23:05 esweet431

Yes, this can be done this way, but I don't think it's a good idea in this scenario.

The file CMakePresets.json is usually source controlled. In my example it describes how to build the project using vcpkg, but it should not contain the exact location of vcpkg because it changes across machines, and it changes (a lot) when testing and debugging new vcpkg versions.

Changing CMakePresets.json each time is error prone because the changes can be easily committed with a bit of inattention, ruining other people's day.

sterin avatar May 03 '21 23:05 sterin

What about CMakeUserPresets.json that has two entries that inherit a base preset from CMakePresets.json and set the environment differently in each one? The user presets file is meant for such a case as this and is not meant to be committed to source control.

bobbrow avatar May 03 '21 23:05 bobbrow

It is definitely possible to this. It is also possible to use a symbolic link to point to the vcpkg installation. I just think this overcomplicates things.

While CMakePresets.json is supposed to be the sole source of truth, the use of environment variables to set machine-specific values is standard practice, it even appears in CMake Tools' own documentation.

What CMake Tools is missing is a way to set these values, or override these values, from inside vscode without going through hoops.

sterin avatar May 04 '21 00:05 sterin

This is currently a blocking issue for me and my team, and it is preventing us from switching from caches to presets.

Suppose you have say 3 different configure presets, 1-2 build presets for each configure preset, and maybe 2 different test presets. Now, as a developer, you want to use a compiler launcher, a different compiler or compiler version, or something like @sterin described. You would have to write an awful amount of user presets just to be able to do that and since the build/test presets all refer to a configure preset, you would have to override them as well.

Not being able to modify the environment defeats the whole purpose of the $penv{<variable-name>} macro in presets.

globberwops avatar May 05 '21 18:05 globberwops

It seems that in cmakeDriver.ts (here) adding environment and configureEnvironment settings to the configuration env ignored when presets are enabled. IMO environments are not such things to be ignored because of environment prop in presets.

evakili avatar Jul 21 '22 21:07 evakili

It's very confusing that these vscode configure options are ignored when using presets. They should have an effect when using presets too.

It's perfectly valid to do stuff like cmake --preset bla -DSOME_EXTRA_CACHE_VARIABLE=abcd

elupus avatar Jun 16 '23 14:06 elupus

Same issue here, any updates?

HaoboGu avatar Jul 20 '23 10:07 HaoboGu

@HaoboGu We have set this for 1.16 and we plan to investigate and design for this in the 1.16 release. Thanks for your interest.

gcampbell-msft avatar Jul 21 '23 14:07 gcampbell-msft

Same issue here, any updates?

vipcxj avatar Jan 15 '24 10:01 vipcxj

@vipcxj This should be resolved by #3537 which is currently in pre-release. I am marking this as fixed and will close it when we release it to the official release! Thanks!

gcampbell-msft avatar Jan 30 '24 20:01 gcampbell-msft