Skip to content

PowerShell Editor Services - LSP client starts but doesn't attach to buffer #2810

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

Closed
scottmckendry opened this issue Sep 13, 2023 · 25 comments
Closed
Labels
bug Something isn't working

Comments

@scottmckendry
Copy link

Description

This is a Windows-specific issue. Running Neovim in WSL or native Linux (Arch) in my tests I could not reproduce.

When editing .ps1 files in windows-native neovim, the client starts but does not attach to the open buffer.

Neovim version

NVIM v0.9.1
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3

Nvim-lspconfig version

No response

Operating system and version

Windows 11 22H2

Affected language servers

powershell_es

Steps to reproduce

  1. Open a .ps1 file.
  2. Check output of :LspInfo & :LspLog

Actual behavior

:LspLog shows the client has started - no errors or warnings.

:LspInfo shows that the client is not attached to the current buffer.

Expected behavior

The client should attach to the current buffer when a .ps1 file is opened.

Minimal config

local on_windows = vim.loop.os_uname().version:match("Windows")

local function join_paths(...)
  local path_sep = on_windows and "\\" or "/"
  local result = table.concat({ ... }, path_sep)
  return result
end

vim.cmd([[set runtimepath=$VIMRUNTIME]])

local temp_dir = vim.loop.os_getenv("TEMP") or "/tmp"

vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))

local package_root = join_paths(temp_dir, "nvim", "site", "pack")
local lspconfig_path = join_paths(package_root, "test", "start", "nvim-lspconfig")

if vim.fn.isdirectory(lspconfig_path) ~= 1 then
  vim.fn.system({ "git", "clone", "https://github.com/neovim/nvim-lspconfig", lspconfig_path })
end

local mason_path = join_paths(package_root, "test", "start", "mason")

if vim.fn.isdirectory(mason_path) ~= 1 then
  vim.fn.system({ "git", "clone", "https://github.com/williamboman/mason.nvim", mason_path })
end

vim.lsp.set_log_level("trace")
require("vim.lsp.log").set_format_func(vim.inspect)
local nvim_lsp = require("lspconfig")
local on_attach = function(_, bufnr)
  local function buf_set_option(...)
    vim.api.nvim_buf_set_option(bufnr, ...)
  end

  buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")

  -- Mappings.
  local opts = { buffer = bufnr, noremap = true, silent = true }
  vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
  vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
  vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
  vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
  vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, opts)
  vim.keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, opts)
  vim.keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, opts)
  vim.keymap.set("n", "<space>wl", function()
    print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  end, opts)
  vim.keymap.set("n", "<space>D", vim.lsp.buf.type_definition, opts)
  vim.keymap.set("n", "<space>rn", vim.lsp.buf.rename, opts)
  vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
  vim.keymap.set("n", "<space>e", vim.diagnostic.open_float, opts)
  vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
  vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
  vim.keymap.set("n", "<space>q", vim.diagnostic.setloclist, opts)
end

require("mason").setup({
  install_root_dir = mason_path,
})

local mason_root = join_paths(mason_path, "packages")

-- Add the server that troubles you here
local name = "powershell_es"
local bundle_path = vim.fs.normalize(join_paths(mason_root, "powershell-editor-services"))
if not name then
  print("You have not defined a server name, please edit minimal_init.lua")
end
-- if not nvim_lsp[name].document_config.default_config.cmd and not cmd then
-- 	print([[You have not defined a server default cmd for a server
--     that requires it please edit minimal_init.lua]])
-- end

nvim_lsp[name].setup({
  bundle_path = bundle_path,
  on_attach = on_attach,
})

print(
  [[You can find your log at $HOME/.cache/nvim/lsp.log. Please paste in a github issue under a details tag as described in the issue template.]]
)

LSP log

https://gist.github.com/scottmckendry/c3b502ba707aa9effbcca656e823d499

@MarcoBuess
Copy link

Any updates on this? I'm basically having the same issue. Can't get Lsp to attach to the buffer even after manually setting cmd and bundle_path. What kinda doesn't make sense though is, that it works if you install powershell_es via Mason until I close and re-open nvim.

@MarcoBuess
Copy link

So after examining LspLog, I got it to work using the following:

local mason_registry = require("mason-registry")

installed_servers["powershell_es"] = {
    bundle_path = mason_registry.get_package("powershell-editor-services"):get_install_path(),
}

