devenv icon indicating copy to clipboard operation
devenv copied to clipboard

clangd can't resolve standard libraries when languages.cplusplus is enabled

Open Redhawk18 opened this issue 1 year ago • 8 comments

Describe the bug I added a nix flake to this project, but I need to still run nix-shell -p clang-tools everytime or I get import errors. Does anyone know why?

To reproduce https://github.com/Redhawk18/RandomX/blob/master/flake.nix

Redhawk18 avatar Aug 15 '24 16:08 Redhawk18

@Redhawk18, could you please post the error message you're seeing and the steps needed to reproduce it?

sandydoo avatar Aug 16 '24 07:08 sandydoo

@Redhawk18, could you please post the error message you're seeing and the steps needed to reproduce it?

I get import errors, and the steps are that flake since nix is reproducible.

Redhawk18 avatar Aug 16 '24 19:08 Redhawk18

@Redhawk18

clangd works for me without doing the nix-shell step which is a tool which comes under clang-tools.

[k3ys@nixos:~/sandbox/cachix/issues/flakes]$ clangd
clangd is a language server that provides IDE-like features to editors.

It should be used via an editor plugin rather than invoked directly. For more information, see:
https://clangd.llvm.org/
https://microsoft.github.io/language-server-protocol/

clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment variable.

I[02:12:23.767] clangd version 18.1.8
I[02:12:23.767] Features: linux
I[02:12:23.767] PID: 210840
I[02:12:23.767] Working directory: /home/k3ys/sandbox/cachix/issues/flakes
I[02:12:23.767] argv[0]: clangd
I[02:12:23.767] Starting LSP over stdin/stdout

You should post the specific import error that you mentioned, otherwise we won't know what's the cause of the error.

k3yss avatar Aug 16 '24 20:08 k3yss

@Redhawk18

clangd works for me without doing the nix-shell step which is a tool which comes under clang-tools.

[k3ys@nixos:~/sandbox/cachix/issues/flakes]$ clangd
clangd is a language server that provides IDE-like features to editors.

It should be used via an editor plugin rather than invoked directly. For more information, see:
https://clangd.llvm.org/
https://microsoft.github.io/language-server-protocol/

clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment variable.

I[02:12:23.767] clangd version 18.1.8
I[02:12:23.767] Features: linux
I[02:12:23.767] PID: 210840
I[02:12:23.767] Working directory: /home/k3ys/sandbox/cachix/issues/flakes
I[02:12:23.767] argv[0]: clangd
I[02:12:23.767] Starting LSP over stdin/stdout

You should post the specific import error that you mentioned, otherwise we won't know what's the cause of the error.

standard cpp files that are apart of the c++ standard libraries are saying not found

[START][2024-08-16 16:59:43] LSP logging initiated
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.738] clangd version 18.1.8\nI[16:59:43.738] Features: linux\nI[16:59:43.738] PID: 65359\nI[16:59:43.738] Working directory: /home/redhawk/code/RandomX\nI[16:59:43.738] argv[0]: /nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd\nI[16:59:43.738] argv[1]: --header-insertion=never\nI[16:59:43.738] argv[2]: -j\nI[16:59:43.738] argv[3]: 16\nI[16:59:43.738] argv[4]: --background-index\nI[16:59:43.739] Starting LSP over stdin/stdout\nI[16:59:43.741] <-- initialize(1)\nI[16:59:43.746] --> reply:initialize(1) 4 ms\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.753] <-- initialized\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.753] <-- textDocument/didOpen\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.754] Failed to find compilation database for /home/redhawk/code/RandomX/src/dataset.hpp\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.754] ASTWorker building file /home/redhawk/code/RandomX/src/dataset.hpp version 0 with command clangd fallback\n[/home/redhawk/code/RandomX/src]\n/nix/store/v39mysip3yjpvddmsyj6pfvdhwgmak4m-clang-wrapper-18.1.8/bin/clang -resource-dir=/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/lib/clang/18 -- /home/redhawk/code/RandomX/src/dataset.hpp\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.754] <-- textDocument/semanticTokens/full(2)\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.800] Built preamble of size 283880 for file /home/redhawk/code/RandomX/src/dataset.hpp version 0 in 0.04 seconds\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.800] --> workspace/semanticTokens/refresh(0)\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.800] <-- $/cancelRequest\nI[16:59:43.800] <-- textDocument/semanticTokens/full(3)\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.800] <-- reply(0)\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"E[16:59:43.815] IncludeCleaner: Failed to get an entry for resolved path : No such file or directory\nE[16:59:43.815] IncludeCleaner: Failed to get an entry for resolved path : No such file or directory\nE[16:59:43.815] IncludeCleaner: Failed to get an entry for resolved path : No such file or directory\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.817] --> textDocument/publishDiagnostics\n"
[ERROR][2024-08-16 16:59:43] .../vim/lsp/rpc.lua:734	"rpc"	"/nix/store/3jlclvmii9b6qmpqw9w09cp3zdnngvhk-clang-18.1.8/bin/clangd"	"stderr"	"I[16:59:43.817] --> reply:textDocument/semanticTokens/full(2) 63 ms, error: Task was cancelled.\nI[16:59:43.818] --> reply:textDocument/semanticTokens/full(3) 17 ms\n"

