williamboman / nvim-lsp-installer

Further development has moved to https://github.com/williamboman/mason.nvim!
https://github.com/williamboman/mason.nvim
Apache License 2.0
2k stars 123 forks source link

[bug] `on_server_ready` doesn't work as expected in ftplugin #216

Closed kylo252 closed 3 years ago

kylo252 commented 3 years ago

Description

First discovered in https://github.com/LunarVim/LunarVim/pull/1863, where basically the on_server_ready() is executing way too early.

How to reproduce

I chose jdtls because I wanted a server with slower installation. Add the following to java.lua

Click to expand ```lua local installer = require("nvim-lsp-installer") local servers = require("nvim-lsp-installer.servers") local log = require("nvim-lsp-installer.log") local server_name = "jdtls" local config = {} local install_notification = false local server_available, requested_server = servers.get_server(server_name) -- until server:on_ready() fn exists upstream function requested_server:on_ready(cb) installer.on_server_ready(function(server) if server.name == server_name then cb() end end) end if server_available then if not requested_server:is_installed() then install_notification = true log.debug("Automatic server installation detected") vim.notify("Automatic server installation detected", vim.log.levels.INFO) requested_server:install() end end requested_server:on_ready(function() requested_server:setup(config) if install_notification then log.debug(string.format("Installation complete for [%s] server", requested_server.name)) vim.notify(string.format("Installation complete for [%s] server", requested_server.name), vim.log.levels.INFO) install_notification = false end end) ```

https://user-images.githubusercontent.com/59826753/139529436-5ed0dcc7-5438-41b0-8313-30d34600829b.mp4

You can see how how both the notification and the log entry are appearing before the installation is actually done.

