null-ls.nvim icon indicating copy to clipboard operation
null-ls.nvim copied to clipboard

stylua not respecting stylua.toml

Open esn89 opened this issue 2 years ago • 3 comments

FAQ

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

Issues

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

Neovim Version

NVIM v0.7.2

Operating System

MacOS 12.5

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 nls = require("null-ls")
local nls_utils = require("null-ls.utils")
local b = nls.builtins

local sources = {
	b.formatting.stylua.with( {extra_args = { "--config-path", "/Users/itsme/.stylua.toml" }})
}

local null_ls_config = function()
    local null_ls = require("null-ls")
    -- add only what you need to reproduce your issue
    null_ls.setup({
        sources = sources,
        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

Open an lua file and then save, it does not seem to respect the 4 spaces indent as declared in my ~/.stylua.toml, but goes to 8.

Here is the contents of the stylua.toml

column_width = 120
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferSingle"
call_parentheses = "Always"

Expected behavior

I expect it to respect the stylua.toml rules.

Actual behavior

It follows some weird default of 8 spaces. There is formatting, but not quite the way I want it to.

I am even tried to set it as the default method:

b.formatting.stylua
}

but still the same

Debug log

[ERROR Sat Jul 23 12:02:48 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command flake8 is not executable (make sure it's installed and on your $PATH)
[ERROR Sat Jul 23 12:04:07 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command flake8 is not executable (make sure it's installed and on your $PATH)
[ERROR Sat Jul 23 12:04:29 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command black is not executable (make sure it's installed and on your $PATH)
[ERROR Sat Jul 23 13:00:39 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command luacheck is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:13:03 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:13:12 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:13:36 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:13:47 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:15:13 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:15:19 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:21:25 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)
[ERROR Tue Jul 26 21:21:34 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:180: command fixjson is not executable (make sure it's installed and on your $PATH)

Help

No

Implementation help

No response

Requirements

  • [X] I have read and followed the instructions above and understand that my issue will be closed if I did not provide the required information.

esn89 avatar Jul 30 '22 06:07 esn89

The debug log is missing debug info, so I don't think you've enabled debug mode. I would recommend doing that, since otherwise I don't have enough info to really proceed. What happens when you run stylua from the command line with the same extra arguments you've specified?

jose-elias-alvarez avatar Aug 01 '22 01:08 jose-elias-alvarez

Hi @jose-elias-alvarez

That's odd, I did turn on debug mode:


local null_ls_config = function()
    local null_ls = require("null-ls")
    -- add only what you need to reproduce your issue
    null_ls.setup({
        sources = sources,
        debug = true,
    })
end

Is there another way to do so?

esn89 avatar Aug 01 '22 19:08 esn89

That's the right way to do it - did you try formatting a file with stylua after enabling the option? Also, did you try running the command directly from the command line? This is what null-ls is doing under the hood (replace file.lua with an actual Lua file):

cat file.lua | stylua --stdin-filepath file.lua - --config-path /Users/itsme/.stylua.toml

This works on my end, so if that doesn't work for you, that might give us a hint.

jose-elias-alvarez avatar Aug 02 '22 13:08 jose-elias-alvarez

@esn89, Stylua, as it's set up in null-ls i.e.

  generator_opts = {
    command = "stylua",
    args = h.range_formatting_args_factory({
      "--search-parent-directories",
      "--stdin-filepath",
      "$FILENAME",
      "-",
      }, "--range-start", "--range-end", { row_offset = -1, col_offset = -1 }),
    to_stdin = true,
  },

will search recursively for a configuration file, see stylua#finding-the-configuration, therefore, since you provided an extra-args extra_args = { "--config-path", "/Users/itsme/.stylua.toml" }, I suppose the issue could be there.

My suggestion is to place a stylua.toml at your project's root like suggested in Stylua's README since null-ls built-in stylua has its arguments dynamically resolved and drop the --config-path.

kaiuri avatar Aug 04 '22 00:08 kaiuri

Even with the --search-parent-directories flag enabled, I can verify that stylua still behaves correctly and picks up the config file if its location is specified. I think there's something else going on here.

jose-elias-alvarez avatar Aug 04 '22 13:08 jose-elias-alvarez

@jose-elias-alvarez hmm... Maybe it could be Windows related then?

kaiuri avatar Aug 04 '22 16:08 kaiuri

@esn89 I don't think stylua is finding your config file in your home directory, according to their docs it either looks for the stylua.toml/.stylua.toml config file in the directory where you're running the command from or in $XDG_CONFIG_HOME. Is the test file also in the home directory? You can also pass a custom config path with --config-path, in your case --config-path ~/.stylua.toml

mtrajano avatar Aug 04 '22 19:08 mtrajano

Correct, I think this is what's happening.

Let me try this again tonight, real quick.

Edit:

I think it is because of this:

error: Config file not in correct format: unknown field `call_parentheses`, expected one of `column_width`, `line_endings`, `indent_type`, `indent_width`, `quote_style`, `no_call_parentheses` at line 1 column 1

It doesn't like the "call_parentheses". Which is odd, because I followed the doc: error: Config file not in correct format: unknown field call_parentheses, expected one of column_width, line_endings, indent_type, indent_width, quote_style, no_call_parentheses at line 1 column 1

Will try to remove this and keep on trying.

esn89 avatar Aug 05 '22 01:08 esn89

Oh god, halleluja, removing that stupid "call_parentheses" in my ~/.config/.stylua.toml works.

Thank you @jose-elias-alvarez for being patient with me and helping and thank you @mtrajano for letting me know about the correct path.

esn89 avatar Aug 05 '22 03:08 esn89