stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.58k stars 136 forks source link

bug: Black not obeying interpreter options, nor virtualenv #390

Closed peter-bowman-lantern closed 2 months ago

peter-bowman-lantern commented 2 months ago

Neovim version (nvim -v)

0.9.5

Operating system/version

MacOs 14.14

Add the debug logs

Log file

Log file: /Users/bowmanpete/.local/state/nvim/conform.log 14:13:11[DEBUG] black stderr: { "Traceback (most recent call last):", ' File "/Users/bowmanpete/.config/nvim/lua/lsp_servers/bin/black", line 5, in ', " from black import patched_main", ' File "src/black/init.py", line 31, in ', "ModuleNotFoundError: No module named 'click'", "" } 14:13:11[INFO] Run isort on /Users/bowmanpete/...point.py 14:13:11[DEBUG] Run command: { "isort", "--stdout", "--filename", "/Users/.....point.py", "-" } 14:13:11[DEBUG] Run CWD: /Users/...data-etl-library 14:13:11[DEBUG] isort exited with code 0 14:13:11[ERROR] Formatter 'black' error: Traceback (most recent call last): File "/Users/bowmanpete/.config/nvim/lua/lsp_servers/bin/black", line 5, in from black import patched_main File "src/black/init.py", line 31, in ModuleNotFoundError: No module named 'click'

Config/nvim/lsp_servers is the path I've installed my mason packages to

Describe the bug

Basically the path black is looking for is the path set in where I've told it that my lap servers are installed via mason. However my virtual environment is somewhere else. upon setting up the command = "python -m black" in order for it to obey my virtual env it doesn't work Screenshot 2024-04-29 at 14 15 32 then my check health output image

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

  1. Setup a virtual environment
  2. Install black using that virtual environemtn
  3. try to format a python file
  4. check where its looking for black to be set up

Expected Behavior

It formats the file

Minimal example file

No response

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

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

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        -- add your config here
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

No response

peter-bowman-lantern commented 2 months ago

Think I figured out how to solve it:

Basically use this function:

get_venv_command = function(command)
    if vim.env.VIRTUAL_ENV then
        return vim.env.VIRTUAL_ENV .. "/bin/" .. command)
    else
        return command
    end
end

somewhere in your config and then set

require('conform').setup({
formatters = {
  {
    black = {
      command = get_venv_command("black"),
    },
  },
}

This works for anything where you want to use the venv command where its activated and the global one vim will default to when its not