For my config its syntactically a little differnt as opposed to the example in e.g. kickstart-nvim. I'm not specifing the servers directly, but rather fetching the installed ones from mason and then iterating over them after. Hence the slightly alien syntax, but I guess you will figure that out for you. I will then attach for all installed servers like so:

mason_lspconfig.setup_handlers {
    function(server_name)
        require('lspconfig')[server_name].setup {
            capabilities = capabilities,
            on_attach = require('config.keymaps').lsp_keymaps,
            settings = installed_servers[server_name],
            filetypes = (installed_servers[server_name] or {}).filetypes,
            bundle_path = (installed_servers[server_name] or {}).bundle_path,
            handlers = handlers,
         }
    end,
}

@scottmckendry
Copy link
Author

Glad to hear you got this working, but unfortunately I've had no such luck. Here's my config based on your snippets:

local mason_registry = require("mason-registry")
local bundle_path = mason_registry.get_package("powershell-editor-services"):get_install_path()
require("lspconfig").powershell_es.setup({
    bundle_path = bundle_path,
    settings = {
        bundle_path = bundle_path,
    },
})

The behaviour I'm seeing is no different from before, the client still does not attach to the buffer. Are your dotfiles somewhere public that I can check out?

@MarcoBuess
Copy link

Unfortunately my config is currently private, but here are the full 9 yards if that helps

local mason_lspconfig = require('mason-lspconfig')
local mason_registry = require("mason-registry")

-- Make server list from installed lsp servers
local installed_servers = {}
for _, key in ipairs(mason_lspconfig.get_installed_servers()) do
    installed_servers[key] = {}
end

-- Modify installed servers options if needed
installed_servers["lua_ls"] = {
    Lua = {
        workspace = { checkThirdParty = false },
        telemetry = { enable = false },
    },
}

installed_servers["powershell_es"] = {
    bundle_path = mason_registry.get_package("powershell-editor-services"):get_install_path(),
}

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)

