stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.55k stars 135 forks source link

bug: stylua formatter doesn't respect stylua.toml file at root path #450

Closed Terseus closed 3 weeks ago

Terseus commented 3 weeks ago

Neovim version (nvim -v)

NVIM v0.10.0

Operating system/version

Archlinux

Add the debug logs

Log file

00:42:50[DEBUG] Running formatters on /home/terseus/dotfiles/nvim/.config/nvim/lua/plugins/conform.lua: { "stylua" }
00:42:50[INFO] Run stylua on /home/terseus/dotfiles/nvim/.config/nvim/lua/plugins/conform.lua
00:42:50[DEBUG] Run command: { "stylua", "--search-parent-directories", "--stdin-filepath", "/home/terseus/dotfiles/nvim/.config/nvim/lua/plugins/conform.lua", "-" }
00:42:50[DEBUG] Run CWD: /home/terseus/dotfiles
00:42:50[DEBUG] stylua exited with code 0

Describe the bug

stylua formatter doesn't respect the config file stylua.toml file, in my case located at:

/home/terseus/dotfiles/stylua.toml

If I run :lua require("conform").format() in Neovim there's no change in my badly formatted lua file. If I run stylua <filename> from the terminal from the directory /home/terseus/dotfiles stylua formats the file as expected.

What is the severity of this bug?

blocking (cannot use plugin)

Steps To Reproduce

Expected Behavior

require("conform").format() stylua formatting should be equivalent to running it from the terminal.

Minimal example file

See "Steps To Reproduce".

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

I've found that it works (somehow) if I change stylua args:

require("conform.formatters.stylua").args = { "--search-parent-directories", "$FILENAME" }

With this config stylua formats the file, although it doesn't get updated in the UI, I have to do :e to force Neovim to load the file and see it formatted.

matthewrobinsondev commented 3 weeks ago

Just ran into this myself

matthewrobinsondev commented 3 weeks ago

will try and find some time tomorrow to take a look if not

stevearc commented 3 weeks ago

What version of stylua are you using? I (obviously) use it all the time and haven't encountered any problems

Side note, for the args modification you made passing in the filename explicitly, you'll also want to set stdin = false. That's the reason you have to :e to force Neovim to see the changes.

matthewrobinsondev commented 3 weeks ago

My issue was using an old nightly build of 0.10, i upgraded to nightly 0.11 and it was resolved, apologies.

Terseus commented 3 weeks ago

@stevearc

❯ stylua --version
stylua 0.20.0

Sorry, my bad, I had a .styluaignore file with a silly rule that made stylua to ignore the specific directory with all of my plugins configs, thus looking like stylua didn't worked when using conform, only when using pre-commit or direct file formatting (for some reason).

Removing the .styluaignore made conform.nvim work with stylua without any problem.

Closing the issue, as it's invalid.