Skip to content

Fix IntelliSense in projects with compiler arguments in CMAKE_<LANG>_COMPILER - #4627

Merged
Hannia Valera (hanniavalera) merged 2 commits into
microsoft:mainfrom
indel-ag:commandfragment
May 5, 2026
Merged

Hannia Valera (hanniavalera) merged 2 commits into
microsoft:mainfrom
indel-ag:commandfragment

Conversation

@cwalther

@cwalther Christian Walther (cwalther) commented Dec 3, 2025 •

Copy link
Copy Markdown
Contributor

This changes visible behavior

The following changes are proposed:

  • Pass mandatory compiler arguments from CMake 4.3 to cpptools

The purpose of this change

Fix IntelliSense in projects with compiler arguments in CMAKE_<LANG>_COMPILER by passing those arguments, newly exposed in the file API by CMake 4.3, to cpptools so it can properly determine system include paths and built-in preprocessor macro definitions.

Other Notes/Information

Problem

In CMake, the CC or similar environment variables as well as the CMAKE_<LANG>_COMPILER variables set by toolchain files can, in addition to the compiler path, contain mandatory command line arguments that must be passed to every invocation of the compiler. Previously, these arguments were not exposed in the File API, therefore CMake Tools as an IntelliSense configuration provider had no way to pass them on to cpptools, therefore cpptools would call the compiler without them when trying to determine system include paths and built-in preprocessor macro definitions, potentially resulting in non-working code navigation.

For example, for building for embedded systems with different CPU architectures we use toolchain files with lines like

set(CMAKE_CXX_COMPILER "${INDEL_TOOLCHAIN_LOCATION}/bin/clang++${CMAKE_HOST_EXECUTABLE_SUFFIX};--config;aarch64-indel-elf.cfg;-mcpu=cortex-a72")

where the referenced config file contains arguments like --target, --sysroot, -stdlib++-isystem that influence include paths and definitions. Currently, Visual Studio Code cpptools calls the compiler without these arguments when trying to determine the include paths and definitions, and gets back wrong values related to the host platform instead of the embedded system.

Steps to Reproduce

Making a generic minimal reproducing example is a bit tricky because CMake expects working compilers (including sysroots). Here is something that works for me on an x86_64 Linux system with Clang 18 installed (Ubuntu 20.04, clang-18 package from official archive.ubuntu.com/security.ubuntu.com):

CMakeLists.txt:

cmake_minimum_required(VERSION 3.24)
project("compilerargs" C)
add_executable("test" main.c)

toolchain.cmake:

set(CMAKE_C_COMPILER "/usr/bin/clang-18;-target;i386-linux-gnu")

CMakePresets.json:

{
	"version": 6,
	"configurePresets": [
		{
			"name": "Test",
			"generator": "Ninja",
			"binaryDir": "${sourceDir}/output",
			"toolchainFile": "toolchain.cmake"
		}
	]
}

main.c:

#include <something.h>

int main() {
	return hello;
}

/usr/include/i386-linux-gnu/something.h:

int hello = 42;

Steps to reproduce

  1. Put the first four files above in a folder anywhere and the last in the named location.
  2. Open the folder in Visual Studio Code.
  3. Click Build in the status bar and select preset Test when prompted.
  4. The build completes successfully and you can run ./output/test; echo $? to get the expected result 42. file output/test confirms that it was compiled for i386.
  5. Open main.c.

Actual result

  • The first line #include <something.h> has red squiggly underlines. Ctrl-clicking on it does nothing.
  • Hovering on hello shows no tooltip.
  • For reasons I don’t understand that may be related to remnants from previous experiments done on my system, ctrl-clicking on hello does work and takes me to its definition in …/i386-linux-gnu/something.h. I would expect it to fail as well.
  • CMake: Log Diagnostics and C/C++: Log Diagnostics give no indication that the required -target i386-linux-gnu arguments have made it from CMake to cpptools, in particular the C/C++ diagnostics show the wrong system include: /usr/include/x86_64-linux-gnu for main.c.

Expected result

  • No red squiggly underlines.
  • Hovering on hello shows a tooltip “int hello”.
  • Ctrl-clicking on either <something.h> or hello opens …/i386-linux-gnu/something.h.
  • CMake: Log Diagnostics shows "compilerFragments": ["-target i386-linux-gnu"] for main.c.
  • C/C++: Log Diagnostics shows the correct system include: /usr/include/i386-linux-gnu for main.c.

Proposed Solution

CMake has just merged an addition to the file API to expose these arguments, to be released in CMake 4.3. I therefore propose the attached commits to retrieve them from the file API and pass them on to cpptools.

Only the last two commits are part of this change, the rest are #4620, which is required to actually be able to test with CMake 4.3 (so don’t use the combined diff for review, but the individual commits). But if there are objections to that, I will of course rework it. This change does not strictly depend on #4620, some of the added tests will just be skipped without it.

An update to vscode-cmake-tools-api is required, submitted in microsoft/vscode-cmake-tools-api#15, so there is a dependency version bump in package.json. CONTRIBUTING.md tells me to ping “our team” in that case, but I don’t know who that is (maybe Garrett Campbell (@gcampbell-msft)?).

Open Questions

  • I am not sure what to do about the use of shlex.split(), see comments in cpptools.ts:471. Possible options:

    • Leave it as it is and declare the (rare, as far as I can tell) case where it causes problems unsupported.
    • Add a second shlex-split function that does what I would expect and what is needed here.
    • Change the behavior of shlex.split to be what I would expect and what is needed here. However I fear that would break other things, because presumably the current behavior, which is explicitly tested for in test/unit-tests/shlex.test.td, was introduced for a reason in support parsing substring and also substrings with single quote and space #1663.

    Any preferences? Any comment from Elaheh Rashedi (@elahehrashedi) who wrote that behavior?

