zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
49.77k stars 3.06k forks source link

Confusion with clangd in C++ files. #19126

Closed omennia closed 1 month ago

omennia commented 1 month ago

Check for existing issues

Describe the bug / provide steps to reproduce it

I really love zed as an editor, but I need to use it to edit singular cpp files. I need some features that are only available to the GNU compiler.

Although I can use the gnu compiler to compile and run my code, I am using the clangd language server. It complains that it can't find libraries and everything that is only gnu related. Is there any way around this? Is it possible to make clangd recognize the gnu libraries?

I have searched everywhere and can't seem to find a definitive answer.

Environment

Here are my current settings:

{
  "assistant": {
    "default_model": {
      "provider": "zed.dev",
      "model": "claude-3-5-sonnet-20240620"
    },
    "version": "2"
  },
  "theme": "Ayu Dark",
  "languages": {
    "C++": {
      "compiler_path": "g++-14",
      "extra_clang_arguments": [
        "-std=c++17",
        "-isystem${workspaceFolder}/**",
        "-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
        "-isystem/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin23/14/../../../../../../include/c++/14",
        "-isystem/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin23/14/../../../../../../include/c++/14/aarch64-apple-darwin23",
        "-isystem/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin23/14/../../../../../../include/c++/14/backward",
        "-isystem/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin23/14/include",
        "-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include"
      ]
    }
  },
  "lsp": {
    "clangd": {
      "binary": {
        "path": "/usr/bin/clangd",
        "arguments": [
          "--background-index",
          "--clang-tidy",
          "--completion-style=detailed",
          "--header-insertion=iwyu",
          "--header-insertion-decorators=0",
          "--suggest-missing-includes",
          "--cross-file-rename",
          "--header-insertion=iwyu",
          "--header-insertion-decorators=0",
          "--suggest-missing-includes",
          "--cross-file-rename",
          "--compile-commands-dir=${workspaceFolder}"
        ]
      }
    }
  }
}

I have tried to use g++-14 as the path in the clangd, but I get errors like:

Language server error: clangd

oneshot canceled
-- stderr--

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your Zed.log file to this issue.

Zed.log


notpeter commented 1 month ago
    "-isystem${workspaceFolder}/**",

      "--compile-commands-dir=${workspaceFolder}"

workspaceFolder is a vscode specific variable which does not exist in Zed.

omennia commented 1 month ago

Thank you, that last bit must have been added by GitHub copilot.

Right now I have created a file in ~/Library/Preferences/clangd/config.yaml, with the following structure:

CompileFlags:
  Add:
    [
      --gcc-toolchain=g++-14,
      -std=gnu++17,
      bugprone-*,
      modernize*,
      -Wno-error=conflicting-declarations,
      -Wno-conflicting-declarations,
      -Wl,
      -ld_classic,
      -I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/../../../../../../include/c++/14,
      -I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/../../../../../../include/c++/14/aarch64-apple-darwin24,
      -I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/../../../../../../include/c++/14/backward,
      -I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/include,
      -I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/include-fixed,
      # # -I/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/usr/include,
      # # -I/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks,
    ]
  Remove:   
      [
        Library/Developer/CommandLineTools/usr/lib/clang/16/include,
        Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include,
        Library/Developer/CommandLineTools/usr/include,
        Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks,
      ]
  Compiler: g++-14

And I am still getting an error like:
clang: In included file: declaration conflicts with target of using declaration already in scope

I don't understand what I am doing wrong. Everything works flawlessly in VSCode and Sublime Text.

omennia commented 1 month ago

I have managed to temporarily solve the issue by using ccls instead of clang.

I have created the .ccls file in my project root directory with the following content:

g++-14
%c -std=c17
%c -std=c++17
%h %hpp --include=Global.h
-I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/../../../../../../include/c++/14
-I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/../../../../../../include/c++/14/aarch64-apple-darwin24
-I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/../../../../../../include/c++/14/backward
-I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/include
-I/opt/homebrew/Cellar/gcc/14.2.0/bin/../lib/gcc/current/gcc/aarch64-apple-darwin24/14/include-fixed

And I have changed my lsp settings in Zed to:

"lsp": {
    "clangd": {
      "binary": {
        "path": "/opt/homebrew/bin/ccls"
      }
    }
  }

It no longer gives import conflict errors, but I am also no longer using clangd. If anyone finds a fix for clangd, please let me know.

notpeter commented 1 month ago

Ok, I'm going to go ahead and close. Without a minimal repo that triggers the problem, it isn't really possible to reproduce your issue nor suggest what change(s) would be required to make it work.

Glad you were able to switch to using ccls. I knew it was an LSP that had GCC-specific support (unlike clangd) but I wasn't aware of anyone else using it with Zed. Exciting that you got it working!