mfussenegger / nvim-lint

An asynchronous linter plugin for Neovim complementary to the built-in Language Server Protocol support.
GNU General Public License v3.0
1.94k stars 204 forks source link

fix: golangci-lint respect cwd #517

Closed troyanov closed 8 months ago

troyanov commented 8 months ago

This PR allows golangcilint to use provided cwd instead of vim.fn.getcwd() using functionality that was added in https://github.com/mfussenegger/nvim-lint/pull/358

It might be helpful for a monorepo with multiple languages.

project-root
├── .golangci.yaml
├── client
│   └── lxc.py
├── agent
│   ├── go.mod
│   └── cmd
│       └── main.go
└── core
    ├── go.mod
    └── main.go

If my vim.fn.getcwd() will be project-root I won't be able to run golangcilint since it will fail to find any Go modules

ERRO [linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies

However, with some auto commands I can search for go.mod among ancestors that are related to my buffer path:

vim.api.nvim_create_autocmd({ 'BufEnter' }, {
  pattern = { '*.go' },
  callback = function()
    local lsputil = require('lspconfig.util')
    local cwd = lsputil.root_pattern("go.mod")(vim.fn.expand('%:p'))
    local config = lsputil.root_pattern(".golangci.yaml")(vim.fn.expand('%:p'))
    local golangcilint = require('lint.linters.golangcilint')
    golangcilint.args = {
      'run',
      '--out-format',
      'json',
      '--timeout',
      '5m',
    }
    if config ~= nil then
      config = config .. '/.golangci.yaml'
      table.insert(golangcilint.args, '--config')
      table.insert(golangcilint.args, config)
    end
    golangcilint.cwd = cwd
  end,
})