@cwalther

Copy link
Copy Markdown
Contributor Author

Rebased on the updated #4620 to fix merge conflicts. No changes required.

@cwalther

Copy link
Copy Markdown
Contributor Author

Rebased on main, with minor manual conflict resolution. Verified that it still works, using the Steps to Reproduce above and CMake 4.3.1. Tests also still pass (although #4891 was required for me).

I am guessing that the proper location for the changelog entry is now 1.24, because as far as I understand, version 1.23.51 has now been released as the final 1.23 release, and there is a new pre-release 1.24.0.

@cwalther

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree company="Indel AG"

@cwalther

Copy link
Copy Markdown
Contributor Author

Rebased to fix merge conflict in changelog.

@cwalther

Copy link
Copy Markdown
Contributor Author

I will suspend further changelog conflict fixing whack-a-mole until asked or other changes become necessary.

@hanniavalera

Copy link
Copy Markdown
Contributor

Hi Christian Walther (@cwalther), I really appreciate this contribution! I would like to properly review it, would you mind placing a changelog entry for it under 1.24 under Improvements? I understand it is a little bit of a whack-a-mole, but would appreciate your time so we can take this in!

@cwalther

Copy link
Copy Markdown
Contributor Author

Rebased and moved changelog entry from Features to Improvements.

I see you made some changes to shlex.split in #4898. I will check whether these resolve my “open question” next.

@cwalther

Copy link
Copy Markdown
Contributor Author

#4898 does not completely resolve the open question: shlex.splitstill does a different thing than a POSIX shell would in that it still does not consume quotes that have done their duty (splitUnix('"fo o" bar') is ['"fo o"', 'bar'] instead of the expected ['fo o', 'bar']). However its handling of backslashes and mismatched quotes is now improved, which further reduces the (already small, in my opinion) set of cases where the way it is used here causes incorrect results.

@cwalther

Copy link
Copy Markdown
Contributor Author

CI fails because microsoft/vscode-cmake-tools-api#15 is not released yet. Until it is, you can test with a local checkout using this change:

diff --git a/package.json b/package.json
index 7ca2066c..f2cabc34 100644
--- a/package.json
+++ b/package.json
@@ -4497,7 +4497,7 @@
     "tsconfig-paths": "^3.11.0",
     "tslint": "^6.1.3",
     "typescript": "^4.1.5",
-    "vscode-cmake-tools": "^1.7.0",
+    "vscode-cmake-tools": "file:../vscode-cmake-tools-api",
     "vscode-nls-dev": "^3.3.2",
     "webpack": "^5.104.1",
     "webpack-cli": "^5.1.4"
@hanniavalera

Copy link
Copy Markdown
Contributor

Christian Walther (@cwalther) got in your PR for the API + published it, all should be set now :) would you be alright resolving the conflicts? please let me or MacGyver Codilla (@mcodilla) know if you have any questions!

@cwalther

Copy link
Copy Markdown
Contributor Author

Thanks, looks good!

Rebased on main with manual conflict resolution in CHANGELOG.md, no other changes required; added yarn.lock changes for API package update.

@cwalther

Copy link
Copy Markdown
Contributor Author

I’m not sure why the tests fail on Windows. I see no reason why CMake should choose cl.exe when I explicitly tell it to use cross-compile-gcc.exe. Looks like that somehow doesn’t get through. I guess I’ll have to set up a Windows development system and debug it.

@cwalther

Copy link
Copy Markdown
Contributor Author

Before I do that, a shot in the dark: This reminds me of a similar problem I had while working on the CMake part: Could it be that in this Windows case CMake defaults to a Visual Studio generator that does not support other compilers than MSVC? Let’s try with an explicit generator.

Hannia Valera (@hanniavalera) or MacGyver Codilla (@mcodilla), could you start the workflow again?

@cwalther

Copy link
Copy Markdown
Contributor Author

Still the same failure. Too bad. Let me try to reproduce and debug it.

The `CMAKE_<LANG>_COMPILER` variable or the `CC` or similar environment
variable can contain command line arguments that must be passed to every
invocation of the compiler. Previously, these were lost on the way from
CMake to cpptools because the CMake file API did not expose them, causing
non-working code navigation after cpptools called the compiler without them
to determine system include paths and built-in preprocessor macro
definitions.

CMake 4.3 adds a new `commandFragment` member to the `toolchains.
toolchains[].compiler` object to rectify that. Pass that along in
configuration provider responses.
@cwalther

Copy link
Copy Markdown
Contributor Author

Got it. I need to pass the generator using -D, not -G, otherwise it appends another -G that overrides it. There also was a forward/backward-slash path mismatch. This passes for me on Windows now. Hope it doesn’t break the others.

@hanniavalera

Copy link
Copy Markdown
Contributor

Christian Walther (@cwalther), thank you so much for the ongoing iterations and contributions, i was about to reply to inform you that is what i was theorizing, but we'll see if we're missing something! (i doubt it, though!) thanks a lot!

@hanniavalera
Hannia Valera (hanniavalera) merged commit 032b060 into microsoft:main May 5, 2026
4 checks passed
@hanniavalera

Copy link
Copy Markdown
Contributor

Approved and merged! Thanks a lot once again! I really appreciate the diligence :)

@cwalther

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

@cwalther
Christian Walther (cwalther) deleted the commandfragment branch May 6, 2026 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants