Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get csharp-ls language server in neovim to find the root of my project?

I'm trying to set up the csharp-ls LSP for use in neovim for C# files. My init.lua file looks like this:

vim.lsp.config(
    'csharp-ls',
    {
        cmd = { 'csharp-ls' },
        filetypes = { 'cs' },
        root_markers = { '*.sln', '*.csproj' },
    }
)

vim.lsp.enable('csharp-ls', false)
vim.lsp.enable('csharp-ls', true)

I'm opening a file called Program.cs in a directory that has these files:

Console1.csproj
Console1.sln
Program.cs

When I run :checkhealth in neovim I'm getting this showing that root is nil:

vim.lsp: Active Clients ~
- csharp-ls (id: 1)
  - Version: 0.18.0.0
  - Root directory: nil
  - Command: { "csharp-ls" }
  - Settings: {}
  - Attached buffers: 1

vim.lsp: Enabled Configurations ~
- csharp-ls:
  - cmd: { "csharp-ls" }
  - filetypes: cs
  - root_markers: *.sln, *.csproj

How do I get it to detect the .sln file as the root of the project?

like image 336
Martin Brown Avatar asked Oct 24 '25 03:10

Martin Brown


1 Answers

The root_markers property does not support globs. So if you have a file called Console1.sln *.sln is not going to find it. The way I found to work around this is to set the root directory to a function like this:

vim.lsp.config(
  'csharp-ls',
  {
    cmd = { 'csharp-ls' },
    filetypes = { 'cs' },
    root_dir = vim.fs.root(
      0, 
      function(name, path)
        return name:match('%.sln$') ~= nil
      end)
  }
)
like image 157
Martin Brown Avatar answered Oct 26 '25 11:10

Martin Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!