Log file ```logtalk [DEBUG Sat 30 Oct 2021 12:33:34 PM CEST] ...nvim-lsp-installer/lua/nvim-lsp-installer/ui/display.lua:393: Opening window [DEBUG Sat 30 Oct 2021 12:33:37 PM CEST] ...nvim-lsp-installer/lua/nvim-lsp-installer/ui/display.lua:199: Deleting window [DEBUG Sat 30 Oct 2021 12:33:53 PM CEST] ...nvim-lsp-installer/lua/nvim-lsp-installer/ui/display.lua:393: Opening window [DEBUG Sat 30 Oct 2021 12:33:54 PM CEST] ...nvim-lsp-installer/lua/nvim-lsp-installer/ui/display.lua:199: Deleting window [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] /home/hatsu/.config/nvim/ftplugin/java.lua:24: Automatic server installation detected [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] ...-installer/lua/nvim-lsp-installer/ui/status-win/init.lua:612: Installing server { _default_options = { cmd = { "java", "-Declipse.application=org.eclipse.jdt.ls.core.id1", "-Dosgi.bundles.defaultStartLevel=4", "-Declipse.product=org.eclipse.jdt.ls.core.product", "-Dlog.protocol=true", "-Dlog.level=ALL", "-Xms1g", "-Xmx2G", "-jar", "/home/hatsu/.local/share/nvim/lsp_servers/jdtls/plugins/org.eclipse.equinox.launcher_*.jar", "-configuration", "/home/hatsu/.local/share/nvim/lsp_servers/jdtls/config_linux", "-data", "/home/hatsu/workspace", "--add-modules=ALL-SYSTEM", "--add-opens", "java.base/java.util=ALL-UNNAMED", "--add-opens", "java.base/java.lang=ALL-UNNAMED" } }, _installer = , homepage = "https://github.com/eclipse/eclipse.jdt.ls", name = "jdtls", on_ready = , root_dir = "/home/hatsu/.local/share/nvim/lsp_servers/jdtls", = <1>{ __index = , create_root_dir = , get_default_options = , get_supported_filetypes = , install = , install_attached = , is_installed = , new = , setup = , uninstall = } } nil [INFO Sat 30 Oct 2021 12:34:01 PM CEST] ...-installer/lua/nvim-lsp-installer/ui/status-win/init.lua:547: Starting install server_name="jdtls", requested_version="N/A" [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] ...art/nvim-lsp-installer/lua/nvim-lsp-installer/server.lua:129: Uninstalling server jdtls [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] ...r/start/nvim-lsp-installer/lua/nvim-lsp-installer/fs.lua:36: fs: mkdirp /home/hatsu/.local/share/nvim/lsp_servers/jdtls [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:116: Spawning cmd="wget", spawn_opts={ args = { "-nv", "-O", "archive.tar.gz", "https://download.eclipse.org/jdtls/snapshots/jdt-language-server-latest.tar.gz" }, cwd = "/home/hatsu/.local/share/nvim/lsp_servers/jdtls" } [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:162: Spawned with pid 12426 [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] /home/hatsu/.config/nvim/ftplugin/java.lua:33: Installation complete for [jdtls] server [DEBUG Sat 30 Oct 2021 12:34:01 PM CEST] ...nvim-lsp-installer/lua/nvim-lsp-installer/ui/display.lua:393: Opening window [DEBUG Sat 30 Oct 2021 12:34:09 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:148: Job pid=12426 exited with exit_code=0, signal=0 [DEBUG Sat 30 Oct 2021 12:34:09 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:116: Spawning cmd="tar", spawn_opts={ args = { "-xvf", "archive.tar.gz" }, cwd = "/home/hatsu/.local/share/nvim/lsp_servers/jdtls" } [DEBUG Sat 30 Oct 2021 12:34:09 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:162: Spawned with pid 12462 [DEBUG Sat 30 Oct 2021 12:34:10 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:148: Job pid=12462 exited with exit_code=0, signal=0 [DEBUG Sat 30 Oct 2021 12:34:10 PM CEST] ...r/start/nvim-lsp-installer/lua/nvim-lsp-installer/fs.lua:20: fs: rmrf /home/hatsu/.local/share/nvim/lsp_servers/jdtls/archive.tar.gz [DEBUG Sat 30 Oct 2021 12:34:10 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:116: Spawning cmd="wget", spawn_opts={ args = { "-nv", "-O", "lombok.jar", "https://projectlombok.org/downloads/lombok.jar" }, cwd = "/home/hatsu/.local/share/nvim/lsp_servers/jdtls" } [DEBUG Sat 30 Oct 2021 12:34:10 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:162: Spawned with pid 12466 [DEBUG Sat 30 Oct 2021 12:34:11 PM CEST] ...rt/nvim-lsp-installer/lua/nvim-lsp-installer/process.lua:148: Job pid=12466 exited with exit_code=0, signal=0 [INFO Sat 30 Oct 2021 12:34:11 PM CEST] ...-installer/lua/nvim-lsp-installer/ui/status-win/init.lua:566: Installation completed server_name="jdtls", success=true [DEBUG Sat 30 Oct 2021 12:34:13 PM CEST] ...nvim-lsp-installer/lua/nvim-lsp-installer/ui/display.lua:199: Deleting window ```
williamboman commented 3 years ago

Aaah I think I know what's happening now.

Prior to #199 the server installations would run inside its "final" directory (inside ~/.cache/nvim/lsp_servers). This meant that the very first thing that happens when a server is installed is that its directory gets created. This trips up the "is installed" logic, which merely checks for the existence of the directory. This has mostly been fine for the normal use cases, but since you're calling on_server_ready() in the same event loop tick as you're calling server:install(), it'll cause nvim-lsp-installer to think the server is successfully installed when this scheduled function executes.

Could you try pulling the latest version of this plugin - by installing in a tmpdir (#199) I think we should be all good now?

kylo252 commented 3 years ago

Could you try pulling the latest version of this plugin - by installing in a tmpdir (#199) I think we should be all good now?

welp.. this is embarassing because I keep telling people to update before testing It was merged 2 hours before this post and about a day after #210, so maybe I get a pass? 😄

I'll try to test some more things but it does seem that the tmpdir actually solves the issue.

williamboman commented 3 years ago

Hehe I'll accept it. Using tmpdirs seems to have introduced some other issues (see #215), hopefully there's a quickfix, otherwise might have to rollback.

williamboman commented 3 years ago

@kylo252 Did you see #214 btw?