microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.47k stars 454 forks source link

Suppress "Unable to resolve configuration with compilerPath" warning in CMake+Swift projects #4097

Closed tristanlabelle closed 1 month ago

tristanlabelle commented 2 months ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary: While working on a CMake Swift project, the C++ extension will regularly print the message below to the C/C++ Configuration Warnings output window righ after a build, which is annoying because:

  1. It steals the focus from the CMake/Build output pane, hiding the output of the build
  2. I'm not trying to use C/C++ code at the time where this shows up (though the project includes some C sources)
  3. The C/C++ extension probably shouldn't be trying to resolve anything with the swiftc.exe compiler
[9/13/2024, 6:32:16 AM] Unable to resolve configuration with compilerPath "c:/users/tristan/appdata/local/programs/swift/toolchains/0.0.0+asserts/usr/bin/swiftc.exe".  Using "cl.exe" instead.
[9/13/2024, 6:36:45 AM] Unable to resolve configuration with compilerPath "c:/users/tristan/appdata/local/programs/swift/toolchains/0.0.0+asserts/usr/bin/swiftc.exe".  Using "cl.exe" instead.

Steps to reproduce:

  1. Install Swift for Windows from https://www.swift.org/install/windows/
  2. Clone https://github.com/tristanlabelle/swift-winrt
  3. Open the Generator subfolder in VS Code from a VS dev cmd
  4. Run cmake --preset debug and cmake --build --preset debug (the bug is not specific to this repo, I see it regularly when working in Swift)

Expected behavior: The C/C++ extension should not produce this warning. Probably it should ignore anything to do with the Swift compiler.

Configuration and Logs

-------- Diagnostics - 9/13/2024, 6:53:56 AM
Version: 1.21.6
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "d:/swift-winrt/Generator/**"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.22621.0",
    "compilerPath": "cl.exe",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "windows-msvc-x64",
    "compilerPathInCppPropertiesJson": "cl.exe",
    "intelliSenseModeIsExplicit": false,
    "cStandardIsExplicit": false,
    "cppStandardIsExplicit": false,
    "mergeConfigurations": false,
    "compilerPathIsExplicit": false,
    "configurationProvider": "ms-vscode.cmake-tools",
    "browse": {
        "path": [
            "d:/swift-winrt/Generator/**",
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Custom browse configuration: 
{
    "browsePath": [
        "d:/swift-winrt/generator/build/debug/dependencies/swift-argument-parser",
        "d:/swift-winrt/generator/build/debug/_deps/swift-argument-parser-src/sources/argumentparser/completions",
        "d:/swift-winrt/generator/build/debug/_deps/swift-argument-parser-src/sources/argumentparser/parsable properties",
        "d:/swift-winrt/generator/build/debug/_deps/swift-argument-parser-src/sources/argumentparser/parsable types",
        "d:/swift-winrt/generator/build/debug/_deps/swift-argument-parser-src/sources/argumentparser/parsing",
        "d:/swift-winrt/generator/build/debug/_deps/swift-argument-parser-src/sources/argumentparser/usage",
        "d:/swift-winrt/generator/build/debug/_deps/swift-argument-parser-src/sources/argumentparser/utilities",
        "d:/swift-winrt/generator/build/debug/_deps/swift-argument-parser-src/sources/argumentparsertoolinfo",
        "d:/swift-winrt/generator/sources/codewriters/c",
        "d:/swift-winrt/generator/sources/codewriters/cmake",
        "d:/swift-winrt/generator/sources/codewriters",
        "d:/swift-winrt/generator/sources/codewriters/swift",
        "d:/swift-winrt/generator/sources/codewriters/swift/syntaxwriters",
        "d:/swift-winrt/generator/build/debug/dependencies/swift-collections",
        "d:/swift-winrt/generator/build/debug/_deps/swift-collections-src/sources/collections",
        "d:/swift-winrt/generator/build/debug/_deps/swift-collections-src/sources/dequemodule",
        "d:/swift-winrt/generator/build/debug/dependencies/swift-dotnetmetadata",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/dotnetmetadatacinterop/include",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/dotnetmetadata",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/dotnetmetadata/attributes",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/dotnetmetadatacinterop",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/dotnetmetadataformat",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/dotnetxmldocs",
        "d:/swift-winrt/generator/build/debug/_deps/swift-collections-src/sources/orderedcollections/hashtable",
        "d:/swift-winrt/generator/build/debug/_deps/swift-collections-src/sources/orderedcollections/ordereddictionary",
        "d:/swift-winrt/generator/build/debug/_deps/swift-collections-src/sources/orderedcollections/orderedset",
        "d:/swift-winrt/generator/build/debug/_deps/swift-collections-src/sources/orderedcollections/utilities",
        "d:/swift-winrt/generator/build/debug/sources/codewriters",
        "d:/swift-winrt/generator/sources/projectionmodel",
        "d:/swift-winrt/generator/sources/projectionmodel/cabi",
        "d:/swift-winrt/generator/build/debug/sources/projectionmodel",
        "d:/swift-winrt/generator/sources/swiftwinrt",
        "d:/swift-winrt/generator/sources/swiftwinrt/writing",
        "d:/swift-winrt/generator/build/debug/sources/swiftwinrt",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/windowsmetadata/attributes",
        "d:/swift-winrt/generator/build/debug/_deps/swift-dotnetmetadata-src/sources/windowsmetadata"
    ],
    "compilerPath": "c:/users/tristan/appdata/local/programs/swift/toolchains/0.0.0+asserts/usr/bin/swiftc.exe",
    "compilerArgs": [],
    "compilerFragments": [
        "-sdk C:/Users/tristan/AppData/Local/Programs/Swift/Platforms/0.0.0/Windows.platform/Developer/SDKs/Windows.sdk/ -O -g -wmo -libc MD",
        "-suppress-remarks",
        "-suppress-warnings"
    ]
}
cpptools version (native): 1.21.6.0
Translation Unit Mappings:
[ D:\swift-winrt\Generator\.build\checkouts\swift-dotnetmetadata\Sources\DotNetMetadataCInterop\shim.c - source TU]:
Translation Unit Configurations:
[ D:\swift-winrt\Generator\.build\checkouts\swift-dotnetmetadata\Sources\DotNetMetadataCInterop\shim.c ]:
    Process ID: 18168
    Memory Usage: 50 MB
    Compiler Path: C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe
    Includes:
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.39.33519\include
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.39.33519\atlmfc\include
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\VS\include
        C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\Include\um
    System Includes:
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\cppwinrt
    Defines:
        _DEBUG
        UNICODE
        _UNICODE
    Standard Version: ms_c17
    IntelliSense Mode: windows-msvc-x64
    Other Flags:
        --using_directory
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.39.33519\ATLMFC\lib\x64
        --using_directory
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.39.33519\lib\x64
        --using_directory
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.39.33519\lib\x86\store\references
        --using_directory
        C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.22621.0
        --using_directory
        C:\Program Files (x86)\Windows Kits\10\References\10.0.22621.0
        --using_directory
        C:\Windows\Microsoft.NET\Framework64\v4.0.30319
Total Memory Usage: 50 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 6004


### Other Extensions

CMake Tools and Swift extension, both of which I need (and more than the C/C++ extension in this scenario)

### Additional context

_No response_
tristanlabelle commented 2 months ago

I'd be happy to submit a PR but I can't find how the string is referenced: https://github.com/microsoft/vscode-cpptools/blob/fc5a084781aab590d0220f2ac4ff3c3e3cfcaec8/Extension/src/nativeStrings.json#L241

bobbrow commented 2 months ago

I don't think you need a PR, can you just set the C_Cpp.configurationWarnings setting to "disabled"? Maybe that's a bigger hammer than you want, but as long as CMake reports your compiler this way, we'll try to use it. I don't recall off hand whether we have a way to override the configuration provider's compiler. Perhaps @Colengms or @sean-mcmanus knows.

sean-mcmanus commented 2 months ago

As far as I know there's no way to override configuration provider's compiler path.

sean-mcmanus commented 2 months ago

Why is our extension being run on Swift files? We don't currently support Swift. Can the files.associations be changed such that our extension doesn't process the files?

bobbrow commented 2 months ago

There are C files in the workspace, so we get activated and will try to get the system includes before we index it.

sean-mcmanus commented 2 months ago

@bobbrow You mean .c files that are actually Swift files? That should be fixed by setting files.associations appropriately, such as

   "files.associations": {
      "**/DotNetMetadataCInterop/*.c": "swift"
   }

Or CMake Tools or some Swift extension or us could maybe do that automatically?

I don't think it matters if we're activated if the file is not associated with C/C++ then we won't process it, although maybe setting the files.association might also fix the activation.

tristanlabelle commented 2 months ago

I've boiled it down to a more minimal repro where nothing should cause the C/C++ extension to be involved

  1. Install a Swift toolchain from https://www.swift.org/install/windows/
  2. Create a folder with these files:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.21.0)
project(Repro LANGUAGES Swift)

CMakePresets.json:

{
    "version": 3,
    "configurePresets": [
      {
        "name": "default",
        "generator": "Ninja",
        "binaryDir": "${sourceDir}/build"
      }
    ],
    "buildPresets": [
      {
        "name": "default",
        "configurePreset": "default"
      }
    ]
  }

.vscode/settings.json:

{
    "C_Cpp.configurationWarnings": "disabled"
}
  1. Open that folder in VS Code from a VS dev cmd
  2. Run the CMake: Configure command

A few seconds after CMake configure completes, the focus switches to the C/C++ Configuration Warnings pane, which shows:

[9/14/2024, 6:43:07 AM] Unable to resolve configuration with compilerPath "c:/users/tristan/appdata/local/programs/swift/toolchains/0.0.0+asserts/usr/bin/swiftc.exe".  Using "cl.exe" instead.

(note that the C_Cpp.configurationWarnings config did not prevent this)

CMake should be able to report a per-language compiler path so it seems like a bug that the C/C++ Extension ends up with the swiftc.exe path at all when it should only query the compiler for C and for C++ languages.

bobbrow commented 2 months ago

Thanks for the update @tristanlabelle,

I'm sorry, I gave you wrong information about that setting. It does not actually control this scenario.

I don't currently see any way to disable that behavior besides disabling the C++ extension for that workspace. CMake Tools activates the C++ extension automatically because it calls our API. It's possible that CMake Tools could be improved to not call our API and activate our extension if it detects that there is no C or C++ code that needs configurations, but in the meantime, if you don't need C++ support active, there should be no harm in disabling the extension for that workspace.

To do this, go to the Extensions tab and click the gear for the extension (lower right corner): image Then choose "Disable (Workspace)" image

tristanlabelle commented 2 months ago

Thanks as well for the follow-up. Unfortunately, this solution would regress our developer experience. While I am able to reproduce the issue with a minimal Swift-only project, all of the real-world projects in which we encounter this bug (including The Browser Company's main codebase) have a mix of Swift and C/C++ files. This interoperability is also a selling point of Swift, so it's a common scenario.

There's a few ways this could be solved (best to worse):

  1. The CMake Tools should provide the C/C++ extension with per-language compiler info since that is CMake's data model, or the C/C++ extension could query it from CMake, depending on what the interface between the C/C++ extension and CMake is. I couldn't find the code in the repo.
  2. The C/C++ extension could explicitly ignore a compiler called swiftc
  3. The C/C++ extension could provide a new setting or extend the existing one to silence those warnings.
tristanlabelle commented 2 months ago

It seems likely that the issue is in this integration: https://github.com/microsoft/vscode-cmake-tools/blob/d860f0859de84de5babc043f5896d1f6a3033be0/src/cpptools.ts#L508 .

sean-mcmanus commented 2 months ago

@gcampbell-msft Should this be moved to the vscode-cmake-tools repo?

bobbrow commented 2 months ago

@tristanlabelle @gcampbell-msft, my guess is that CMake Tools should skip the SWIFT compiler kind of like what we did for RC a long time ago. I think the fix would go here: https://github.com/microsoft/vscode-cmake-tools/blob/d860f0859de84de5babc043f5896d1f6a3033be0/src/cpptools.ts#L434

tristanlabelle commented 1 month ago

Ping @gcampbell-msft !

v-frankwang commented 1 month ago

@tristanlabelle When we use the reproduction steps you provided to reproduce the issue, we get this error, can you give some advice?

image

tristanlabelle commented 1 month ago

@v-frankwang , it looks like you don't have Swift installed as in my repro steps. where.exe swiftc.exe in the terminal should print the path to the swiftc compiler

v-frankwang commented 1 month ago

@tristanlabelle I still get the error after installing the Swift compiler. Error message.txt

tristanlabelle commented 1 month ago

@v-frankwang please make sure to open VS Code from a VS Developer Command prompt which reports x64 for both host and target:

C:\Code>set VSCMD_ARG
VSCMD_ARG_HOST_ARCH=x64
VSCMD_ARG_TGT_ARCH=x64

You might want to use the explicit x64 Native Tools Command Prompt for VS 2022 shortcut in the start menu.

gcampbell-msft commented 1 month ago

@tristanlabelle For the fix that @bobbrow pointed out, is the filegroup language for swift simply "swift"?

tristanlabelle commented 1 month ago

@gcampbell-msft , I don't know what that filegroup concept is. It doesn't seem to come from CMake, from a code search, so I assume it comes from the CMake Tools extension?

gcampbell-msft commented 1 month ago

@tristanlabelle I'll investigate, thanks.

gcampbell-msft commented 1 month ago

@tristanlabelle Could you test out the fix in this vsix?

cmake-tools.zip

You'll have to change the file extension from .zip to .vsix after downloading.

tristanlabelle commented 1 month ago

@gcampbell-msft Sorry for the wait. Yup! That fixed it with my repro. Mind @'ing me in the PR?