scalameta / nvim-metals

A Metals plugin for Neovim
https://scalameta.org/metals/
Apache License 2.0
466 stars 75 forks source link

Failed to run `config` for nvim-metals #651

Closed michaelmior closed 7 months ago

michaelmior commented 7 months ago

Describe the bug

I get the error below when opening a Scala project. Despite this error, everything seems to work correctly after dismissing the message.

Failed to run `config` for nvim-metals

...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:372: attempt to call field 'setup' (a nil value)

# stacktrace:
  - /opt/homebrew/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:25
  - /opt/homebrew/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24

Below is my config for lazy.nvim

    {
      "scalameta/nvim-metals",
      dependencies = {
        "nvim-lua/plenary.nvim",
        {"j-hui/fidget.nvim", opts = {}},
      },
      ft = { "scala", "sbt", "java" },
      opts = function()
        local metals_config = require("metals").bare_config()
        require("metals").initialize_or_attach(metals_config)
      end,
    }

Expected behavior

No error message should be produced

Operating system

macOS

Version of Metals

1.2.2

Commit of nvim-metals

94c8d4d

ckipp01 commented 7 months ago

Hey @michaelmior so you'll want to switch this up a bit. Inside of opts you'll basically want to put together all the config, and then return it. Then you'll want to utilize config to set up an autocmd with that returned config:

    config = function(self, metals_config)
      local nvim_metals_group = vim.api.nvim_create_augroup("nvim-metals", { clear = true })
      vim.api.nvim_create_autocmd("FileType", {
        pattern = self.ft,
        callback = function()
          require("metals").initialize_or_attach(metals_config)
        end,
        group = nvim_metals_group,
      })
    end

You can see an example of this in the minimal config.

michaelmior commented 7 months ago

@ckipp01 Thanks! That did the trick. I did look at the page you referenced, but I missed that particular snippet. I think it would be helpful to have such an example in the README for new users.