Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
CMake
CMake
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,288
    • Issues 3,288
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 11
    • Merge Requests 11
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value Stream
  • External Wiki
    • External Wiki
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • CMake
  • CMakeCMake
  • Issues
  • #22043

Closed
Open
Created Apr 08, 2021 by Alex Reinking@alexreinking

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.

Edited Apr 08, 2021 by Alex Reinking
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None