WSL2 neovim editing c++ compiled in msvc2019 with clang10

Wondering if anyone else has got this setup to work with c/c++.

I have nvim installed on WSL2 (ubuntu 20.04) and use it to edit my c++ files, but for compilation/debugging I use msvc2019 with clang10.
The windows clang spits out a compile_commands.json, but it wont be understood by the linux clang language server being used by nvim.

what are my options to address this? is it possible to point nvim to the windows clang language server?

vscode has a nvim plugin and you can tell it to use your wsl2 nvim. having tried it out, the autocomplete and symbol lookup seem to work flawlessly (and makes me cry thinking about what my experience COULD be) (atm, i use ctags/cscope and they are far from flawless). how can i emulate whatever its doing in my nvim wsl2 setup?

going a full windows route is the last option i would consider since windows and its various terminals have always been awful in my experience. I’ve also never been able to compile nvim master on windows.

You should use your build system to generate the compile_commands.json. I had only bad experience on not doing so. For cmake I do use use { 'cdelledonne/vim-cmake', requires = { 'antoinemadec/FixCursorHold.nvim' } } --console window: cg,cb,ci,cq,<C-C>, xor <l>cmg/cmb.

The alternative is to emulate the build system by figuring out all the paths yourself and add them to the compile_commands.json.

use your build system to generate the compile_commands.json

Sry if it wasn’t clear. my project is in fact a cmake project. msvc2019 uses cmake if you have it setup.
The core issue is my nvim is in linux land via WLS2 but the compile_commands.json is generated via a windows version of clang in msvc.

unless you mean using the linux make system to just generate the compile_commands.json for the repo that sits on the /mnt/c/ side. I wanted to try that but was unsure how sustainable that is from a workflow standpoint. double compiling everytime might get old fast. there’s probably a slick way to do a post build thing that calls a script to do the linux compile that trys to only generate a compile_commands.json so that it’s fast and not a noticable overhead (if that’s possible, i dont know cmake enough to know).

msvc has a cross compile feature but im not sure if it uses wsl linux distro to build it, i could try that too i guess.

the other thing i was thinking was to point the nvim linux language server for clang at the windows clang, but i doubt it works even if you (could?) do that?

I think this might work if you turn the paths in compile_commands.json
from windows paths to linux paths (e.g. C:\Users\file
/mnt/c/Users/file).

i managed to build the /mnt/c/ windows repo with linux using clang10. so now i have a compile_commands.json that my linux clangd server can understand.

it looks like my diagnostics went away but it seems most features arent supported or something else is wrong. I can’t jump to definition or autocomplete. for example, if i do this->m_ it will just be a bunch of matches in the file and not any actual members informed by the server.

:lua print(vim.inspect(vim.lsp.buf_get_clients())) does return a big json for clangd so its showing some of life.

In the lspconfig repo it mentions checking the logs so when i call this for the buffer im in (Renderer.cpp)
:lua vim.cmd('e'..vim.lsp.get_log_path())

There are some interesting errors in the log:

/usr/local/share/nvim/runtime/lua/vim/lsp/rpc.lua:458 Updating file /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp with command \n[/mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest]\n/usr/bin/clang -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/glad -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/glfw -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/glm -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/dependencies/glad/include -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/generated -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glad -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glfw -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glm -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/imgui-files -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/stb -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/soil2 -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glfw/include -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glm/glm/.. -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/soil2/src/SOIL2 -isystem /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies -isystem /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/assimp -Wno-error -O3 -DNDEBUG -std=gnu++2a -Winvalid-pch -Xclang -include-pch -Xclang /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/CMakeFiles/GraphicsSelfStudy.dir/cmake_pch.hxx.pch -Xclang -include -Xclang /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/CMakeFiles/GraphicsSelfStudy.dir/cmake_pch.hxx -o CMakeFiles/GraphicsSelfStudy.dir/src/Renderer.cpp.o -c /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp -fsyntax-only -resource-dir=/usr/lib/llvm-10/lib/clang/10.0.0\n"
/usr/local/share/nvim/runtime/lua/vim/lsp/rpc.lua:458 Could not build a preamble for file /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp\n"
/usr/local/share/nvim/runtime/lua/vim/lsp/rpc.lua:458 BeginSourceFile() failed when building AST for /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp\n"

