source_group Visual Studio

I am trying to use source_group to show the files nicely in my IDE (Visual Studio 2022). The structure of my header files has some files in subfolders and others only in the include folder (sorry I can only embed one photo):

include
├── DD
│   ├── comparator.hpp
│   ├── dd_solution.hpp
│   └── vertex.hpp
├── FORPFSSPSD
│   ├── FORPFSSPSD.h
│   ├── aliases.hpp
│   ├── plexity.hpp
│   └── ...
├── cyclefinder.h
├── delay.h
└── ...

And I use this CMake code to group the header files (LIB_COMMON_HEADERS has the list of all headers):

source_group(
    TREE "${PROJECT_SOURCE_DIR}/include"
    PREFIX "Header Files"
    FILES ${LIB_COMMON_HEADERS}
)

However, in Visual Studio 2022 it shows as follows:

Notice how the files immediately in the folder include/*.h and not in a subfolder are not added in to the “Header files” group.

I’ve also noticed that if I define the subgrup as follows (removing /include from the TREE):

source_group(
    TREE "${PROJECT_SOURCE_DIR}"
    PREFIX "Header Files"
    FILES ${LIB_COMMON_HEADERS}
)

Then the files are grouped but all inside the include directory but not immediately inside the Header Files group:

Header Files
└── include
     ├── DD
     │   ├── comparator.hpp
     │   ├── dd_solution.hpp
     │   └── vertex.hpp
     ├── FORPFSSPSD
     │   ├── FORPFSSPSD.h
     │   ├── aliases.hpp
     │   ├── plexity.hpp
     │   └── ...
     ├── cyclefinder.h
     ├── delay.h
     └── ...

Am I doing something wrong? Shouldn’t the first code already work properly? If my code is fine, is this a bug in CMake or Visual Studio 2022?

Are you opening this through the CMakeLists.txt and not the Visual Studio solution (if you are generating a Visual Studio solution)? It could very well be an issue with how Visual Studio builds the view from the CMake file API information. I suppose that could suggest how CMake reports the information could also be at fault. If you are generating a Visual Studio solution and not something like Ninja, you could take a look at the VS solution and see if the layout is correct.

At any rate, the way you are using source_group appears to be correct.

I’m opening this through the CMakeLists.txt option of Visual Studio so I guess then that it’s a Visual Studio issue. I’ll try to open an issue.

Thanks for the help :blush: