-
Notifications
You must be signed in to change notification settings - Fork 777
builtins.formatting.black/blue doesn't work #1020
Comments
What happens when you run the following commands in Neovim? " should echo 1
:echo executable("black")
" should format the current file and show a bunch of messages from black
:!black % Unfortunately I don't have access to a Windows development environment, so hopefully somebody else has some insight here. I couldn't find much else about this situation on Google apart from this Stack Overflow question, which may not be relevant, but I can try to investigate more when I have time. |
:echo executable("black")
" echo 1
!black %
" format current file correctly It's worth noting that currently only the black and blue (based on black) has this problem in my tests, other python formatting tools like yapf and isort work fine. But black and blue can run normally whether they are called directly or through stdin in windows cmd. I tried formatter.nvim with its default settings for black and it work correctly too. So I guess the problem might be related to the way null-ls calls black. Thank you for your reply, I hope my additional information can help to troubleshoot the problem. |
Thanks for the info, the fact that it works with formatter.nvim is a valuable hint. Let's see if somebody happens to have some insight here, and if not I'll try to look into this when I can. |
I have the same problem on Linux Fedora 36. This is using the minimal version. The log appears normal, only this message is odd:
If I use my main configuration, I get
This project uses a Python virtual environment and poetry, but I suspect that is not relevant here. |
@ygworldr Not sure this is related, could you open a separate issue? |
Issue opened. Thank you. |
As per #1036 (duplicate of this one), I tried with formatter and got the following: Peek.2022-09-05.08-38.mp4This is the lua config I used: -- this template is borrowed from nvim-lspconfig
local on_windows = vim.loop.os_uname().version:match("Windows")
local function join_paths(...)
local path_sep = on_windows and "\\" or "/"
local result = table.concat({ ... }, path_sep)
return result
end
vim.cmd([[set runtimepath=$VIMRUNTIME]])
local temp_dir
if on_windows then
temp_dir = vim.loop.os_getenv("TEMP")
else
temp_dir = "/tmp"
end
vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))
local package_root = join_paths(temp_dir, "nvim", "site", "pack")
local install_path = join_paths(package_root, "packer", "start", "packer.nvim")
local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")
local null_ls_config = function()
local null_ls = require("null-ls")
-- add only what you need to reproduce your issue
null_ls.setup({
sources = { null_ls.builtins.formatting.black },
debug = true,
})
end
local formatter_config = function()
local formater = require("formatter")
formater.setup({
logging = true,
log_level = vim.log.levels.DEBUG,
filetype = {
python = {
-- add only what you need to reproduce your issue
function()
return {
exe = "black",
args = { "--line-length", "79" },
stdin = false,
}
end,
},
},
})
end
local function load_plugins()
-- only add other plugins if they are necessary to reproduce the issue
require("packer").startup({
{
"wbthomason/packer.nvim",
{
-- "jose-elias-alvarez/null-ls.nvim",
"mhartington/formatter.nvim",
-- requires = { "nvim-lua/plenary.nvim" },
-- config = null_ls_config,
config = formatter_config,
},
},
config = {
package_root = package_root,
compile_path = compile_path,
},
})
end
if vim.fn.isdirectory(install_path) == 0 then
vim.fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
load_plugins()
require("packer").sync()
else
load_plugins()
require("packer").sync()
end |
@kierun Does this work for you when you use the same argument structure as null-ls? local formatter = require("formatter")
local util = require("formatter.util")
formatter.setup({
logging = true,
log_level = vim.log.levels.DEBUG,
filetype = {
python = {
-- add only what you need to reproduce your issue
function()
return {
exe = "black",
args = {
"--stdin-filename",
util.escape_path(util.get_current_buffer_file_path()),
"--quiet",
"-",
},
stdin = true,
}
end,
},
},
}) The default options for |
@jose-elias-alvarez Yes, it does work as long as I call |
That's a tough one, then. I genuinely have no idea why |
I am also having this issue. :echo executable("black") echoes 1 and
formats the buffer and prints some |
I also want to add that I have the same problem with :echo executable("isort") echoes 1 and !isort % sorts the imports correctly. |
The fact that it's specific to Python-based sources does indeed seem like a clue, though it's a complicated ecosystem that I don't personally work with, so there's only so much I can do. Like I said before:
|
@jose-elias-alvarez @serhez Recently I tried it again and to my surprise everything works fine now. Don't know if the problem is solved for others, maybe more feedback is needed. My current version:
|
It works for me too!
|
I found this thread yesterday after experiencing this issue when first installing |
Curious to see if anybody is still experiencing issues or if this can be closed. |
I am still experiencing this issue @jose-elias-alvarez 😞 I am using:
As mentioned previously, using |
I have the same issue with
Here is my config:
|
I have the same issue with black and isort. Works perfectly fine for other builtin formatters like goimports.
|
What does the following commands do?
Replace |
Both In the case of a go file, I can see in :NullLsLog the lines corresponding to the invocation of goimports. But, for a python file, I cannot see any invocation of |
@varunbpatil The only things I can think of is older versions of |
To add more details to help debug the issue, I have the same problem both inside an (activated) virtualenv and outside. In both cases,
|
Same issue here. black and isort not working via null-ls. |
I had this same issue where it would not autoformat with
Sure enough when I tried to run the file Reinstalling |
Reinstalling, as @BradLewis mentions, doesn't work for me. This issue is still a thing. |
Same issue here. As for me, I'm trying to use
When trying to format a file using
|
I have the same issue over my remote development setup, not on my local. I'm not sure this is a null-ls problem as running
in the shell outside of nvim results in black hanging and not returning. So I think null-ls just times out. local h = require("null-ls.helpers")
local methods = require("null-ls.methods")
local FORMATTING = methods.internal.FORMATTING
local black = h.make_builtin({
name = "black",
meta = {
url = "https://github.com/psf/black",
description = "The uncompromising Python code formatter",
},
method = FORMATTING,
filetypes = { "python" },
generator_opts = {
command = "black",
args = {
"$FILENAME",
"--quiet",
},
to_temp_file = true,
from_temp_file = true,
},
factory = h.formatter_factory,
}) |
Uh oh!
There was an error while loading. Please reload this page.
FAQ
Issues
Neovim Version
NVIM v0.7.2
Operating System
Windows 10 21H2 19044.1889
Minimal config
Steps to reproduce
Expected behavior
Formatting.
Actual behavior
Formatting not working properly, the contents of the file have not changed.
I tried run
bat foo.py | black --stdin-filename foo.py --quiet -
in CMD, it works fine, so i think is not caused by black or blue.Besieds, I tried it in WSL2 Ubuntu and everything worked fine, so i think this problem is related to Windows system.
Debug log
Help
No
Implementation help
No response
Requirements
The text was updated successfully, but these errors were encountered: