Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

builtins.formatting.black/blue doesn't work #1020

Open
3 tasks done
Verf opened this issue Aug 18, 2022 · 29 comments
Open
3 tasks done

builtins.formatting.black/blue doesn't work #1020

Verf opened this issue Aug 18, 2022 · 29 comments
Labels
bug Something isn't working

Comments

@Verf
Copy link

Verf commented Aug 18, 2022

FAQ

  • I have checked the FAQ and it didn't resolve my problem.

Issues

  • I have checked existing issues and there are no issues with the same problem.

Neovim Version

NVIM v0.7.2

Operating System

Windows 10 21H2 19044.1889

Minimal config

-- 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 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",
                requires = { "nvim-lua/plenary.nvim" },
                config = null_ls_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

Steps to reproduce

  1. nvim --clean -u minimal_init.lua
  2. Edit any single python file, eg:
#coding: utf-8
print(    'hello world'    )
  1. :lua vim.lsp.buf.formatting()
  2. Nothing happens, the file content remains the same. If work correctly, the file should be formatted as:
# coding: utf-8
print("hello world")

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

[TRACE 8/18/2022 12:06:06 PM] ...\site\pack\packer\start\null-ls.nvim/lua/null-ls/rpc.lua:121: received LSP request for method shutdown
[TRACE 8/18/2022 12:06:06 PM] ...\site\pack\packer\start\null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method exit
[TRACE 8/18/2022 12:06:07 PM] ...te\pack\packer\start\null-ls.nvim/lua/null-ls/client.lua:106: starting null-ls client
[TRACE 8/18/2022 12:06:07 PM] ...\site\pack\packer\start\null-ls.nvim/lua/null-ls/rpc.lua:121: received LSP request for method initialize
[DEBUG 8/18/2022 12:06:07 PM] ...te\pack\packer\start\null-ls.nvim/lua/null-ls/client.lua:161: unable to notify client for method textDocument/didOpen (client not active): {
  textDocument = {
    uri = "file:///C:/Users/fht/Desktop/foo.py"
  }
}
[TRACE 8/18/2022 12:06:07 PM] ...\site\pack\packer\start\null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method initialized
[TRACE 8/18/2022 12:06:07 PM] ...\site\pack\packer\start\null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method textDocument/didOpen
[TRACE 8/18/2022 12:06:07 PM] ...ack\packer\start\null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_OPEN
[DEBUG 8/18/2022 12:06:07 PM] ...ack\packer\start\null-ls.nvim/lua/null-ls/generators.lua:24: no generators available
[TRACE 8/18/2022 12:06:24 PM] ...\site\pack\packer\start\null-ls.nvim/lua/null-ls/rpc.lua:121: received LSP request for method textDocument/formatting
[TRACE 8/18/2022 12:06:24 PM] ...ack\packer\start\null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_FORMATTING
[DEBUG 8/18/2022 12:06:24 PM] ...t\null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:346: spawning command "black" at C:\Users\fht\Desktop with args { "--stdin-filename", "C:\\Users\\fht\\Desktop\\foo.py", "--quiet", "-" }
[TRACE 8/18/2022 12:06:24 PM] ...t\null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:217: error output: nil
[TRACE 8/18/2022 12:06:24 PM] ...t\null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:218: output: nil

Help

No

Implementation help

No response

Requirements

  • I have read and followed the instructions above and understand that my issue will be closed if I did not provide the required information.
@Verf Verf added the bug Something isn't working label Aug 18, 2022
@jose-elias-alvarez
Copy link
Owner

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.

@Verf
Copy link
Author

Verf commented Aug 19, 2022

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.

@jose-elias-alvarez
Copy link
Owner

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.

@ghost
Copy link

ghost commented Aug 22, 2022

I have the same problem on Linux Fedora 36. This is using the minimal version.

The log appears normal, only this message is odd:

[DEBUG Mon 22 Aug 2022 16:42:43 BST] ...te/pack/packer/start/null-ls.nvim/lua/null-ls/client.lua:161: unable to notify client for method textDocument/didOpen (client not active): {
    textDocument = {
    uri = "file:///home/yann/worldr/github/setupr/setupr/commands.py"
  }
}

If I use my main configuration, I get

[WARN  Mon 22 Aug 2022 16:48:44 BST] ...te/pack/packer/start/null-ls.nvim/lua/null-ls/client.lua:129: failed to attach buffer 1

This project uses a Python virtual environment and poetry, but I suspect that is not relevant here.

@jose-elias-alvarez
Copy link
Owner

@ygworldr Not sure this is related, could you open a separate issue?

@kierun
Copy link

kierun commented Aug 23, 2022

Issue opened. Thank you.

@kierun
Copy link

kierun commented Sep 5, 2022

As per #1036 (duplicate of this one), I tried with formatter and got the following:

Peek.2022-09-05.08-38.mp4

This 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

@jose-elias-alvarez
Copy link
Owner

@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 black are more or less the same. Unfortunately formatter.nvim uses vim.fn.jobstart, so it'll be hard to investigate differences between how the two plugins spawn processes and capture output.

@kierun
Copy link

kierun commented Sep 7, 2022

@jose-elias-alvarez Yes, it does work as long as I call :FormatWrite.

@jose-elias-alvarez
Copy link
Owner

That's a tough one, then. I genuinely have no idea why black output would be empty in our case, and I think this will require some deeper investigation by someone having the issue to figure out exactly where things are going wrong.

@jose-elias-alvarez jose-elias-alvarez changed the title builtins.formatting.black/blue doesn't work on Windows10 builtins.formatting.black/blue doesn't work Sep 24, 2022
@serhez
Copy link

serhez commented Oct 6, 2022

I am also having this issue.

:echo executable("black")

echoes 1 and

!black %

formats the buffer and prints some black output. I guess the problem must be with null-ls not calling black correctly. I should also add that I use many other formatters through null-ls and they work brilliantly :)

@serhez
Copy link

serhez commented Oct 6, 2022

I also want to add that I have the same problem with isort; similarly, it does not work on save but

:echo executable("isort")

echoes 1 and

!isort %

sorts the imports correctly.

@jose-elias-alvarez
Copy link
Owner

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:

I think this will require some deeper investigation by someone having the issue to figure out exactly where things are going wrong.

@Verf
Copy link
Author

Verf commented Nov 30, 2022

@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:

  • Neovim v0.8.1
  • null-ls c51978f
  • black 22.10.0
  • blue 0.9.1

@kierun
Copy link

kierun commented Nov 30, 2022

It works for me too!

  • black, 22.8.0 (compiled: no)
  • Python (CPython) 3.10.8
  • NVIM v0.9.0-dev-397+ga6f0444ab

@HoldenLucas
Copy link

HoldenLucas commented Nov 30, 2022

I found this thread yesterday after experiencing this issue when first installing black, and it has since resolved itself without any config changes.

@jose-elias-alvarez
Copy link
Owner

Curious to see if anybody is still experiencing issues or if this can be closed.

@serhez
Copy link

serhez commented Dec 16, 2022

I am still experiencing this issue @jose-elias-alvarez 😞

I am using:

  • Black 22.12.0
  • Nvim 0.8.0
  • Null-ls latest (as of today)

As mentioned previously, using !black % does format the buffer but it's not called on save by null-ls; I use other formatters on-save through null-ls and they do work (e.g., for Lua, Golang and Typescript).

@IamGianluca
Copy link

IamGianluca commented Dec 19, 2022

I have the same issue with black and usort. I can execute them when running the following commands from inside nvim, but they don't seem to work automatically with null-ls.

:!black %
:!usort format %

Here is my config:

--=====================================================
-- nvim-cmp Settings
--=====================================================

local lsp = require('lsp-zero')
lsp.preset('recommended')

lsp.on_attach(function(client, bufnr)
	local opts = { buffer = bufnr, remap = false }

	vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
	vim.keymap.set("n", "gD", function() vim.lsp.buf.declaration() end, opts)
	vim.keymap.set("n", "gr", function() vim.lsp.buf.references() end, opts)
	vim.keymap.set("n", "gi", function() vim.lsp.buf.implementation() end, opts)
	vim.keymap.set("i", "<C-k>", function() vim.lsp.buf.signature_help() end, opts)
	vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
	vim.keymap.set("n", "<leader>rn", function() vim.lsp.buf.rename() end, opts)
	vim.keymap.set("n", "<leader>ca", function() vim.lsp.buf.code_action() end, opts)
	vim.keymap.set("n", "<leader>f", function() vim.lsp.buf.format() end, opts)

	vim.keymap.set("n", "<leader>vws", function() vim.lsp.buf.workspace_symbol() end, opts)
	vim.keymap.set("n", "<leader>vd", function() vim.diagnostic.open_float() end, opts)
	vim.keymap.set("n", "[d", function() vim.diagnostic.goto_next() end, opts)
	vim.keymap.set("n", "]d", function() vim.diagnostic.goto_prev() end, opts)
end)
lsp.setup()


--=====================================================
-- null-ls Settings
--=====================================================

-- needed to format and sort imports in Python since PyRight doesn't offer
-- those functionalities
local null_ls = require('null-ls')
local null_opts = lsp.build_options('null-ls', {})

