Skip to content

mason setup is called twice #1297

Closed
Closed
@tomasgareau

Description

@tomasgareau

When setting up nvim-lspconfig, we tell Lazy that mason.nvim is a dependency and configure it right away (#865 & linked issues touch on why):

kickstart.nvim/init.lua

Lines 456 to 460 in a8f5395

-- Main LSP Configuration
'neovim/nvim-lspconfig',
dependencies = {
-- Automatically install LSPs and related tools to stdpath for Neovim
{ 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants

But later in nvim-lspconfig's config function, we explicitly setup mason again:

kickstart.nvim/init.lua

Lines 648 to 654 in a8f5395

-- Ensure the servers and tools above are installed
-- To check the current status of installed tools and/or manually install
-- other tools, you can run
-- :Mason
--
-- You can press `g?` for help in this menu.
require('mason').setup()

These two setup calls can interfere with each other. For example, if you tweaked the second setup call to append Mason's bin path (e.g., to allow overriding Mason binaries with system binaries):

require("mason").setup({
  PATH = "append"
})

the first setup call will prepend the Mason bin path (the default behaviour) and your change will append the bin path. Your path then ends up being:

/path/to/mason/bin:...:/rest/of/your/path:...:/path/to/mason/bin

Oops haha.

I think we can just bin the second setup call and put all the mason configuration in the dependencies block:

  {
    -- Main LSP Configuration
    'neovim/nvim-lspconfig',
    dependencies = {
      -- Automatically install LSPs and related tools to stdpath for Neovim
-      { 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants
+      {
+        "williamboman/mason.nvim",
+        opts = {
+	  -- Mason must be loaded before dependants so we need to configure it here.
+	  -- You can override its default settings by adding them to this table, e.g.:
+	  -- PATH = "append" -- append Mason's bin path so system packages take precedence
+	  PATH = "append",
+        },
+      },
      'williamboman/mason-lspconfig.nvim',
      'WhoIsSethDaniel/mason-tool-installer.nvim',

      -- etc
    },
    config = function()
      -- etc...
    
      -- Ensure the servers and tools above are installed
      --  To check the current status of installed tools and/or manually install
      --  other tools, you can run
      --    :Mason
      --
      --  You can press `g?` for help in this menu.
-      require('mason').setup()

(but I'm not sure if e.g., the second setup call is required for auto-installation after the other config). Happy to toss up a PR if the above makes sense!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions