CTest build-and-test preserves environment variables after running
The documentation for set(ENV{VAR} ...)
says
This command affects only the current CMake process, not the process from which CMake was called, nor the system environment at large, nor the environment of subsequent build or test processes.
But that isn't true in CTest's build-and-test mode. Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(repro)
set(ENV{THIS_DEFINITELY_DOES_NOT_EXIST} "Some arbitrary value")
add_custom_target(print-env ALL env)
Now we'll run CTest with only the PATH
env-var set.
$ env -i PATH="$PATH" env | wc -l
1
$ env -i PATH="$PATH" ctest --build-and-test . build --build-generator Ninja
Internal cmake changing into directory: /path/to/build
======== CMake output ======
The C compiler identification is GNU 9.3.0
The CXX compiler identification is GNU 9.3.0
Check for working C compiler: /usr/bin/cc
Check for working C compiler: /usr/bin/cc -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: /usr/bin/c++
Check for working CXX compiler: /usr/bin/c++ -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Configuring done
Generating done
Build files have been written to: /path/to/build
======== End CMake output ======
Change Dir: /path/to/build
Run Clean Command:/usr/bin/ninja clean
[1/1] Cleaning all built files...
Cleaning... 0 files.
Run Build Command(s):/usr/bin/ninja && [1/1] cd /path/to/build && env
OLDPWD=/path/to/build
THIS_DEFINITELY_DOES_NOT_EXIST=Some arbitrary value
PATH=...
LANG=
LC_MESSAGES=
CXX=/usr/bin/c++
LC_ALL=
PWD=/path/to/build
CC=/usr/bin/cc
Every environment variable leaked into the custom target. How did this happen?
The practical consequence of this for me was that toolchain information from a parent project was leaking into a child ExternalProject but only in build-and-test. I lost at least eight hours of productivity on this.