local handlers = {
    ["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "rounded" }),
}

mason_lspconfig.setup_handlers {
    function(server_name)
        require('lspconfig')[server_name].setup {
            capabilities = capabilities,
            on_attach = require('config.keymaps').lsp_keymaps,
            settings = installed_servers[server_name],
            filetypes = (installed_servers[server_name] or {}).filetypes,
            bundle_path = (installed_servers[server_name] or {}).bundle_path,
            handlers = handlers,
        }
    end,
}

@scottmckendry
Copy link
Author

scottmckendry commented Oct 12, 2023

Thanks for sharing. I didn't think there was anything I was missing. Still, no luck getting the PSES client attached sadly 😞

@MarcoBuess
Copy link

What does LspLog say? That gave me the final hints to solve the issue.

@scottmckendry
Copy link
Author

This is the full trace log:
https://gist.github.com/scottmckendry/6542f0a05d3729bb1935a419a0df2835

There's a warning at the bottom mentioning something about OmniSharp. Do you make anything of that?

@MarcoBuess
Copy link

Don't see anything sus there. Have you tried removing

settings = {
    bundle_path = bundle_path,
},

@scottmckendry
Copy link
Author

Thanks for your help with this. I've just tried your suggested change but no difference, unfortunately. Nothing new in LSP log either.

@MarcoBuess
Copy link

But you are sure that Mason has it installed? Otherwise my code wouldn't work.

@scottmckendry
Copy link
Author

I spun up a VM to see if I could get any different behaviour than I've been seeing on the two machines I use regularly (both have the same problem).

What I noticed with the VM is that PSES seemed to work initially after first opening Neovim after cloning my dotfiles, and then immediately stopped working the next time I opened it.

Even after removing ~/AppDate/Local/nvim-data I couldn't get it working again. Very strange. I even tried turning off all the Windows Defender features in the VM with no difference in behaviour.

Just to be sure Mason was installing PSES correctly, I tried replacing the package with the latest release from the repo. No difference. The client starts but doesn't attach to the buffer.

@RichardEpure
Copy link

RichardEpure commented Oct 15, 2023

I've experienced the exact same behaviour in regards to the LSP starting but not attaching. I set the bundle path like you did and nothing changed. I kept the config as is but updated PowerShell to 7.3.8 and it fixed the issue.

@MarcoBuess
Copy link

Thats neat. While fixing the issue I was already on pwsh 7.3.8 so I might not have run into these issues.

@TheLeoP
Copy link

TheLeoP commented Oct 17, 2023

Maybe this is related to this vscode issue (?) and maybe this omnisharp problem (?). This issues make me think that this may be a windows library problem and that's why it worked in a fresh VM install

@scottmckendry
Copy link
Author

@TheLeoP I think you might be right. I'm now running PS 7.3.8 and PSES 3.13.0 with no change.
What I found most odd about testing with a VM was that it only worked for the very first session of Neovim I opened once the LSP was configured.

@TheLeoP
Copy link

TheLeoP commented Oct 19, 2023

@scottmckendry how did you install the language server in the VM? Is there a change that the VM came preinstalled with a version of the langauge server and your dotfiles downloaded a newer version throught mason?

@scottmckendry
Copy link
Author

@TheLeoP the lang server was installed with Mason. I'd be very surprised if there was anything pre-existing on the VM that somehow made it work. It was a fresh Win11 install. All I did was install PS7 and git, then clone my dotfiles and test.

Also the bundle_path was set to point at the Mason package directory. This was why I thought it could have been a Windows Defender false positive, but turning all of the Defender features off didn't seem to have any effect.

@MarcoBuess
Copy link

I can confirm the behavior. Before figuring the issue out, it was only working for me when I installed the lsp freshly with Mason and only for that nvim session. After restarting nvim the functionality was gone.

@TheLeoP
Copy link

TheLeoP commented Oct 19, 2023

Can you reproduce this reliably @scottmckendry @MarcoBuess? If yes, nvim-lspconfig creates a command similar to the following:

& 'C:/Users/pcx/AppData/Local/nvim-data/mason/packages/powershell-editor-services/PowerShellEditorServices/Start-EditorServices.ps1' -Stdio -BundledModulesPath 'C:/Users/pcx/AppData/Local/nvim-data/mason/packages/powershell-editor-services' -LogPath 'C:/Users/pcx/AppData/Local/Temp/nvim/powershell_es.log' -SessionDetailsPath 'C:/Users/pcx/AppData/Local/Temp/nvim/powershell_es.session.json' -FeatureFlags @() -AdditionalModules @() -HostName nvim -HostProfileId 0 -HostVersion 1.0.0 -LogLevel 

Could you check the content of powershell_es.session.json and powershell_es.log when it works and when it does not work?

@TheLeoP
Copy link

TheLeoP commented Oct 19, 2023

For me, powershell_es.log only contains the following when it does not work:

2023-10-13 21:52:35.202 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-13 22:10:11.210 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-16 17:14:10.558 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-16 18:06:13.004 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-16 18:16:39.739 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-16 19:00:26.471 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-16 19:18:00.398 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-16 19:37:52.020 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-17 11:51:04.710 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-18 09:54:10.706 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-18 13:14:30.823 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-18 16:28:18.307 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}
2023-10-18 17:30:59.688 -05:00 [WRN] Tried to send request or notification before initialization was completed and will be sent later {"Id":1,"Error":{"Code":-32800,"Data":null,"Message":"Request Cancelled","$type":"ErrorMessage"},"Method":"initialize","$type":"RequestCancelled"}

@TheLeoP
Copy link

TheLeoP commented Oct 21, 2023

@scottmckendry @MarcoBuess @RichardEpure

As per this comment, the issue (at least for me) was powershell_es trying to load my powershell profile and taking to long to do it. To make Neovim not load it you only need to do the following:

      require("lspconfig").powershell_es.setup {
        bundle_path = "path/to/your/bundle_path",
        init_options = {
          enableProfileLoading = false,
        },
      }

Maybe it would be worth adding this to the default powershell_es config? Or some mention to it in on its documentation?

@MarcoBuess
Copy link

Nice find. As I mentioned, I currently have it working even if its loading the profile. I believe it does start the lsp with -NoProfile doesn't it?

@TheLeoP
Copy link

TheLeoP commented Oct 21, 2023

@MarcoBuess the comment I linked explains why -NoProfile is different than using the enableProfileLoading option

@scottmckendry
Copy link
Author

Thanks @TheLeoP! That's worked for me. Curious to find out what's preventing my profile from loading. The VS Code extension shares the same server and it loads my profile with no issues.

Either way, this is now resolved for me.

@rudesome
Copy link

This setting indeed solves the issue! Thanks!

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

5 participants