null_ls.setup({
	on_attach = function(client, bufnr)
		null_opts.on_attach(client, bufnr)
		--- you can add more stuff here if you need it
	end,
	sources = {
		null_ls.builtins.formatting.black,
		null_ls.builtins.formatting.usort,
	}
})
:version

NVIM v0.9.0-dev
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -g -O2 -fdebug-prefix-map=/build/neovim-r0qoVt/neovim-0.9.0~ubuntu1+git202212170019-37915cc5a-333b5866f=. -fstack-protector-strong -Wformat -Werror=format-security -
U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wsh
adow -Wconversion -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=
auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/build/neovim-r0qoVt/neovim-0.9.0~ubuntu1+git202212170019-37915cc
5a-333b5866f/build/cmake.config -I/build/neovim-r0qoVt/neovim-0.9.0~ubuntu1+git202212170019-37915cc5a-333b5866f/src -I/build/neovim-r0qoVt/neovim-0.9.0~ubuntu1+git202212170019-37915cc5a-333b
5866f/.deps/usr/include -I/usr/include -I/build/neovim-r0qoVt/neovim-0.9.0~ubuntu1+git202212170019-37915cc5a-333b5866f/build/src/nvim/auto -I/build/neovim-r0qoVt/neovim-0.9.0~ubuntu1+git2022
12170019-37915cc5a-333b5866f/build/include
Compiled by buildd@lcy02-amd64-092

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

@varunbpatil
Copy link

I have the same issue with black and isort. Works perfectly fine for other builtin formatters like goimports.
In the logs, I see that there is no output along the lines of:

received LSP notification for method textDocument/formatting
running generators for method NULL_LS_FORMATTING

@kierun
Copy link

kierun commented Jan 9, 2023

I have the same issue with black and isort. Works perfectly fine for other builtin formatters like goimports. In the logs, I see that there is no output along the lines of:

received LSP notification for method textDocument/formatting
running generators for method NULL_LS_FORMATTING

What does the following commands do?

:echo executable("isort")
!isort %

Replace isort with black to check the other one. Look at the logs, there should be some hints there as to what is going wrong.

@varunbpatil
Copy link

varunbpatil commented Jan 9, 2023

@kierun

Both :echo executable("isort") and :echo executable("black") return 1.
:!black % and :!isort % work fine and so does :lua vim.lsp.buf.format().

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 black or isort. I can see invocation of flake8 though.

@kierun
Copy link

kierun commented Jan 9, 2023

@varunbpatil The only things I can think of is older versions of neovim or null-ls

@IamGianluca
Copy link

To add more details to help debug the issue, I have the same problem both inside an (activated) virtualenv and outside. In both cases, black and usort are installed and executable. In both cases, the following commands return 1.

:echo executable("black")
:echo executable("usort")

@aquillano
Copy link

Same issue here. black and isort not working via null-ls.

@BradLewis
Copy link

BradLewis commented Apr 2, 2023

I had this same issue where it would not autoformat with black but all of the commands mentioned above worked correctly. (:echo executable("black") would return 1, :!black % would correctly format the document etc). I enabled debug logging for null-ls and I was able to see this error in the logs.

[TRACE Sun  2 Apr 12:53:23 2023] ...y/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:204: error output: Traceback (most recent call last):
  File "/Users/bradlewis/.local/share/nvim/mason/bin/black", line 5, in <module>
    from black import patched_main
ModuleNotFoundError: No module named 'black'

Sure enough when I tried to run the file ~/.local/share/nvim/mason/bin/black I got the same error.

Reinstalling black through mason fixed the issue (:MasonInstall black) and now everything works without issue.
Not really sure what the underlying problem was with it in this case.

@serhez
Copy link

serhez commented Apr 2, 2023

Reinstalling, as @BradLewis mentions, doesn't work for me. This issue is still a thing.

@k14lb3
Copy link

k14lb3 commented Apr 7, 2023

Same issue here. As for me, I'm trying to use beautysh as the formatter.

  • :echo executable("beautysh") → Echoes 1
  • !beautysh % → Formats the document

When trying to format a file using beautysh via null-ls, it doesn't log this ...

[TRACE xxx] .../xxx: received LSP request for method textDocument/formatting
[TRACE xxx] .../xxx: running generators for method NULL_LS_FORMATTING
  • Apple M2 Pro
  • NVIM v0.8.3

@bncpr
Copy link

bncpr commented Jun 7, 2023

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

black --stdin-filename main.py --quiet -

in the shell outside of nvim results in black hanging and not returning. So I think null-ls just times out.
I'm using a workaround currently, basically changed the builtin formatter to use temp file instead of stdin:

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,
})

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests