Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSInstall errors with clang set as compiler #1449

Closed
bhavitsharma opened this issue Jun 29, 2021 · 14 comments
Closed

TSInstall errors with clang set as compiler #1449

bhavitsharma opened this issue Jun 29, 2021 · 14 comments
Labels
bug Something isn't working

Comments

@bhavitsharma
Copy link

Describe the bug

TSInstall cpp fails to compile when using clang as a compiler on nixos.

To Reproduce
env CC=$(which clang) nvim '+TSInstallSync cpp'

Expected behavior
Should compile

Output of :checkhealth nvim_treesitter

Output of nvim --version

NVIM v0.5.0-dev Build type: Release LuaJIT 2.1.0-beta3 Compilation: Compiled by nixbld

Features: +acl +iconv +tui
See ":help feature-compile"

system vimrc file: "$VIM/sysinit.vim"
fall-back for $VIM: "
/nix/store/bqfq1sz0ifjf6gnz6j6iqx5015p4yz1d-neovim-unwrapped-master/share/nvim

Run :checkhealth for more info

Additional context

src/scanner.cc:2:10: fatal error: 'string' file not found                                       
#include <string>                                                                               
         ^~~~~~~~                                                                               
1 error generated.                                                                              
Error detected while processing command line:                                                   
Error during compilation                                                                        
Failed to execute the following command:                                                        
{                                                                                               
  cmd = "clang",                                                                                
  err = "Error during compilation",                                                             
  info = "Compiling...",                                                                        
  opts = {                                                                                      
    args = { "-o", "parser.so", "-I./src", "src/parser.c", "src/scanner.cc", "-shared", "-Os", "
-lstdc++", "-fPIC" },                                                                           
    cwd = "/home/bhavit/.local/share/nvim/tree-sitter-cpp"                                      
  }                                                                                             
}    

I am using homemanager for managing my dotfiles and here's my config for nvim.nix.

home.packages = with pkgs; [
    neovim-nightly
    clang-tools
    rls
    sumneko-lua-language-server
    fzf
    ripgrep
    tree-sitter
    llvmPackages_12.llvm
    clang_12
    haskell-language-server
    clangStdenv
    clangMultiStdenv
];
xdg.configFile."nvim" = {
    source = ../nvim;
    recursive = true;
};
@bhavitsharma bhavitsharma added the bug Something isn't working label Jun 29, 2021
@bhavitsharma
Copy link
Author

I think this is a problem with clang. When I tried to clone the repo and compile using clang -o parser.so -I./src src/parser.c src/scanner.cc -lstdc++ -fPIC, it gave the same error

@theHamsta
Copy link
Member

theHamsta commented Jun 29, 2021

Apple Clang? It's somehow not entering C++ mode. But you have GCC's stdc++ installed?

@bhavitsharma
Copy link
Author

Stupid me: using clang++ worked. require 'nvim-treesitter.install'.compilers = { 'clang++'} worked.

@theHamsta
Copy link
Member

@bhavitsharma But it should also work with clang. clang++ will not always handle C files so you've got the inverse problem.

@bhavitsharma
Copy link
Author

bhavitsharma commented Jun 29, 2021

I guess then it's a problem with my nix configuration rather than treesitter because I can't compile even this with clang.

#include <string>
int main() { 
return 0;
}

clang temp.cpp. Using clang++ works fine.

@theHamsta
Copy link
Member

But it can happen that clang++ fails with CStruct = { .foo=1, .bar=2 }

@bhavitsharma
Copy link
Author

I think C++ 17 has supports for designated initializers for struct :) but I get your point. I am not sure if the issue with clang is reproducible in other platforms? Quick internet search reveals to use clang -x c++ but I am not sure. The general recommendation to compile c++ files is to use clang++ instead of clang.

@YodaEmbedding
Copy link

YodaEmbedding commented Oct 26, 2021

I'm on Neovim 0.5 with compat branch.

  • Arch Linux: no installation problems.
  • NixOS: can't compile because of missing C++ headers (cwctype, algorithm, ...).

Workaround

Create a default.nix:

{ pkgs ? import <nixpkgs> {} }:

with pkgs;
pkgs.mkShell {
  buildInputs = [
    gcc
    nodejs
    neovim
    tree-sitter
    (python39.withPackages (pp: with pp; [
      pynvim
    ]))
  ];
}

Run nix-shell --command 'nvim' and run :TSInstall all.


Old instructions:
require("nvim-treesitter.install").compilers = { "clang++" }

require("nvim-treesitter.configs").setup {
  ensure_installed = "all",
}

This worked for all language parsers except tree-sitter-rst, tree-sitter-svelte, and tree-sitter-vim. For those, I had to comment out the line:

-- require("nvim-treesitter.install").compilers = { "clang++" }

...and ran nvim again so that the remaining three also installed.


Discussion

My NixOS configuration was pretty vanilla (just installing packages), so I think the culprit is somewhere in nvim-treesitter. Outside of nvim, running cargo build --release on each of the parsers individually successfully builds them without errors. ...So evidently, it's not really the fault of the tree-sitter-<lang> repositories themselves.


Relevant thread: https://www.reddit.com/r/neovim/comments/oa3xq8/how_to_install_nvimtreesitter_on_nixos/

@YodaEmbedding
Copy link

YodaEmbedding commented Nov 2, 2021

I found a way to reproduce the error. If you have clang/glibc/musl installed, everything goes haywire.

{ pkgs ? import <nixpkgs> {} }:

with pkgs;
pkgs.mkShell {
  buildInputs = [
    gcc
    nodejs
    neovim
    tree-sitter
    (python39.withPackages (pp: with pp; [
      pynvim
    ]))

    # Including this suddenly breaks TSInstall:
    clang glibc musl
  ];
}

Everything works fine without clang/glibc/musl.


Should this bug be reopened?

@theHamsta
Copy link
Member

@bhavitsharma only in C++20. There is an open PR to use makefiles to compile c++ with clang++ and the C files with clang. But with only one command you can only use clamg

YodaEmbedding added a commit to YodaEmbedding/dotfiles that referenced this issue Mar 14, 2022
@Pablo1107
Copy link

Maybe this issue should still be open? I know there are workarounds, but should treesitter compile C++ files with clang++ instead of clang to avoid this issue?

@Luvjeet
Copy link

Luvjeet commented Mar 1, 2023

Stupid me: using clang++ worked. require 'nvim-treesitter.install'.compilers = { 'clang++'} worked.

where to write this code in init.lua?

@dongdongbh
Copy link

It worked after I changed my GCC version from gcc-9 to gcc-10.3. This may be helpful for anyone else who is experiencing the same problem.

@hallettj
Copy link

hallettj commented Aug 6, 2023

I'm another NixOS user, and I'm installing plugins with lazy.nvim instead of through Nix. If you got here because you are in a similar situation, and you are getting oodles of errors when you start neovim, my workaround is to avoid the whole clang issue by setting the grammar compiler to gcc.

I have this in my plugin config - since I'm using lazy.nvim I put this in the config callback for my nvim-treesitter plugin config (see in context):

require('nvim-treesitter.install').compilers = { 'gcc' }

And to make sure that gcc is available while running neovim I have neovim installed through Home Manager like this (see in context):

  programs.neovim = {
    enable = true;
    defaultEditor = true;
    withPython3 = true;
    extraPackages = with pkgs; [
      gcc
      # ...
    ];
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants