Visual Studio fails to reload properly upon CMake changes

Here is a basic C++ project

# CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(FOOBAR LANGUAGES "CXX")
add_executable(baz)
target_sources(baz PRIVATE main.cpp)
// main.cpp
#include <iostream>
int main() { std::cout << "Hello world\n"; }

Let’s generate a Visual Studio project. And Open it.

$> cmake -S . -B build/ -G "Visual Studio 16 2019"
$> cmake --open build

Everything works fine. Visual Studio is working nicely :+1:

image.png

However, lets add a new line to our CMake code:

target_compile_definitions(baz PRIVATE FOOBAR)

Then run ZERO_CHECK (This re-runs CMake):

However, it doesn’t properly reload the Visual Studio Project.

Go into the properties and FOOBAR hasn’t been added.
image.png

As a result the intellisense doesn’t work:
image.png

However. The files have been modified. You can verify this locally.
And if you close the visual studio project and re-open it, everything works fine.

image.png

So basically Visual Studio doesn’t seem to properly reload new properties.

This isn’t just isolated to preprocessor definitions. This occurs with compiler options, include directories, etc.

This really hurts our workflow. We use CMake to generate our Visual Studio Solutions.

This functionality used to work properly. However, after a Visual Studio update this stopped working.

However, I’m pretty sure this is a Visual Studio bug, so I created this ticket for the VS team here:

Although I think this is a VS bug I wanted to let the CMake community know.

Yeah, this sounds like a VS issue. CMake used to have a plugin thing to do the reload, but VS started detecting changed project files (back in VS2012?) and CMake has been out of the loop since then (AFAIK).

For reference this issue has now been fixed in VS:

1 Like