Skip to content

Cached target source lists accumulate across reconfigures and keep removed files #1841

Open
nanoframework/nf-interpreter
#3540

Description

@dkovyazin

Describe the bug

The per-target source and include lists are built with list(APPEND ...) and then stored in the CMake cache:

list(APPEND TARGET_ESP32_IDF_COMMON_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/targetHAL.c)
...
set(TARGET_ESP32_IDF_COMMON_SOURCES ${TARGET_ESP32_IDF_COMMON_SOURCES} CACHE INTERNAL "make global")

list(APPEND) starts from the value already in the cache, so on every reconfigure of an existing build directory the list gains another full copy of itself, and — the part that actually breaks builds — a source file removed from the list stays in the cache forever.

How it shows up

nanoframework/nf-interpreter#3502 deleted targets/ESP32/_common/targetHAL_StorageOperation.cpp and removed it from targets/ESP32/_common/CMakeLists.txt. On a build directory configured before that commit, the next build fails at generate time:

CMake Error at CMake/binutils.ESP32.cmake:310 (target_sources):
  Cannot find source file:
    .../targets/ESP32/_common/targetHAL_StorageOperation.cpp
Call Stack (most recent call first):
  CMake/binutils.ESP32.cmake:418 (nf_add_platform_sources)
  targets/ESP32/ESP32_S3/CMakeLists.txt:10 (nf_setup_target_build)

The file is gone from the CMakeLists but still sits in TARGET_ESP32_IDF_COMMON_SOURCES in CMakeCache.txt. Deleting that one cache entry, or the whole build directory, is the only way out.

CI does not hit this because it always configures a fresh build directory. It hits developers doing incremental builds across any commit that removes a source file.

Where it applies

Every list built this way, e.g. in the ESP32 tree:

  • targets/ESP32/_common/CMakeLists.txt — TARGET_ESP32_IDF_COMMON_SOURCES
  • targets/ESP32/_include/CMakeLists.txt — TARGET_ESP32_IDF_COMMON_INCLUDE_DIRS
  • targets/ESP32/_nanoCLR/CMakeLists.txt — TARGET_ESP32_IDF_NANOCLR_SOURCES, TARGET_ESP32_IDF_NANOCLR_INCLUDE_DIRS
  • targets/ESP32/_Network/CMakeLists.txt — TARGET_ESP32_IDF_NETWORK_SOURCES, TARGET_ESP32_IDF_NETWORK_INCLUDE_DIRS

and the equivalents under targets/ChibiOS, targets/ThreadX, targets/FreeRTOS and targets/TI_SimpleLink.

Possible fix

For the ESP32 lists each variable has a single writing CMakeLists, so clearing it at the top of that file is enough:

set(TARGET_ESP32_IDF_COMMON_SOURCES "")

That is what we run in our fork, and it removes both the duplication and the stale entries.

The other RTOS trees need more care: there several directories append to the same list within one configure. Building ST_B_L475E_IOT01A, for instance, fills TARGET_THREADX_COMMON_SOURCES from four CMakeLists — targets/ThreadX/_common, targets/ThreadX/ST/_common, targets/ThreadX/ST/_common/CubeMX and the board directory itself — so a per-file reset would drop the earlier contributions. There the reset belongs once in the parent scope, before the add_subdirectory() calls. We only build the ESP32 targets and cannot test the others, hence an issue rather than a pull request.

Activity

  1. josesimoes commented on Sep 24, 2026

    @josesimoes
    Member

    @dkovyazin OK to submit a PR with this fix. As for the other platforms, feel free to throw a fix for those too. The CI pipeline will take care of the testing for you. 😉

  2. dkovyazin commented on Sep 24, 2026

    @dkovyazin
    Author

    @josesimoes thanks — the PR is up: nanoframework/nf-interpreter#3540.

    It covers every tree, not only ESP32. The clear is done once in the top level CMakeLists.txt, before the RTOS dispatch, rather than at the top of each writing file: TARGET_THREADX_COMMON_SOURCES and friends are filled from several CMakeLists within one configure, so a per-file reset would drop the earlier contributions. One reset in the parent scope is correct for all of them.

    Tested end to end on ESP32_S3_QUAD, since that is the hardware I have. With the call disabled, a single reconfigure of an existing build directory doubles the lists (16 → 32 entries on TARGET_ESP32_IDF_COMMON_SOURCES); with it in, the lists stay at their correct length, a directory already poisoned recovers on the next configure, and a source line removed from a CMakeLists disappears from the cache the way it should. The other trees I only read through — the pipeline can take those, as you said.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions