microsoft / vscode-cmake-tools

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

Go to Definition on a function callsite goes to the header declaration instead of its implementation after upgrading vscode-cmake-tools from 1.7.3 to 1.8.1 #2108

Open aleun opened 3 years ago

aleun commented 3 years ago

Brief Issue Summary

When using vscode-cpptools version 1.6.0, Go to Definition on a function callsite goes to the header declaration instead of its implementation after upgrading vscode-cmake-tools from 1.7.3 to 1.8.1.

I am not sure if this issue belongs in this repo or vscode-cpptools, but let's start here.

I have a minimal embedded C project that I'm able to repro consistently against: https://github.com/aleun/cmake-cpp-repro. It is self-contained and includes the ARM GCC, CMake, and Ninja Windows binaries I used.

Expected:

  1. Clone repo https://github.com/aleun/cmake-cpp-repro
  2. Open VS Code in nxp directory: code nxp
  3. Open main.c
  4. Put cursor on run_foo
  5. Right Click -> Go to Definition
  6. Cursor is now at run_foo definition in foo.c.

cpp_cmake_repro173

Apparent Behavior:

  1. Clone repo https://github.com/aleun/cmake-cpp-repro
  2. Open VS Code in nxp directory: code nxp
  3. Open main.c
  4. Put cursor on run_foo
  5. Right Click -> Go to Definition
  6. Cursor is now at run_foo in foo.h, not foo.c.

cpp_cmake_repro181

C/C++: Log Diagnostics Output

Here are the log outputs for both 1.7.3 and 1.8.1. browsePath is empty in the latter.

CMake Tools 1.7.3

-------- Diagnostics - 9/2/2021, 3:15:02 PM
Version: 1.6.0
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "${workspaceFolder}/**"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.19041.0",
    "compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.30.30423\\bin\\Hostx64\\x64\\cl.exe",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "windows-msvc-x64",
    "compilerArgs": [],
    "intelliSenseModeIsExplicit": false,
    "cStandardIsExplicit": false,
    "cppStandardIsExplicit": false,
    "compilerPathIsExplicit": false,
    "configurationProvider": "ms-vscode.cmake-tools",
    "browse": {
        "path": [
            "${workspaceFolder}/**"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Custom browse configuration:
{
    "browsePath": [
        "c:/proj/cmake-cpp-repro/core",
        "c:/proj/cmake-cpp-repro/nxp",
        "c:/proj/cmake-cpp-repro/nxp/build/core/cmakefiles/core.dir"
    ],
    "standard": "gnu99",
    "compilerPath": "c:/proj/cmake-cpp-repro/nxp/artifacts/compilers/arm/gcc/2020.10.0/bin/arm-none-eabi-gcc.exe",
    "compilerArgs": [
        "--specs=nano.specs",
        "-ffunction-sections",
        "-fdata-sections",
        "-fno-strict-aliasing",
        "-fno-builtin",
        "-fshort-enums",
        "-Wall",
        "-Wextra",
        "-Wuninitialized",
        "-Wdouble-promotion",
        "-Werror",
        "-Wno-unused-parameter",
        "-O0",
        "-g3",
        "-std=gnu99"
    ]
}
Custom configurations:
[ C:\proj\cmake-cpp-repro\nxp\main.c ]
{
    "defines": [],
    "standard": "gnu99",
    "includePath": [
        "c:/proj/cmake-cpp-repro/nxp",
        "c:/proj/cmake-cpp-repro/core"
    ],
    "compilerPath": "c:/proj/cmake-cpp-repro/nxp/artifacts/compilers/arm/gcc/2020.10.0/bin/arm-none-eabi-gcc.exe",
    "compilerArgs": [
        "--specs=nano.specs",
        "-ffunction-sections",
        "-fdata-sections",
        "-fno-strict-aliasing",
        "-fno-builtin",
        "-fshort-enums",
        "-Wall",
        "-Wextra",
        "-Wuninitialized",
        "-Wdouble-promotion",
        "-Werror",
        "-Wno-unused-parameter",
        "-O0",
        "-g3",
        "-std=gnu99"
    ]
}
Translation Unit Mappings:
[ C:\proj\cmake-cpp-repro\nxp\main.c ]:
    C:\PROJ\CMAKE-CPP-REPRO\NXP\MAIN.C
Translation Unit Configurations:
[ C:\proj\cmake-cpp-repro\nxp\main.c ]:
    Process ID: 29208
    Memory Usage: 13 MB
    Compiler Path: c:/proj/cmake-cpp-repro/nxp/artifacts/compilers/arm/gcc/2020.10.0/bin/arm-none-eabi-gcc.exe
    Includes:
        C:\PROJ\CMAKE-CPP-REPRO\NXP
        C:\PROJ\CMAKE-CPP-REPRO\CORE
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\ARM-NONE-EABI\INCLUDE\NEWLIB-NANO
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\LIB\GCC\ARM-NONE-EABI\10.2.1\INCLUDE
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\LIB\GCC\ARM-NONE-EABI\10.2.1\INCLUDE-FIXED
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\ARM-NONE-EABI\INCLUDE
    Standard Version: c99
    IntelliSense Mode: windows-gcc-arm
    Other Flags:
        --gcc
        --gnu_version=100201
Total Memory Usage: 13 MB

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

CMake Tools 1.8.1

-------- Diagnostics - 9/2/2021, 3:13:50 PM
Version: 1.6.0
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "${workspaceFolder}/**"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.19041.0",
    "compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.30.30423\\bin\\Hostx64\\x64\\cl.exe",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "windows-msvc-x64",
    "compilerArgs": [],
    "intelliSenseModeIsExplicit": false,
    "cStandardIsExplicit": false,
    "cppStandardIsExplicit": false,
    "compilerPathIsExplicit": false,
    "configurationProvider": "ms-vscode.cmake-tools",
    "browse": {
        "path": [
            "${workspaceFolder}/**"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Custom browse configuration:
{
    "browsePath": []
}
Custom configurations:
[ C:\proj\cmake-cpp-repro\nxp\main.c ]
{
    "defines": [],
    "standard": "gnu99",
    "includePath": [
        "c:/proj/cmake-cpp-repro/nxp",
        "c:/proj/cmake-cpp-repro/core"
    ],
    "compilerPath": "c:/proj/cmake-cpp-repro/nxp/artifacts/compilers/arm/gcc/2020.10.0/bin/arm-none-eabi-gcc.exe",
    "compilerArgs": [
        "--specs=nano.specs",
        "-ffunction-sections",
        "-fdata-sections",
        "-fno-strict-aliasing",
        "-fno-builtin",
        "-fshort-enums",
        "-Wall",
        "-Wextra",
        "-Wuninitialized",
        "-Wdouble-promotion",
        "-Werror",
        "-Wno-unused-parameter",
        "-O0",
        "-g3",
        "-std=gnu99"
    ]
}
Translation Unit Mappings:
[ C:\proj\cmake-cpp-repro\nxp\main.c ]:
    C:\PROJ\CMAKE-CPP-REPRO\NXP\MAIN.C
Translation Unit Configurations:
[ C:\proj\cmake-cpp-repro\nxp\main.c ]:
    Process ID: 8724
    Memory Usage: 13 MB
    Compiler Path: c:/proj/cmake-cpp-repro/nxp/artifacts/compilers/arm/gcc/2020.10.0/bin/arm-none-eabi-gcc.exe
    Includes:
        C:\PROJ\CMAKE-CPP-REPRO\NXP
        C:\PROJ\CMAKE-CPP-REPRO\CORE
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\ARM-NONE-EABI\INCLUDE\NEWLIB-NANO
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\LIB\GCC\ARM-NONE-EABI\10.2.1\INCLUDE
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\LIB\GCC\ARM-NONE-EABI\10.2.1\INCLUDE-FIXED
        C:\PROJ\CMAKE-CPP-REPRO\NXP\ARTIFACTS\COMPILERS\ARM\GCC\2020.10.0\ARM-NONE-EABI\INCLUDE
    Standard Version: c99
    IntelliSense Mode: windows-gcc-arm
    Other Flags:
        --gcc
        --gnu_version=100201
Total Memory Usage: 13 MB

Platform and Versions

// Output of VS Code Help > About page:
Version: 1.60.0 (user setup)
Commit: e7d7e9a9348e6a8cc8c03f877d39cb72e5dfb1ff
Date: 2021-09-01T10:41:52.311Z
Electron: 13.1.8
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.19043
$ code --list-extensions --show-versions | rg "cmake|cpp"
ms-vscode.cmake-tools@1.8.1
ms-vscode.cpptools@1.6.0
twxs.cmake@0.0.17
bobbrow commented 3 years ago

Hey @aleun, thanks for the detailed repro steps and logs! It seems that the browsePath is not being set correctly in your second set of logs and the result is that the source file you're trying to navigate to has not been processed by cpptools yet so it is unaware of the definition.

The disappointing part is that I am unable to repro this under a debugger. The first time I loaded your project in VS Code I was able to reproduce the empty browsePath, but after debugging and reloading the project without a debugger attached, I no longer have a repro (even when I delete my cached configuration and reconfigure). Is this 100% repro for you?

aleun commented 3 years ago

I just tried opening and closing the project multiple times, with and without deleting the cache.

It repros 100% for me on first opening the project with no cache.

However, I have noticed that if I reopen the project without clearing the cache, after some non-deterministic number of attempts to navigate, it will eventually go to the correct definition. I managed to capture this behavior. In the video, I delete the cache, reload, navigate to definition several times (all times go to foo.h). Then I reload, navigate to definition several times again (the final attempt goes to foo.c).

cmake-cpp-repro

bobbrow commented 3 years ago

Attempting to do the goto definition quickly after loading the folder will be met with some non-determinism as the language server starts up and figures out if paths have changed, etc. When all of that settles (e.g. fire and DB icons are gone from the status bar), then you can be sure that the extension is fully loaded and ready. I'm unable to see the full status bar in your GIF, so I don't know. The most important part is what you see in the Log Diagnostics output.

aleun commented 3 years ago

Attempting to do the goto definition quickly after loading the folder will be met with some non-determinism as the language server starts up and figures out if paths have changed, etc. When all of that settles (e.g. fire and DB icons are gone from the status bar), then you can be sure that the extension is fully loaded and ready.

I just tried again and made sure the fire and DB icons were gone. I'm seeing the same behavior consistently: if it's the first project load with no cache, then the browsePath is empty, but on subsequent reload, the browsePath contains the expected paths.

psbanga commented 2 years ago

For me I am able to reproduce 100% of the time. Haven't tried to clear the logs. But i shutdown, open again, even restart system, it is 100% reproducible. Uninstalled cmake tools, restart vscode, no longer reproducible.