IntelliSense Doesn’t Update Automatically for Generated Files in Visual Studio C++ Project


I’m encountering an issue with IntelliSense in Visual Studio where it doesn’t automatically update its database after a rebuild when using a custom NuGet package that generates .cpp and .hpp files during the build process.

In my C++ project, I use a NuGet package with a custom .targets file. This .targets file runs a tool during the build process (via an task) that takes several input files, processes them, and generates two files: generated.cpp and generated.hpp. These files are then included in the project using ClInclude and ClCompile.

The input files define classes and members that my project uses, and the generated headers are included in my code. The build process itself works fine, and IntelliSense initially recognizes these files as expected.

If I change something in one of the input files (e.g., add a new member to a class), rebuild the project, and then try to use the new member in my code, IntelliSense doesn’t recognize the change. It shows errors for the new member as if the header hasn’t been updated.

The only ways I’ve found to fix this are:

Manually open the generated.cpp file, right-click, and choose “Rescan file”.
Rescan the entire solution or delete the IntelliSense database manually.
Neither of these is a practical solution for development.

Here’s a simplified example of the setup:

Input Files
input.file (used as input to the tool during the build process)

class MyClass {
    int oldMember;
    int newMember; // Added later
};

Generated Files (produced by the tool):

generated.hpp

cppCopy code
class MyClass {
    int oldMember;
    int newMember;
};

generated.cpp

#include "generated.hpp"

Project File Snippet


<Target Name="GenerateSources" BeforeTargets="ClCompile">
  <Exec Command="my-tool.exe input.file -o generated.cpp -h generated.hpp" />
  <ItemGroup>
    <ClInclude Include="generated.hpp" />
    <ClCompile Include="generated.cpp" />
  </ItemGroup>
</Target>

After modifying input.file to add a new member and rebuilding, IntelliSense doesn’t reflect the new member until I manually rescan.

How can I configure Visual Studio or modify my setup so that IntelliSense automatically updates its database to reflect changes in the generated .hpp files after a rebuild? Ideally, I want IntelliSense to rescan the affected files without manual intervention.

I’ve tried several approaches to make IntelliSense automatically recognize updates to the generated files after a rebuild. Some of the things I’ve tried include:

  • Modifying the .targets file to ensure the generated files are marked as updated, such as touching the files after they are generated.
  • Attempting to hook the generation task into design-time build targets (I’m not sure which ones specifically to use).
  • Experimenting with various project settings and IntelliSense configurations.
  • Restarting Visual Studio 2019 after making changes.
  • Generating TLogs using the GetOutOfDateItems task to track dependencies.
    Unfortunately, none of these attempts worked. IntelliSense continues to show outdated information unless I manually rescan the files or rebuild the entire IntelliSense database, which is impractical and time-consuming.

I expected IntelliSense to automatically pick up changes to the generated.hpp and generated.cpp files after the build, just as it does for regular source files I edit inside the IDE.

Leave a Reply

Your email address will not be published. Required fields are marked *