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

LSP: filetype sent as language id breaks sourcekit-lsp #3264

Open
achilleas-k opened this issue Aug 6, 2024 · 9 comments
Open

LSP: filetype sent as language id breaks sourcekit-lsp #3264

achilleas-k opened this issue Aug 6, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@achilleas-k
Copy link

Problem

I'm having trouble getting the lsp to work with objective c files and sourcekit-lsp. The filetype is detected as objc, but it seems sourcekit-lsp calls it objective-c. Adding objc to the filetypes in the config enables the connection with the lsp, but no lsp-based features actually work (completion, inline warnings, etc).

I suspect the issue might be that sourcekit-lsp expects the filetype to be objective-c and neovim is passing objc.

Steps to reproduce using "nvim -u minimal_init.lua"

Here's a minimal config:

-- init.lua  
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"  
if not vim.loop.fs_stat(lazypath) then  
   vim.fn.system({  
       "git",  
       "clone",  
       "--filter=blob:none",  
       "https://github.com/folke/lazy.nvim.git",  
       "--branch=stable",  
       lazypath  
   })  
end  
vim.opt.rtp:prepend(lazypath)  
require("lazy").setup({  
   "neovim/nvim-lspconfig",  
   config = function()  
       local lspconfig = require('lspconfig')  
       lspconfig.sourcekit.setup {  
           capabilities = {  
               workspace = {  
                   didChangeWatchedFiles = {  
                       dynamicRegistration = true,  
                   },  
               },  
           },  
       }  
  
       vim.api.nvim_create_autocmd('LspAttach', {  
           desc = "LSP Actions",  
           callback = function(args)  
               vim.keymap.set("n", "K", vim.lsp.buf.hover, {noremap = true, silent = true})  
               vim.keymap.set("n", "gd", vim.lsp.buf.definition, {noremap = true, silent = true})  
           end,  
       })  
   end,  
})

and launching nvim with that config on a .m objc file shows the following :LspInfo

Language client log: /Users/achilleas/.local/state/nvim-debug/lsp.log
Detected filetype:   objc

0 client(s) attached to this buffer:

Configured servers list: sourcekit

Forcing the filetype detection by adding

filetypes = { 'swift', 'c', 'cpp', 'objective-c', 'objc', 'objective-cpp' },

makes it appear like it's working:

Language client log: /Users/achilleas/.local/state/nvim-debug/lsp.log  
Detected filetype:   objc  

1 client(s) attached to this buffer:  

Client: sourcekit (id: 1, bufnr: [1])  
  filetypes:       swift, c, cpp, objective-c, objc, objective-cpp  
  autostart:       true  
  root directory:  /Users/achilleas/projects/nvim-debug  
  cmd:             /usr/bin/sourcekit-lsp  

Configured servers list: sourcekit

but none of the lsp functionality actually works.

The included log file is from after the filetypes line was added.

Expected behavior

Completion and checks from sourcekit-lsp to work with objective c files.

Neovim version (nvim -v)

NVIM v0.10.1

Language server name/version

sourcekit-lsp (part of Xcode 15.4)

Operating system/version

macOS Sonoma 14.5

Log file

https://gist.github.com/achilleas-k/fe2409175dc0be879b729ccf7e7ebdeb

@achilleas-k achilleas-k added the bug Something isn't working label Aug 6, 2024
@clason
Copy link
Member

clason commented Aug 6, 2024

Filetypes are purely a Neovim thing; the server cares not. The issue is usually with project detection; sourcekit is finicky in this regard (read the documentation).

In any case, this issue belongs at nvim-lspconfig (on mobile so can't transfer it now).

@achilleas-k
Copy link
Author

In any case, this issue belongs at nvim-lspconfig (on mobile so can't transfer it now).

Thanks. I was going to post the issue there but the warning in the README made me consider this might be an LSP client issue.

@achilleas-k
Copy link
Author

achilleas-k commented Aug 6, 2024

Filetypes are purely a Neovim thing; the server cares not.

The reason I suspected it might be a language ID issue is that the sourcekit-lsp can handle multiple types.
Poking around the lsp sources, I see a few places where behaviour is switched based on the language:

I have to assume that the language ID that neovim sends through to the lsp must play a role here (and there's no language matching objc).

@clason
Copy link
Member

clason commented Aug 6, 2024

It does not send any language ID at all; that is simply not part of the protocol.

@achilleas-k
Copy link
Author

Right. Thanks for clarifying.

@clason clason transferred this issue from neovim/neovim Aug 6, 2024
@achilleas-k
Copy link
Author

It does not send any language ID at all; that is simply not part of the protocol.

Sorry, I don't know much about the protocol but based on all the references I saw when researching this issue, I wanted to figure out why I thought what I did.

So I went looking and found the spec for the TextDocumentItem, which includes a languageId and the paragraph below that says:

Text documents have a language identifier to identify a document on the server side when it handles more than one language to avoid re-interpreting the file extension. If a document refers to one of the programming languages listed below it is recommended that clients use those ids.
...
Objective-C objective-c

Then based on the fact that the lsp client in neovim just uses the filetype as the language ID, I added the following to my config:

lspconfig.sourcekit.setup {
    filetypes = { 'swift', 'c', 'cpp', 'objective-c', 'objc', 'objective-cpp' },
    get_language_id = function(_, ftype)
        if ftype == "objc" then
            return "objective-c"
        end
        return ftype
    end,
    ...
}

and now the lsp works.

Now I'm happy that my issue was solved, but this tells me that, based on the spec, the lsp client in neovim should be replacing objc with objective-c when talking to the server.

Assuming it does send a language id and I'm not misinterpreting everything above.

@clason clason transferred this issue from neovim/nvim-lspconfig Aug 6, 2024
@clason
Copy link
Member

clason commented Aug 6, 2024

You learn something new every day. Sorry for giving you the runaround!

@clason clason changed the title objc files not working with sourcekit-lsp LSP: filetype sent as language id breaks sourcekit-lsp Aug 6, 2024
@achilleas-k
Copy link
Author

No worries. It was equally educational for me too :)

@mfussenegger mfussenegger transferred this issue from neovim/neovim Aug 9, 2024
@mfussenegger
Copy link
Member

Moved it again to lspconfig because the mapping needs to be added there.
Similar to

local get_language_id = function(_, ftype)

gicmo added a commit to gicmo/dot-files that referenced this issue Sep 3, 2024
Need to work around a bug in  nvim-lspconfig:
  neovim/nvim-lspconfig#3264
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

3 participants