Repository: neovim/neovim
File: runtime/lua/vim/lsp/health.lua (Lua)

Code before:
        or '? (no serverInfo.version response)'
      local cmd ---@type string
      local ccmd = client.config.cmd
      if type(ccmd) == 'table' then
        cmd = vim.inspect(ccmd)
      elseif type(ccmd) == 'function' then
        cmd = func_tostring(ccmd)
      end
      local dirs_info ---@type string
      if client.workspace_folders and #client.workspace_folders > 1 then
        local wfolders = {} --- @type string[]
        for _, dir in ipairs(client.workspace_folders) do
          wfolders[#wfolders + 1] = dir.name
        end
        dirs_info = ('- Workspace folders:\n    %s'):format(table.concat(wfolders, '\n    '))
      else
        local root_dirs = {} ---@type string[]
        if type(client.root_dir) == 'string' then
          table.insert(root_dirs, client.root_dir --[[@as string]])
        elseif type(client.root_dir) == 'function' then
          for buf, _ in pairs(client.attached_buffers) do
            client.root_dir(buf, function(dir)
              if dir then
                table.insert(root_dirs, dir)
              end
            end)
          end
        end
        if #root_dirs == 1 then
          dirs_info = string.format('- Root directory: %s', vim.fn.fnamemodify(root_dirs[1], ':~'))
        elseif #root_dirs > 1 then
          dirs_info = string.format(
            '- Root directories:\n    %s',
            table.concat(
              vim.iter(root_dirs):map(function(x)
                vim.fn.fnamemodify(x, ':~')
              end),
              '\n    '
            )
          )
        end
      end
      report_info(table.concat({
        string.format('%s (id: %d)', client.name, client.id),
        string.format('- Version: %s', server_version),
        dirs_info,
        string.format('- Command: %s', cmd),
        string.format('- Settings: %s', vim.inspect(client.settings, { newline = '\n  ' })),
        string.format(
          '- Attached buffers: %s',
          vim.iter(pairs(client.attached_buffers)):map(tostring):join(', ')

Review comment (suggestion):
This could be asynchronous, though it's not common (but definitely happens). To protect against this, use `vim.wait` with a timeout:

not tested but shows the idea:

```suggestion
            local dir = nil
            client.root_dir(buf, function(d)
              dir = d
            end)
            vim.wait(1000, function() return not not dir end)
            table.insert(root_dirs, dir or 'root_dir() timeout')
```


Code after:
        or '? (no serverInfo.version response)'
      local cmd ---@type string
      local ccmd = client.config.cmd
      if type(ccmd) == 'table' then
        cmd = vim.inspect(ccmd)
      elseif type(ccmd) == 'function' then
        cmd = func_tostring(ccmd)
      end
      local dirs_info ---@type string
      if client.workspace_folders and #client.workspace_folders > 1 then
        local wfolders = {} --- @type string[]
        for _, dir in ipairs(client.workspace_folders) do
          wfolders[#wfolders + 1] = dir.name
        end
        dirs_info = ('- Workspace folders:\n    %s'):format(table.concat(wfolders, '\n    '))
      else
        local root_dirs = {} ---@type table<string, boolean>
        local timeout = 1
        local timeoutmsg = ('root_dir() took > %ds'):format(timeout)
        for buf, _ in pairs(client.attached_buffers) do
          local dir = client._resolve_root_dir(1000, buf, client.root_dir)
          root_dirs[dir or timeoutmsg] = true
        end
        dirs_info = string.format(
          '- Root %s:\n    %s',
          vim.tbl_count(root_dirs) > 1 and 'directories' or 'directory',
          vim
            .iter(root_dirs)
            :map(function(k, _)
              return k == timeoutmsg and timeoutmsg or vim.fn.fnamemodify(k, ':~')
            end)
            :join('\n    ')
        )
      end
      report_info(table.concat({
        string.format('%s (id: %d)', client.name, client.id),
        string.format('- Version: %s', server_version),
        dirs_info,
        string.format('- Command: %s', cmd),
        string.format('- Settings: %s', vim.inspect(client.settings, { newline = '\n  ' })),
        string.format(
          '- Attached buffers: %s',
          vim.iter(pairs(client.attached_buffers)):map(tostring):join(', ')
        ),
      }, '\n'))
    end
  else
    report_info('No active clients')
  end
end