VSCode shows error in CMakeLists.txt despite project building just fine; how to match VSCode $ENV with PlatformIO build system's?

I have recently started a project using the Espressif IoT Development Framework for an ESP32 I am playing around with and I have encountered an annoying and incorrect error being detected in my project’s top-level CMakeLists.txt file. The line where it includes the IDF’s project.cmake file from the ESP IDF install shows an error (include could not find load file), specifically it seems that the $ENV{IDF_PATH} substitution is not being recognized by the CMake linter in VSCode since the error is completely missing that first part of the path ($ENV{IDF_PATH}/tools/cmake/project.cmake becomes just /tools/cmake/project.cmake). My code does build and run so when PlatformIO runs the build process the environment variable IDF_PATH must be present, but VSCode doesn’t seem to be seeing it when checking for errors, so it consistently shows me 3 errors on this file. Does anyone know to make VSCode see the environment variables that the PlatformIO build system uses? I think that would solve this little issue. Just to clarify, my project builds 100% completely fine, but VSCode shows an error where it doesn’t seem it should exist.

Thanks!

PlatformIO does not export IDF_PATH etc in the VSCode / system’s environment variables, it only does so during runtime when VSCode calls into pio run for compilation. The CMake extension will not work with PlatformIO in the ESP-IDF project case and show false errors.

You may be able to fix it externally by adding the IDF_PATH variable to your user’s environment variables with value C:\Users\<user>\.platformio\packages\framework-espidf, and maybe also have C:\Users\<user>\.platformio\packages\toolchain-xtensa32\bin in your path.

Thanks for the reply. I was hoping to see a more elegant solution but based on what you’ve shown that won’t work.

I had a similar error. %IDF_PATH% was unset and I was almost ready to add it to the user env vars. Then, rebooting my PC fixed the problem!

  • VSCode complained include could not find load file.
  • Build failed.
  • Restarting VSCode didn’t help.
  • Clearing the build directory didn’t help.
  • Rebooting the entire PC fixed it. Build passed. VSCode started to know about %IDF_PATH%.