mason-tool-installer.nvim icon indicating copy to clipboard operation
mason-tool-installer.nvim copied to clipboard

The ensure_installed option does not work when using lazy.nvim with event='VeryLazy'

Open bcampolo opened this issue 1 year ago • 4 comments
trafficstars

Currently mason-tools-installer calls run_on_start during the VimEnter event (as seen below), but when using lazy.nvim with event set to 'VeryLazy', the plugin has not been loaded yet when VimEnter occurs so the run_on_start command never runs.

https://github.com/WhoIsSethDaniel/mason-tool-installer.nvim/blob/8b70e7f1e0a4119c1234c3bde4a01c241cabcc74/plugin/mason-tool-installer.lua#L1C1-L3C3

vim.api.nvim_create_autocmd({ 'VimEnter' }, {
  callback = require('mason-tool-installer').run_on_start,
})

I'm not sure what the preferred way to fix this would be, but for now I am able to work around the problem, but explicitly running: vim.api.nvim_command('MasonToolsInstall') in my lazy spec config function after the setup method is called.

bcampolo avatar Jan 14 '24 15:01 bcampolo

You have correctly identified the problem with lazy loading this plugin while also expecting it to work correctly on start.

I think the correct answer is: don't lazy load this plugin. Since it, by default, runs on start and since you want it to run on start, you don't get much from lazy loading.

If there's a way around this using lazy.nvim I don't know what it is. But I'm also not that familiar with lazy.nvim.

WhoIsSethDaniel avatar Jan 22 '24 19:01 WhoIsSethDaniel

I don't know Vim/Neovim plumbing as well as I'd like to, but I think it would be good for the run_on_start to be called when the mason-tool-installer plugin is loaded vs explicitly on the VimEnter event. This would behave similarly to how it behaves now if not lazy loading and when lazy loading would load when required by the user's configuration. This is essentially what my work-around above is doing.

Edit: Just took a peek at mason-lspconfig and it seems like it checks the ensure_installed during its .setup function, instead of an autocmd tied to a particular event.

bcampolo avatar Jan 22 '24 21:01 bcampolo

I don't know Vim/Neovim plumbing as well as I'd like to, but I think it would be good for the run_on_start to be called when the mason-tool-installer plugin is loaded vs explicitly on the VimEnter event. This would behave similarly to how it behaves now if not lazy loading and when lazy loading would load when required by the user's configuration. This is essentially what my work-around above is doing.

Edit: Just took a peek at mason-lspconfig and it seems like it checks the ensure_installed during its .setup function, instead of an autocmd tied to a particular event.

I agree. That would be the opt way of letting the library handle the user desires without interrupt their workflow

TheRustifyer avatar Feb 06 '24 10:02 TheRustifyer

Here is a snippet you can use however you see fit. Just make sure you setup mason beforehand, though I wouldn't lazy load it since its not recommended.

local registry = require('mason-registry')

-- These are package names sourced from the Mason registry,
-- and may not necessarily match the server names used in lspconfig
local ensure_installed = {
  'yaml-language-server',
  'terraform-ls',
  'stylua',
  'pyright',
  'marksman',
  'lua-language-server',
  'json-lsp',
  'isort',
  'gopls',
  'docker-compose-language-service',
  'clangd',
  'black',
}

-- Ensure packages are installed and up to date
registry.refresh(function()
  for _, name in pairs(ensure_installed) do
    local package = registry.get_package(name)
    if not registry.is_installed(name) then
      package:install()
    else
      package:check_new_version(function(success, result_or_err)
        if success then
          package:install({ version = result_or_err.latest_version })
        end
      end)
    end
  end
end)

smartinellimarco avatar Mar 08 '24 04:03 smartinellimarco