image

All of this is fixed if I hope a devshell with clang-tools But adding it to my flake does not change anything

Redhawk18 avatar Aug 16 '24 21:08 Redhawk18

@Redhawk18, could you please post the error message you're seeing and the steps needed to reproduce it?

I get import errors, and the steps are that flake since nix is reproducible.

...and it builds fine on my machine 🤷 which is why posting a flake and saying "it doesn't work" is not enough.

A friendly reminder that this is an open-source project. If you're seeking help to debug an issue that you are having, then the least you can do is post the actual error, what you did to get there, and how you've tried to fix or debug it. Otherwise, you're wasting everyone's time by making us guess at what the issue is.

sandydoo avatar Aug 17 '24 08:08 sandydoo

Disabling languages.cplusplus fixes the clangd errors. Looks like it's because clang is added to packages, whereas the project expects gcc.

sandydoo avatar Aug 17 '24 08:08 sandydoo

I found a good write up of this issue: https://blog.kotatsu.dev/posts/2024-04-10-nixpkgs-clangd-missing-headers/

We're picking up the unwrapped clangd from clang, instead of clang-tools because of the ordering of packages. I don't know enough about C++ development to make a judgement call. Though I think clang should be removed from packages.

➜ whereis clangd
clangd: /nix/store/j0k075wkfs3dzy5xgwschji29dqh2z26-clang-18.1.8/bin/clangd /nix/store/zkdmhpcn50nfvf8xcp0x2cv4n80ma4x8-clang-tools-18.1.8/bin/clangd

sandydoo avatar Aug 19 '24 13:08 sandydoo

I also have the same problem, that clangd can't find the standard libraries when using vscode and clangd plugin. However, when I add clang-tools and set languages.cplusplus.enable = false, it works perfectly for me.

{
  inputs = {
    nixpkgs.url = "https://mirrors.ustc.edu.cn/nix-channels/nixpkgs-unstable/nixexprs.tar.xz";
    devenv.url = "github:cachix/devenv";
    devenv.inputs.nixpkgs.follows = "nixpkgs";
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    trusted-substituters = "https://devenv.cachix.org";
  };

  outputs = { self, nixpkgs, devenv, ... } @ inputs:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
    in {
      packages.${system}.devenv-up = self.devShells.${system}.default.config.procfileScript;

      devShells.${system}.default = devenv.lib.mkShell {
        inherit inputs pkgs;
        # Import devenv shell modules
        modules = [
          # ./FPGA.nix

          ({ config, pkgs, ... }: {
            # Add packages to the dev environment
            packages = with pkgs; [ 
              # FPGA
              verilator
              systemc
              verible
              svls
    
              # C/C++
              gnumake
              xmake
              gcc
              # clang-tools
            ];
  
            # Enable languages support
            languages.cplusplus.enable = true;
  
            # When enter shell, exec ...
            enterShell = ''
              export SYSTEMC_INCLUDE="${pkgs.systemc}/include"
              export SYSTEMC_LIBDIR="${pkgs.systemc}/lib"
              export VERILATOR_INCLUDE="${pkgs.verilator}/share/verilator/include"
              echo
              verilator --version
              echo
              gcc --version
            '';
          })
        ];
      };
    };
}

SikongJueluo avatar Sep 15 '24 12:09 SikongJueluo

There is an issue on NixOS/nixpkgs regarding this specific error. https://github.com/NixOS/nixpkgs/issues/76486

The solution would be to add clang-tools with lib.mkBefore, which would add it earlier than clang(@spearman https://github.com/NixOS/nixpkgs/issues/76486) into packages, like this:

{pkgs, lib, ...}: {
languages.cplusplus.enable = true;
packages = with pkgs; lib.mkBefore [ clang-tools ];
}

Emtyffx avatar Jul 14 '25 17:07 Emtyffx

There is an issue on NixOS/nixpkgs regarding this specific error. NixOS/nixpkgs#76486

The solution would be to add clang-tools with lib.mkBefore, which would add it earlier than clang(@spearman NixOS/nixpkgs#76486) into packages, like this:

{pkgs, lib, ...}: {
languages.cplusplus.enable = true;
packages = with pkgs; lib.mkBefore [ clang-tools ];
}```

This worked for me, and it seems like the simplest solution. Could it be added to the current C++ module?

2coffee2crab avatar Jul 19 '25 15:07 2coffee2crab