mrcjkb / haskell-tools.nvim

🦥 Supercharge your Haskell experience in neovim!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Formatter stopped working

seanhess opened this issue · comments

Neovim version (nvim -v)

v0.9.2

Operating system/version

Ventura 13.4.1

Output of :checkhealth haskell-tools

haskell-tools: require("haskell-tools.health").check() Checking for Lua dependencies ~ - OK nvim-lua/plenary.nvim installed. - OK nvim-telescope/telescope.nvim installed. Checking external dependencies ~ - OK haskell-language-server: found haskell-language-server version: 2.2.0.0 (GHC: 8.10.7) (PATH: /Users/shess/.ghcup/hls/2.2.0.0/lib/haskell-language-server-2.2.0.0/bin/haskell-language-server-wrapper) - OK hoogle: found Hoogle 5.0.18.3, https://hoogle.haskell.org/ - OK fast-tags: found fast-tags, version 2.0.2 - OK curl: found curl 7.88.1 (x86_64-apple-darwin22.0) libcurl/7.88.1 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.11 nghttp2/1.51.0 - WARNING haskell-debug-adapter: not found. Install haskell-debug-adapter for extended capabilities. Optional, for dap support. - WARNING ghci-dap: not found. Install ghci-dap for extended capabilities. Optional, for dap support. Checking config ~ - OK No errors found in config. Checking for conflicting plugins ~ - OK No conflicting plugins detected.

How to reproduce the issue

Configuring haskell-tools to suddenly stopped working in any project. The formatters run on the command-line as expected. Neovim is loading settings as expected.

What other information can I hunt down to debug this? Thank you!

Expected behaviour

Files should format on save

Actual behaviour

The formatter doesn't run.

Log files

No response

The minimal config used to reproduce this issue.

Using lazyvim, add lua/plugins/haskell.lua:

vim.g.haskell_tools = {
  codeLens = {
    -- Whether to automatically display/refresh codeLenses
    -- (explicitly set to false to disable)
    autoRefresh = true,
  },
  hoogle = {
    mode = "telescope-local",
  },
  hls = { -- LSP client options
    -- on_attach = function(client, bufnr, ht)
    --   print("ATTACH", ht.default_settings.haskell)
    -- end,
    default_settings = {
      haskell = { -- haskell-language-server options
        formattingProvider = "fourmolu",
        -- formattingProvider = "ormolu",
        -- Setting this to true could have a performance impact on large mono repos.
        -- checkProject = true,
        plugin = {
          hlint = {
            codeActionsOn = true,
            diagnosticsOn = true,
          },
        }, -- don't
      },
    },
  },
}

return {

  { "luc-tielen/telescope_hoogle" },

  {
    "mrcjkb/haskell-tools.nvim",
    dependencies = { "nvim-lua/plenary.nvim", "nvim-telescope/telescope.nvim", "luc-tielen/telescope_hoogle" },
    branch = "2.x.x",
  },
}

Hey 👋 😄

Thanks for reporting this.

Here are some things you can try to troubleshoot:

  • Does everything else work fine? (E.g. LSP diagnostics, etc.)?
  • Does formatting work if you do it manually with :lua vim.lsp.buf.format()?
  • Does formatting work with any of the other providers?
  • If it doesn't work manually, could you please open the hls log file and paste the content here?
    :lua require('haskell-tools').log.nvim_open_hls_logfile()

Hey! Thanks for getting back so quickly.

  1. Yes, everything else works perfectly
  2. Yes! (didn't know about that command)
  3. No, I've tried setting the formattingProvider to fourmolu, ormolu, and commenting it out.
  4. That file is 200k lines long!? Seems like that alone could crash something, but here you go: https://gist.github.com/seanhess/dee113ce392e55d8f7c3c83316058e4d

🤔 haskell-tools doesn't run lsp formatting automatically.
The fact that vim.lsp.buf.format() works when you call it manually tells me that everything's fine on this side.

Do you use any other plugins that may provide auto-formatting, like lsp-format.nvim or null-ls?

You can find out what might be listening for buffer write events with :autocmd BufWritePre and :autocmd BufWritePost.

That file is 200k lines long!?

😅 I guess haskell-language-server doesn't clean up its log file. I'll fix that one...

Weird, no, I don't use any other plugins that provide auto-formatting https://github.com/seanhess/dotfiles

It was definitely auto-formatting a few days ago. I wonder what I changed. Maybe just updating dependencies...

I just installed lsp-format, and it still isn't auto-formatting. I also noticed that my editor isn't auto-formatting my lua config files anymore, so it's definitely nothing to do with haskell-tools.nvim. Thanks for your help. I'll keep digging.

It's something in this diff. I must have accidentally disabled auto-formatting by adding neovim/nvim-lspconfig: seanhess/dotfiles@59ce491#diff-88c222132debac891ac0380c464d8b88a9cd8e1751503f6c0f0f630064c2ef86R1

I've narrowed it down to the config override. Maybe I'm doing it wrong, but if I override that config function in my settings for neovim/nvim-lspconfig the auto-formatting breaks.

return {
  -- add pyright to lspconfig
  {
    "neovim/nvim-lspconfig",
    ---@class PluginLspOpts
    opts = {
      ---@type lspconfig.options
      servers = {
        -- pyright will be automatically installed with mason and loaded with lspconfig
        pyright = {
          autoImportCompletion = true,
        },
      },
    },

    -- if I comment out this block auto-format works
    config = function()
      require("lspconfig").pyright.setup({
        -- unrelated code to fix python type hinting via pyright
      })
    end,
  },
}

Might be worth thinking about, as it seems that lspconfig and haskell-tools both attempt to configure haskell-language-server.

I'll just disable lsp-config for now. Thanks for your help!

Might be worth thinking about, as it seems that lspconfig and haskell-tools both attempt to configure haskell-language-server.

lspconfig won't try to configure hls unless you explicitly call require('lspconfig').hls.setup().

Are you using a neovim distribution?
Based on your snippet it looks like your config function is overriding the opts table, and likely any default options your distribution has set. You probably want to put the options you're passing to pyright.setup {} in the opts.servers.pyright table instead, and delete the config function.

Ah I see. So it'll fall back to haskell-tools' hls config if I don't call hls setup. I'm using lazyvim.

This config is working, thanks so much for your help!

local function get_python_path(workspace)
  local util = require("lspconfig/util")
  local path = util.path
  -- Use activated virtualenv.
  if vim.env.VIRTUAL_ENV then
    return path.join(vim.env.VIRTUAL_ENV, "bin", "python")
  end

  -- Find and use virtualenv in workspace directory.
  for _, pattern in ipairs({ "*", ".*" }) do
    local match = vim.fn.glob(path.join(workspace, pattern, "pyvenv.cfg"))
    if match ~= "" then
      return path.join(path.dirname(match), "bin", "python")
    end
  end

  -- Fallback to system Python.
  return vim.fn.exepath("python3") or vim.fn.exepath("python") or "python"
end

return {
  -- add pyright to lspconfig
  {
    "neovim/nvim-lspconfig",
    ---@class PluginLspOpts
    opts = {
      ---@type lspconfig.options
      servers = {
        -- pyright will be automatically installed with mason and loaded with lspconfig
        pyright = {
          autoImportCompletion = true,

          before_init = function(_, config)
            local python_path = get_python_path(config.root_dir)
            config.settings.python.pythonPath = python_path
            vim.g.python_host_prog = python_path
            vim.g.python3_host_prog = python_path
          end,
        },
      },
    },
  }
}