Not sure what the next step would be. on the linux side i was able to compile but not link. not sure if i need to clean that pipe first before i move onto fixing these nvim lsp errors?

This will tell clangd to run with 1 thread and log in verbose mode

nvim_lsp.clangd.setup{
    cmd = { "clangd", "-j=1", "--log=verbose" };
}

doing this the log spits out the fact that when its trying to build the “preamble” it cannot load the include files, at least that’s how i interpret these lines (when it mentions “failed to load shard”)

"I[11:10:54.958] Enqueueing 59 commands for indexing\n"
"I[11:10:54.958] Updating file /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp with command \n[/mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest]\n/usr/bin/clang -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/glad -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/glfw -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/glm -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/dependencies/glad/include -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/generated -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glad -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glfw -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glm -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/imgui-files -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/stb -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/soil2 -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glfw/include -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glm/glm/.. -I/mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/soil2/src/SOIL2 -isystem /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies -isystem /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/assimp -std=c++20 -O3 -DNDEBUG -Winvalid-pch -Xclang -include-pch -Xclang /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/CMakeFiles/GraphicsSelfStudy.dir/cmake_pch.hxx.pch -Xclang -include -Xclang /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/CMakeFiles/GraphicsSelfStudy.dir/cmake_pch.hxx -o /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/CMakeFiles/GraphicsSelfStudy.dir/src/Renderer.cpp.o -c /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp -fsyntax-only -resource-dir=/usr/lib/llvm-10/lib/clang/10.0.0\n"
"V[11:10:54.960] Failed to load shard: /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Plane.cpp\n"
"V[11:10:55.024] Failed to load shard: /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp\n"
"V[11:10:55.028] Failed to load shard: /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/imgui-files/imgui_widgets.cpp\n"
"V[11:10:55.034] Driver produced command: cc1 -cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Renderer.cpp -mrelocation-model static -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -isystem /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies -isystem /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/assimp -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/glad -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/glfw -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/glm -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/dependencies/glad/include -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/generated -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glad -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glfw -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glm -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/imgui-files -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/stb -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/soil2 -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glfw/include -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/glm/glm/.. -I /mnt/c/Users/lol/clones/GraphicsSelfStudy/dependencies/soil2/src/SOIL2 -D NDEBUG -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Winvalid-pch -std=c++20 -fdeprecated-macro -fdebug-compilation-dir /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-loops -vectorize-slp -include-pch /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/CMakeFiles/GraphicsSelfStudy.dir/cmake_pch.hxx.pch -include /mnt/c/Users/lol/clones/GraphicsSelfStudy/buildTest/CMakeFiles/GraphicsSelfStudy.dir/cmake_pch.hxx -faddrsig -x c++ /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp\n"
"V[11:10:55.034] Building first preamble for /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp\n"
"V[11:10:55.044] Failed to load shard: /mnt/c/Users/lol/clones/GraphicsSelfStudy/src/GlobalUniforms.cpp\n"
...and so on

maybe the shard things dont matter

there are some other errors about not being able to parse uri: Failed to parse URI : Scheme must be provided in URI

the uri’s are in this form
uri = "file:///mnt/c/Users/lol/clones/GraphicsSelfStudy/src/Renderer.cpp

actually it looks like its working, i guess the errors weren’t show stopping.

I had to switch to clang 9 to get it to work with:

########################################
### TERMINAL COMMANDS FOR CLANG 9 ######
########################################
sudo apt update -y; sudo apt install -y clang-9 clangd-9; sudo apt remove -y clang-10 clangd-10;
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-9 100
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-9 100
which clang
which clangd

##############################
### CMakeLists.txt section ###
##############################
if(UNIX AND NOT APPLE)
    # on Linux

    # NOTE: also try in terminal:
    # CXX=/usr/bin/clang
    # export CXX

    set(CMAKE_CXX_COMPILER "/usr/bin/clang")
    set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
elseif(WIN32)
     # on Windows
     # do nothing
endif()

#############
## In WSL ###
#############
mkdir buildLinux; cd buildLinux;

## NOTE: Periodically run these three commands when things change
cmake .. -G Ninja (or "Unix Makefiles")
ninja (or make)
cp compile_commands.json ..