After `unset` in a nested scope, the parent scope variable doesn't become visible
If a variable exists as a cache variable, is set at a scope, then is set in a nested scope, then is unset in that nested scope, the cache variable is now visible in the nested scope, instead of the parent scope one.
In code: CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(unset_test_root LANGUAGES NONE)
include(CTest)
message(STATUS "root: BUILD_TESTING is '${BUILD_TESTING}' before setting it to OFF")
set(BUILD_TESTING OFF)
message(STATUS "root: BUILD_TESTING is '${BUILD_TESTING}' before add_subdirectory(subdir)")
add_subdirectory(subdir)
message(STATUS "root: BUILD_TESTING is '${BUILD_TESTING}' after add_subdirectory(subdir)")
unset(BUILD_TESTING)
message(STATUS "root: BUILD_TESTING is '${BUILD_TESTING}' after unsetting it")
subdir/CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(unset_test_subdir LANGUAGES NONE)
message(STATUS "subdir: BUILD_TESTING is '${BUILD_TESTING}' before setting it to OFF")
set(BUILD_TESTING OFF)
message(STATUS "subdir: BUILD_TESTING is '${BUILD_TESTING}' before add_subdirectory(subdir2)")
#add_subdirectory(subdir2)
message(STATUS "subdir: BUILD_TESTING is '${BUILD_TESTING}' after add_subdirectory(subdir2)")
unset(BUILD_TESTING)
message(STATUS "subdir: BUILD_TESTING is '${BUILD_TESTING}' after unsetting it")
Output:
-- root: BUILD_TESTING is 'ON' before setting it to OFF
-- root: BUILD_TESTING is 'OFF' before add_subdirectory(subdir)
-- subdir: BUILD_TESTING is 'OFF' before setting it to OFF
-- subdir: BUILD_TESTING is 'OFF' before add_subdirectory(subdir2)
-- subdir: BUILD_TESTING is 'OFF' after add_subdirectory(subdir2)
-- subdir: BUILD_TESTING is 'ON' after unsetting it
-- root: BUILD_TESTING is 'OFF' after add_subdirectory(subdir)
-- root: BUILD_TESTING is 'ON' after unsetting it
Expected:
-- subdir: BUILD_TESTING is 'OFF' after unsetting it