stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.02k stars 156 forks source link

bug: Error detected while processing BufWritePre Autocommands for "*": Formatter failed see :ConformInfo for more details #393

Closed thecode-master-sy closed 5 months ago

thecode-master-sy commented 5 months ago

Neovim version (nvim -v)

0.9.5

Operating system/version

Windows 10 pro/10.0.19045 build 19045

Add the debug logs

Log file

Log file: C:\Users\thecodemaster\AppData\Local\nvim-data\conform.log
          22:13:26[INFO] stylua exited with code 1
          22:13:26[DEBUG] stylua stdout: { "" }
          22:13:26[DEBUG] stylua stderr: { "The system cannot find the path specified.\r", "" }
          22:13:26[ERROR] Formatter 'stylua' error: The system cannot find the path specified.

          22:15:36[DEBUG] Running formatters on C:\Users\thecodemaster\Desktop\ecommerce-website\components\Button.tsx: { "prettier" }
          22:15:36[INFO] Run prettier on C:\Users\thecodemaster\Desktop\ecommerce-website\components\Button.tsx
          22:15:36[DEBUG] Run command: { "prettier.cmd", "--stdin-filepath", "C:\\Users\\thecodemaster\\Desktop\\ecommerce-website\\components\\Button.tsx" }
          22:15:36[DEBUG] Run CWD: C:/Users/thecodemaster/Desktop/ecommerce-website
          22:15:36[INFO] prettier exited with code 1
          22:15:36[DEBUG] prettier stdout: { "" }
          22:15:36[DEBUG] prettier stderr: { "The system cannot find the path specified.\r", "" }
          22:15:36[ERROR] Formatter 'prettier' error: The system cannot find the path specified.

Formatters for this buffer:
prettier ready (json, liquid, graphql, yaml, html, css, svelte, typescriptreact, javascriptreact, typescript, markdown, javascript) C:\Users\thecodemaster\AppData\Local\nvim-data\mason\bin\prettier.cmd

Other formatters:
stylua ready (lua) C:\Users\thecodemaster\AppData\Local\nvim-data\mason\bin\stylua.CMD

Describe the bug

Prettier and stylua fails to format on save instead it gives the above error

What is the severity of this bug?

blocking (cannot use plugin)

Steps To Reproduce

  1. install the conform plugin
  2. open a file and try to save.

Expected Behavior

Prettier and stylua should be able to format on save with no errors

Minimal example file

No response

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

No response

mathletedev commented 5 months ago

Also getting an error with prettier and prettierd, except the message is blank: Formatter 'prettierd' error: However stylua seems to be working fine on my machine

dorage commented 5 months ago

I had a same error on lua and ts project. I guess it is bcs of AST parsing is not possible by syntax errors. I solved with quiet=true option.

  format_on_save = { timeout_ms = 500, lsp_fallback = true, async = true, **quiet = true** },
mathletedev commented 5 months ago

I had a same error on lua and ts project. I guess it is bcs of AST parsing is not possible by syntax errors. I solved with quiet=true option.

  format_on_save = { timeout_ms = 500, lsp_fallback = true, async = true, **quiet = true** },

Thanks for the help! With quiet=true conform started producing other errors in its log file, which helped me trace back to the root problem. My issue was that I upgraded to Pretter 3, which conflicted with some of my other packages.

stevearc commented 5 months ago

@thecode-master-sy please ensure that you have set the log level to DEBUG as mentioned in the bug report template. There are a lot of debug logs missing in ConformInfo that would aid in debugging.

thecode-master-sy commented 5 months ago

@stevearc I have done that now, and have updated the log files

stevearc commented 5 months ago

So the first step would be to try to run prettier on the command line to see if that works. If you just run prettier.cmd <file> does that format it?

The next step would be to run it with the same args as conform, using stdin. I don't know Windows command shell syntax, but in bash it would be something like prettier.cmd --stdin-filepath C:\Users\thecodemaster\Desktop\ecommerce-website\components\Button.tsx <C:\Users\thecodemaster\Desktop\ecommerce-website\components\Button.tsx.

Hmmm...looking at it again, I wonder if the issue could be caused by the CWD? C:/Users/thecodemaster/Desktop/ecommerce-website has the wrong slashes, but it should be coming from vim.fs.find. Also, if this didn't work I don't think most formatters would work on Windows. Still, to test that out you can remove the CWD and see if that changes the behavior.

require("conform").setup({
  formatters = {
    prettier = {
      cwd = function() end,
    },
  },
})
thecode-master-sy commented 5 months ago

when I run prettier.cmd on the file path, it doesn't actually format the file, it just logs out the content of the file.

prettier-cmd

Same with the prettier.cmd --stdin-filepath C:\Users\thecodemaster\Desktop\ecommerce-website\components\Button.tsx but this time it doesn't log anything at all

prettier-cmd--stdin

However if I run prettier --write on the file it formats it

thecode-master-sy commented 5 months ago

uninstalling and reinstalling conform fixed this error, it seems like was previously configured with the wrong path. I am not sure how that happened. @stevearc thanks so much for your efforts.