Alire + ada_language_server + nvim

Hello,

Has anyone been able to get ada_language_server working with nvim using an Alire project? By ‘working’ I mean use alire to fetch and build a project. Then be able to edit the project source files with language server protocol features (navigation, tooltips, etc.) with nvim.

I used alire to fetch and build the “magic_hat” project using:

alr get mage_hat
cd mage_hat_0.3.0_255dc372
alr build

For nvim, ALS has been installed and my ‘init.lua’ has the following config code in it:

require('lspconfig').als.setup{
    settings = {
        ada = {
            projectFile = "mage_hat.gpr"
        }
    }
}

When I open the ‘mage_hat.adb’ which is in the src folder of the mage_hat alire project I would expect the ALS to be operational but I am seeing errors in the ALS logs:

[ALS.MAIN] On_DidOpenTextDocument_Notification: file:///home/nintendo/Desktop/mage_hat_0.3.0_255dc372/src/mage_hat.adb                                                
  [ALS.IN] {"id":2,"params":{"textDocument":{"uri":"file:\/\/\/home\/nintendo\/Desktop\/mage_hat_0.3.0_255dc372\/src\/mage_hat.adb"}},"jsonrpc":"2.0","method":"textDocument\/semanticTokens\/full"}
  [ALS.MAIN] Document_Tokens_Full_Request: Request  2 file:///home/nintendo/Desktop/mage_hat_0.3.0_255dc372/src/mage_hat.adb                                             
  [ALS.MAIN.EXCEPTIONS] Unexpected exception: raised GNATCOLL.PROJECTS.INVALID_PROJECT : gnatcoll-projects.adb:7714
  _ALS.MAIN.EXCEPTIONS_ [/home/nintendo/.local/share/nvim/mason/bin/../../../../..//.local/share/alire/builds/gnatcoll_23.0.0_29478a20/30653702b467e53c8eee88abe2cde92767b6a0290a3d01a6e2dca2a07b06501b/lib/gnatcoll/relocatable/libgnatcoll.so.23.0.0  ]                                                        
  _ALS.MAIN.EXCEPTIONS_ 0x7d8000a41d02 gnatcoll__projects__load at ???                                                                                                   
  _ALS.MAIN.EXCEPTIONS_ [/home/nintendo/.local/share/nvim/mason/bin/ada_language_server]
  _ALS.MAIN.EXCEPTIONS_ 0x11c7b5a Lsp.Ada_Handlers.Load_Project at lsp-ada_handlers.adb:4877                                                                             
  _ALS.MAIN.EXCEPTIONS_ 0x116379b Lsp.Ada_Handlers.On_Didchangeconfiguration_Notification at lsp-ada_handlers.adb:4492
  _ALS.MAIN.EXCEPTIONS_ 0xe9ed6e Lsp.Messages.Server_Notifications.Visit at lsp-messages-server_notifications.adb:55                                                     
  _ALS.MAIN.EXCEPTIONS_ 0x10a5c6d Lsp.Servers.Processing_Task_Type.Process_Message at lsp-servers.adb:1198
  _ALS.MAIN.EXCEPTIONS_ 0x10a52b6 Lsp.Servers.Processing_Task_TypeT at lsp-servers.adb:1291                                                                              
  _ALS.MAIN.EXCEPTIONS_ [/home/nintendo/.local/share/nvim/mason/bin/../../../../..//.local/share/alire/toolchains/gnat_native_13.2.2_a27fd794/lib/gcc/x86_64-pc-linux-gnu/13.2.0/adalib/libgnarl-13.so]
  _ALS.MAIN.EXCEPTIONS_ 0x7d8000cc5438 System.Tasking.Stages.Task_Wrapper at s-tassta.adb:1211                                                                            
  _ALS.MAIN.EXCEPTIONS_ [/lib/x86_64-linux-gnu/libc.so.6]  
  _ALS.MAIN.EXCEPTIONS_ 0x7d7ffea94ac1                                                                                                                                     
  _ALS.MAIN.EXCEPTIONS_ 0x7d7ffeb2684e                     
  _ALS.MAIN.EXCEPTIONS_ 0xfffffffffffffffe                                                                                                                                 
  [ALS.MAIN] Loading the implicit project            
  [ALS.MAIN] Show_Message: Unable to load project file: /home/nintendo/Desktop/mage_hat_0.3.0_255dc372/mage_hat.gpr
  _ALS.MAIN_ mage_hat.gpr:2:06: imported project file "mage.gpr" not found
  _ALS.MAIN_ mage_hat.gpr:17:32: unknown package or project "Mage"                                                                                                         
  _ALS.MAIN_ mage_hat.gpr:17:32: wrong expression kind for attribute "switches"
  _ALS.MAIN_ mage_hat_config.gpr:2:06: imported project file "mage.gpr" not found                                                                                          
  _ALS.MAIN_ mage_hat_config.gpr:2:06: imported by "/home/nintendo/Desktop/mage_hat_0.3.0_255dc372/config/mage_hat_config.gpr"
  _ALS.MAIN_ mage_hat_config.gpr:2:06: imported by "/home/nintendo/Desktop/mage_hat_0.3.0_255dc372/mage_hat.gpr"                                                                                                                                      
  [ALS.OUT] {"jsonrpc":"2.0","method":"window/showMessage","params":{"type":1,"message":"Unable to load project file: /home/nintendo/Desktop/mage_hat_0.3.0_255dc372/mage_hat.gpr\nmage_hat.gpr:2:06: imported project file \"mage.gpr\" not found\nma  ge_hat.gpr:17:32: unknown package or project \"Mage\"\nmage_hat.gpr:17:32: wrong expression kind for attribute \"switches\"\nmage_hat_config.gpr:2:06: imported project file \"mage.gpr\" not found\nmage_hat_config.gpr:2:06: imported by \"/home/n  intendo/Desktop/mage_hat_0.3.0_255dc372/config/mage_hat_config.gpr\"\nmage_hat_config.gpr:2:06: imported by \"/home/nintendo/Desktop/mage_hat_0.3.0_255dc372/mage_hat.gpr\"\n"}}
  [ALS.MAIN] RegisterCapability_Request: Request  2  1
  [ALS.MAIN] In Text_Document_Did_Open
  [ALS.MAIN] Uri : file:///home/nintendo/Desktop/mage_hat_0.3.0_255dc372/src/mage_hat.adb
  [ALS.OUT] {"jsonrpc":"2.0","id":2,"method":"client/registerCapability","params":{"registrations":[{"id":"rf","method":"textDocument/rangeFormatting","registerOptions":{"documentSelector":["ada"]}}]}}
  [ALS.MAIN] Finished Text_Document_Did_Open
  [ALS.MAIN] Publish_Diagnostics: file:///home/nintendo/Desktop/mage_hat_0.3.0_255dc372/src/mage_hat.adb 1
  [ALS.OUT] {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":"file:///home/nintendo/Desktop/mage_hat_0.3.0_255dc372/src/mage_hat.adb","diagnostics":[{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character"  :0}},"severity":1,"source":"project","message":"Project file has error and can't be loaded."}]}}
  [ALS.MAIN] SemanticTokens: Response  2  835

The errors in the ALS logs are similar to the errors I get if I try to build mage hat in the root folder without using Alire:

~/Desktop/mage_hat_0.3.0_255dc372$ gprbuild
using project file mage_hat.gpr
mage_hat.gpr:2:06: imported project file "mage.gpr" not found
mage_hat.gpr:17:32: unknown package or project "Mage"
mage_hat.gpr:17:32: wrong expression kind for attribute "switches"
mage_hat_config.gpr:2:06: imported project file "mage.gpr" not found
mage_hat_config.gpr:2:06: imported by "/home/nintendo/Desktop/mage_hat_0.3.0_255dc372/config/mage_hat_config.gpr"
mage_hat_config.gpr:2:06: imported by "/home/nintendo/Desktop/mage_hat_0.3.0_255dc372/mage_hat.gpr"
gprbuild: "mage_hat.gpr" processing failed

Any ideas would be greatly appreciated.

I have a more advanced (read: complex) setup, using neovim. I have tried on and off in the last three years to get ada_language_server working in that context, but it always ends up using 100% of the CPU forever. We have a large codebase (a few million lines of code), but that doesn’t explain it. As a customer, I have reported this to AdaCore but so far they haven’t been able to reproduce, and we haven’t been able to pinpoint the issue.

My setup starts similar to yours, except I provide an absolute paths for the project.

local function ada_on_attach(client, bufnr)
   --  For now, disable semantic tokens highlighting in Ada.  When this is
   --  enabled, the colors used for identifiers depend on information provided
   --  by LSP, but this seems wrong.  For instance, a 'with A.B' will highlight
   --  differently from a 'with A'.
   client.server_capabilities.semanticTokensProvider = nil
end

   local dot_git_finder = require('lspconfig').util.root_pattern('.git');
   local root_dir = dot_git_finder(vim.fn.getcwd())
   vim.g.ada_project = vim.fn.fnamemodify('everything.gpr', ':p')

   require('lspconfig').als.setup({
      on_attach = ada_on_attach,
      root_dir = dot_git_finder,
      settings = {
         ada = {
            projectFile = vim.g.ada_project,
            scenarioVariables = {
              arch= "x86-64",
     [...]
            }
      }
}

Then I also use Mason as neovim plugin, which automatically installs the latest version of ada_language_server. I install it, like all my plugins, via the lazy.vim plugin

   {
      "williamboman/mason.nvim",
      build = ":MasonUpdate",
      cmd = { "Mason" },
      opts = {},
   },

(then run :Mason and select ada_language_server in the list, to install it)

Also: the dependencies between my projects are always specified using relative paths. I do not depend on the GPR_PROJECT_PATH environment variable, which maybe is something different in your setup, since apparently gprbuild is not able to find the dependencies

Thanks for the example. This excerpt seems to work for a project file named ‘splat.gpr’. I’m not sure how to setup the config to automatically load any project without manually tinkering with the nvim config lua files for now.

󰌵 local function ada_on_attach(client, bufnr)      Unused local `bufnr`.   

     client.server_capabilities.semanticTokensProvider = nil   
  end   
      
     local dot_git_finder = require('lspconfig').util.root_pattern('.git');   
󰌵    local root_dir = dot_git_finder(vim.fn.getcwd())      Unused local `root_dir`.   
     vim.g.ada_project = vim.fn.fnamemodify('everything.gpr', ':p')   
      
     require('lspconfig').als.setup({   
     │  on_attach = ada_on_attach,   
     │  root_dir = dot_git_finder,   
     │  settings = {   
     │  │  ada = {   
     │  │  │  -- projectFile = vim.g.ada_project,   
     │  │  │  projectFile = 'splat.gpr',   
     │  │  │  scenarioVariables = {   
     │  │  │  │ arch= "x86-64",   
     │  │  │  }   
     │  │}   
     │}   
  })   
     ````