From 773f28b4912d5505a21da9686d56ab7cf41a9be7 Mon Sep 17 00:00:00 2001 From: Iordanis Petkakis <12776461+dpetka2001@users.noreply.github.com> Date: Mon, 15 Sep 2025 13:08:19 +0300 Subject: [PATCH] fix(mason): migrate to mason v2 (#6053) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description `mason-lspconfig` v2 made changes to conform with latest Neovim `vim.lsp.config`/`vim.lsp.enable` API. - Lock `mason` and `mason-lspconfig` to v1 versions for Neovim <0.11 and latest HEAD for Neovim >=0.11. - Change `:get_install_path()` method in LazyVim to use `LazyVim.get_pkg_path()` and also update name references to new ones. - Change `nvim-lspconfig` from `LazyFile` to `{ "BufReadPre", "BufNewFile", "BufWritePre" }` for Neovim >=0.11, since on `BufReadPost` the LSP would not attach on the first buffer. Previously the `setup.handlers()` function from `mason-lspconfig` would take care of that, but that has been removed on v2. I fail to think if there's a better way to handle this. Most of the changes in `/lsp/init.lua` are thanks to @williamboman comment [here](https://github.com/LazyVim/LazyVim/pull/6041#issuecomment-2857266471). I've also seen that both `mason-lspconfig` and `rustaceanvim` now have as minimum requirements Neovim >=0.11. As such I would suggest, instead of all the conditional checks for Neovim versions to create a stable release where we pin `mason` and `mason-lspconfig` to v1 versions and then only incorporate William's suggestions in a new release and mention in the README that users who are on Neovim <0.11 should use the stable release where the plugin versions are pinned to v1. Not sure how you want to tackle this, so this is merely just a suggestion from my part. Both #6041 and #6045 tackle only with the new name references and the offending line that was causing the error, but don't take into consideration the LSP servers configurations. `opts.setup` would not execute on both, since that was handled by `setup_handlers()` function in the past. I checked with Typescript lang and with those PRs there are no Javascript settings in `vtsls` (by checking with `:=LazyVim.opts("nvim-lspconfig").servers.vtsls`). #6045 also tried to change the method `:get_install_pkg()` by using `vim.fn.exepath`, but to my understanding that only returns the binary path not the package installation path and that is later concatenated to find other paths, which I believe is not correct. PS: There are 2 commits, because my LazyVim fork was not up to date and updated it and then merged the branch I had into a new branch. Sorry if that causes any inconvenience. ## Related Issue(s) Fixes #6039 ## Screenshots ## Checklist - [x] I've read the [CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md) guidelines. --------- Co-authored-by: Folke Lemaitre Co-authored-by: William Boman Co-authored-by: Luis DurĂ£o --- lua/lazyvim/plugins/extras/lang/java.lua | 11 +--- lua/lazyvim/plugins/extras/lang/php.lua | 5 +- lua/lazyvim/plugins/extras/lang/rust.lua | 10 +--- lua/lazyvim/plugins/lsp/init.lua | 71 +++++++++++++----------- lua/lazyvim/util/init.lua | 17 ++++-- tests/extras/extra_spec.lua | 3 +- tests/minit.lua | 4 +- 7 files changed, 63 insertions(+), 58 deletions(-) diff --git a/lua/lazyvim/plugins/extras/lang/java.lua b/lua/lazyvim/plugins/extras/lang/java.lua index 8098b6b3..9e5047cf 100644 --- a/lua/lazyvim/plugins/extras/lang/java.lua +++ b/lua/lazyvim/plugins/extras/lang/java.lua @@ -87,8 +87,7 @@ return { opts = function() local cmd = { vim.fn.exepath("jdtls") } if LazyVim.has("mason.nvim") then - local mason_registry = require("mason-registry") - local lombok_jar = mason_registry.get_package("jdtls"):get_install_path() .. "/lombok.jar" + local lombok_jar = vim.fn.expand("$MASON/share/jdtls/lombok.jar") table.insert(cmd, string.format("--jvm-arg=-javaagent:%s", lombok_jar)) end return { @@ -151,17 +150,13 @@ return { if LazyVim.has("mason.nvim") then local mason_registry = require("mason-registry") if opts.dap and LazyVim.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then - local java_dbg_pkg = mason_registry.get_package("java-debug-adapter") - local java_dbg_path = java_dbg_pkg:get_install_path() local jar_patterns = { - java_dbg_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar", + vim.fn.expand("$MASON/share/java-debug-adapter/com.microsoft.java.debug.plugin-*.jar"), } -- java-test also depends on java-debug-adapter. if opts.test and mason_registry.is_installed("java-test") then - local java_test_pkg = mason_registry.get_package("java-test") - local java_test_path = java_test_pkg:get_install_path() vim.list_extend(jar_patterns, { - java_test_path .. "/extension/server/*.jar", + vim.fn.expand("$MASON/share/java-test/*.jar"), }) end for _, jar_pattern in ipairs(jar_patterns) do diff --git a/lua/lazyvim/plugins/extras/lang/php.lua b/lua/lazyvim/plugins/extras/lang/php.lua index 5a2835aa..04469223 100644 --- a/lua/lazyvim/plugins/extras/lang/php.lua +++ b/lua/lazyvim/plugins/extras/lang/php.lua @@ -48,11 +48,10 @@ return { optional = true, opts = function() local dap = require("dap") - local path = require("mason-registry").get_package("php-debug-adapter"):get_install_path() dap.adapters.php = { type = "executable", - command = "node", - args = { path .. "/extension/out/phpDebug.js" }, + command = "php-debug-adapter", + args = {}, } end, }, diff --git a/lua/lazyvim/plugins/extras/lang/rust.lua b/lua/lazyvim/plugins/extras/lang/rust.lua index eca1b597..b66dde9b 100644 --- a/lua/lazyvim/plugins/extras/lang/rust.lua +++ b/lua/lazyvim/plugins/extras/lang/rust.lua @@ -110,13 +110,9 @@ return { }, config = function(_, opts) if LazyVim.has("mason.nvim") then - local package_path = require("mason-registry").get_package("codelldb"):get_install_path() - local codelldb = package_path .. "/extension/adapter/codelldb" - local library_path = package_path .. "/extension/lldb/lib/liblldb.dylib" - local uname = io.popen("uname"):read("*l") - if uname == "Linux" then - library_path = package_path .. "/extension/lldb/lib/liblldb.so" - end + local codelldb = vim.fn.exepath("codelldb") + local codelldb_lib_ext = io.popen("uname"):read("*l") == "Linux" and ".so" or ".dylib" + local library_path = vim.fn.expand("$MASON/opt/lldb/lib/liblldb" .. codelldb_lib_ext) opts.dap = { adapter = require("rustaceanvim.config").get_codelldb_adapter(codelldb, library_path), } diff --git a/lua/lazyvim/plugins/lsp/init.lua b/lua/lazyvim/plugins/lsp/init.lua index 54f410fc..b1d6a23d 100644 --- a/lua/lazyvim/plugins/lsp/init.lua +++ b/lua/lazyvim/plugins/lsp/init.lua @@ -2,7 +2,7 @@ return { -- lspconfig { "neovim/nvim-lspconfig", - event = "LazyFile", + event = { "BufReadPre", "BufNewFile", "BufWritePre" }, dependencies = { "mason.nvim", { "mason-org/mason-lspconfig.nvim", config = function() end }, @@ -174,31 +174,35 @@ return { opts.capabilities or {} ) - local function setup(server) - local server_opts = vim.tbl_deep_extend("force", { - capabilities = vim.deepcopy(capabilities), - }, servers[server] or {}) - if server_opts.enabled == false then - return - end - - if opts.setup[server] then - if opts.setup[server](server, server_opts) then - return - end - elseif opts.setup["*"] then - if opts.setup["*"](server, server_opts) then - return - end - end - require("lspconfig")[server].setup(server_opts) - end - -- get all the servers that are available through mason-lspconfig local have_mason, mlsp = pcall(require, "mason-lspconfig") local all_mslp_servers = {} - if have_mason then - all_mslp_servers = vim.tbl_keys(require("mason-lspconfig.mappings.server").lspconfig_to_package) + all_mslp_servers = vim.tbl_keys(require("mason-lspconfig.mappings").get_mason_map().lspconfig_to_package) + + local exclude_automatic_enable = {} ---@type string[] + + local function configure(server) + local server_opts = vim.tbl_deep_extend("force", { + capabilities = vim.deepcopy(capabilities), + }, servers[server] or {}) + + if opts.setup[server] then + if opts.setup[server](server, server_opts) then + return true + end + elseif opts.setup["*"] then + if opts.setup["*"](server, server_opts) then + return true + end + end + vim.lsp.config(server, server_opts) + + -- manually enable if mason=false or if this is a server that cannot be installed with mason-lspconfig + if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then + vim.lsp.enable(server) + return true + end + return false end local ensure_installed = {} ---@type string[] @@ -207,24 +211,31 @@ return { server_opts = server_opts == true and {} or server_opts if server_opts.enabled ~= false then -- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig - if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then - setup(server) + if configure(server) then + exclude_automatic_enable[#exclude_automatic_enable + 1] = server else ensure_installed[#ensure_installed + 1] = server end + else + exclude_automatic_enable[#exclude_automatic_enable + 1] = server end end end if have_mason then - mlsp.setup({ + local setup_config = { ensure_installed = vim.tbl_deep_extend( "force", ensure_installed, LazyVim.opts("mason-lspconfig.nvim").ensure_installed or {} ), - handlers = { setup }, - }) + } + + setup_config.automatic_enable = { + exclude = exclude_automatic_enable, + } + + mlsp.setup(setup_config) end if LazyVim.lsp.is_enabled("denols") and LazyVim.lsp.is_enabled("vtsls") then @@ -278,8 +289,4 @@ return { end) end, }, - - -- pin to v1 for now - { "mason-org/mason.nvim", version = "^1.0.0" }, - { "mason-org/mason-lspconfig.nvim", version = "^1.0.0" }, } diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 4b49f539..339fdadb 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -253,11 +253,18 @@ function M.get_pkg_path(pkg, path, opts) opts = opts or {} opts.warn = opts.warn == nil and true or opts.warn path = path or "" - local ret = root .. "/packages/" .. pkg .. "/" .. path - if opts.warn and not vim.loop.fs_stat(ret) and not require("lazy.core.config").headless() then - M.warn( - ("Mason package path not found for **%s**:\n- `%s`\nYou may need to force update the package."):format(pkg, path) - ) + local ret = vim.fs.normalize(root .. "/packages/" .. pkg .. "/" .. path) + if opts.warn then + vim.schedule(function() + if not require("lazy.core.config").headless() and not vim.loop.fs_stat(ret) then + M.warn( + ("Mason package path not found for **%s**:\n- `%s`\nYou may need to force update the package."):format( + pkg, + path + ) + ) + end + end) end return ret end diff --git a/tests/extras/extra_spec.lua b/tests/extras/extra_spec.lua index 2afb6b94..2a4a9bc8 100644 --- a/tests/extras/extra_spec.lua +++ b/tests/extras/extra_spec.lua @@ -26,7 +26,8 @@ describe("Extra", function() return not vim.tbl_contains(ignore, extra.modname) end, extras) - local lsp_to_pkg = require("mason-lspconfig.mappings.server").lspconfig_to_package + local lsp_to_pkg = {} + lsp_to_pkg = require("mason-lspconfig.mappings").get_mason_map().lspconfig_to_package local tsspec = Plugin.Spec.new({ import = "lazyvim.plugins.treesitter", diff --git a/tests/minit.lua b/tests/minit.lua index c7240d96..41907e75 100644 --- a/tests/minit.lua +++ b/tests/minit.lua @@ -9,8 +9,8 @@ require("lazy.minit").setup({ { dir = vim.uv.cwd() }, { "LazyVim/starter" }, { "nvim-treesitter/nvim-treesitter" }, - { "mason-org/mason-lspconfig.nvim", version = "^1.0.0" }, - { "mason-org/mason.nvim", version = "^1.0.0" }, + { "mason-org/mason-lspconfig.nvim" }, + { "mason-org/mason.nvim" }, { "nvim-mini/mini.icons", opts = {} }, }, })