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.93k stars 203 forks source link

flake8 config not recognized #600

Closed svartkanin closed 3 months ago

svartkanin commented 3 months ago

I'm probably doing something wrong but in my current setup the .flake8 configuration file doesn't seem to be used when the linter is applied?

Example test.py which includes tabs

def test():
    print('hello')

.flake8 config file

[flake8]
ignore = W191
show-source = True

linter configuration nvim/lua/plugins/nvim-lint.lua

return {
  'mfussenegger/nvim-lint',
  event = { 'BufReadPre', 'BufNewFile' },
}

nvim/after/plugins/nvim-lint.lua

require('lint').linters_by_ft = {
  markdown = { 'vale' },
  python = { 'mypy', 'flake8' },
}

local lint = require 'lint'
local lint_augroup = vim.api.nvim_create_augroup('lint', { clear = true })

vim.api.nvim_create_autocmd({
  'BufEnter', -- trigger whenever we open a new buffer or we move the cursor into another buffer
  'BufWritePost', -- trigger whenever we save a buffer
  'InsertLeave', -- trigger whenever we leave insert mode
}, {
  group = lint_augroup,
  callback = function() -- will trigger when any of the above events are triggered
    lint.try_lint() -- the lint plugin which we loaded will try to execute linting
  end,
})

Which will show a flake8 warning image

However, running flake8 manually with $ flake8 test.py

will not show the warning as it locates the .flake8 configuration inside the folder

samharju commented 3 months ago

I tried it out and could not reproduce. Steps for what I did:

Created minimal config for neovim with lazy:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
        vim.fn.system({
                "git",
                "clone",
                "--filter=blob:none",
                "https://github.com/folke/lazy.nvim.git",
                "--branch=stable",
                lazypath,
        })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
        {
                "mfussenegger/nvim-lint",
                config = function()
                        local lint = require("lint")

                        lint.linters_by_ft = {
                                python = { "flake8" },
                        }

                        local lint_augroup = vim.api.nvim_create_augroup("nvim_lint_au", { clear = true })

                        vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "InsertLeave" }, {
                                group = lint_augroup,
                                callback = function()
                                        lint.try_lint()
                                end,
                        })
                end,
        },
})

Created the same .flake8 and test.py that you have, then:

mkdir flake
cd flake
python -m venv venv
source venv/bin/activate
pip install flake8
pip freeze
#flake8==7.0.0
#mccabe==0.7.0
#pycodestyle==2.11.1
#pyflakes==3.2.0
vim -u conf.lua test.py

And the config file seems to be picked up correctly. Confirmed by seeing the diagnostic when commenting out the ignore in .flake8.

svartkanin commented 3 months ago

Thanks for the confirmation. I found the culprit, had this in my lsp config

    require('mason-lspconfig').setup {
      handlers = {
        function(server_name)
          local server = servers[server_name] or {}
          -- This handles overriding only values explicitly passed
          -- by the server configuration above. Useful when disabling
          -- certain features of an LSP (for example, turning off formatting for tsserver)
          server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
          require('lspconfig')[server_name].setup(server)
        end,
      },
    }

which seems to have disabled the loading of the flake8 config.