WhoIsSethDaniel / mason-tool-installer.nvim

Install and upgrade third party tools automatically
MIT License
449 stars 16 forks source link

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

Open bcampolo opened 9 months ago

bcampolo commented 9 months ago

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.

WhoIsSethDaniel commented 9 months ago

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.

bcampolo commented 9 months ago

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.

TheRustifyer commented 9 months ago

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

smartinellimarco commented 8 months ago

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)