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

Buf: formatting not working anymore #1002

Closed
3 tasks done
Ganitzsh opened this issue Aug 11, 2022 · 8 comments
Closed
3 tasks done

Buf: formatting not working anymore #1002

Ganitzsh opened this issue Aug 11, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@Ganitzsh
Copy link

Ganitzsh commented Aug 11, 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

v0.7.2

Operating System

macOS 12.4

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 = {
	    require("null-ls").builtins.formatting.buf
        },
        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

You also need to install https://github.com/bufbuild/buf

Steps to reproduce

Open the following file:

syntax = "proto3";

package straudio.v1;

message AudioChunk {
     string name = 1;

  bytes data = 2;
}

run :lua vim.lsp.buf.formatting()

Expected behavior

The file should get formatted

Actual behavior

Nothing happens

Debug log

[TRACE Thu Aug 11 08:48:16 2022] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:121: received LSP request for method textDocument/formatting
[TRACE Thu Aug 11 08:48:16 2022] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_FORMATTING
[DEBUG Thu Aug 11 08:48:16 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:346: spawning command "buf" at /Users/<me>/Dev/audio-streamer with args { "format", "-w", "/tmp/null-ls_SP9h7H.proto" }
[TRACE Thu Aug 11 08:48:16 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:217: error output: Failure: failed to enumerate module files: open /private/tmp/.vnc-vncservice: permission denied

[TRACE Thu Aug 11 08:48:16 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:218: output: nil
[TRACE Thu Aug 11 08:48:16 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:222: ignoring stderr due to generator options

Not sure why it's trying to access a vnc file which is nowhere near the file I'm editing

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.
@Ganitzsh Ganitzsh added the bug Something isn't working label Aug 11, 2022
@jose-elias-alvarez
Copy link
Owner

This works for me, and I don't think it's an issue with null-ls. I have no idea what this error message means, but its presence is what's causing formatting to fail:

Failure: failed to enumerate module files: open /private/tmp/.vnc-vncservice: permission denied

Do you get the same error when you run the formatter from the command line? Either way I think you'll need to look into this on the buf side.

@Ganitzsh
Copy link
Author

Ganitzsh commented Aug 12, 2022

I've tried to run buf directly and it works fine. It worked fine in nvim until a month ago or so. I checked other logs and I don't see anything wrong there. I've tried many configurations, and it doesn't fix the problem, I've looked everywhere and I'm running out of ideas.

I'll post the issue on the buf repository directly and come back here if anything fruitful comes out of it.

@jose-elias-alvarez
Copy link
Owner

I'll mention that looking at the commit history, we haven't made any changes that should affect this (the only change we've made to the built-in definition was one that made it work in the first place, and there haven't been any changes to null-ls core in this area).

@Ganitzsh
Copy link
Author

Ganitzsh commented Aug 13, 2022

Thanks for the details! So according to buf's maintainer the issue comes from the fact that buf attempts to read other proto files at the root of the target hence the open error within the /private/tmp directory. (bufbuild/buf#1334 (comment))

Is it possible to override the temp file location for a specific generator directly from the config? It'd allow me to work with buf while the mentioned issue (bufbuild/buf#1035) is worked on and the buf command can be changed in null-ls to use stdin

@jose-elias-alvarez
Copy link
Owner

It's not currently possible to override the temporary directory, though from an architectural point of view it shouldn't be that hard to do so.

I don't plan on working on this myself, since I think having to use a temp file for formatters and linters is an unpleasant workaround, and if there's any chance of getting stdin support upstream, I think that's the way to go. I'm happy to review and consider merging a PR that doesn't add too much complexity in this area.

@Ganitzsh
Copy link
Author

Okay thanks! I'll close this one as it is mostly resolved.

@kyle-mccarthy
Copy link

@Ganitzsh I was able to get this working correctly with the following:

null_ls.builtins.formatting.buf.with(
  {
    extra_args = function(params)
      local scan = require("plenary.scandir")

      local paths = scan.scan_dir(params.root, {search_pattern = "buf.yaml"})
      local config = paths[1]

      if config then
        return {"--config=" .. config, "--path=" .. params.bufname, "-o" .. params.temp_path}
      end

      return {}
    end,
    args = {"format"}
  }
)

@jose-elias-alvarez I can open a PR if you want. One thing I wasn't sure about is if to_temp_file and from_temp_file could be omitted and the bufname could be used for the output path. Also, when -o or -w aren't specified, it does write the output to stdout, can null-ls redirect it to the buffer?

@jose-elias-alvarez
Copy link
Owner

@kyle-mccarthy This seems like a reasonable workaround for now, but I would rather wait until we merge #1021.

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

3 participants