nvimdev / guard.nvim

async fast minimalist plugin make format easy in neovim
MIT License
441 stars 23 forks source link

Failed to spawn .cmd executables on windows #111

Closed scottmckendry closed 1 month ago

scottmckendry commented 11 months ago

:checkheath guard shows prettier as executable image

but when testing prettier on an HTML file I get the message:

[Guard] failed to spawn process prettier

This is my config:

return {
    "nvimdev/guard.nvim",
    dependencies = {
        "nvimdev/guard-collection",
    },
    event = "BufReadPre",
    config = function()
        local ft = require("guard.filetype")
        ft("go")
            :fmt({
                cmd = "goimports-reviser",
                args = { "-output", "stdout" },
                fname = true,
            })
            :append({
                cmd = "gofmt",
                stdin = true,
            })
            :append({
                cmd = "golines",
                stdin = true,
            })

        ft("lua"):fmt("lsp")

        ft("html"):fmt("prettier")

        require("guard").setup({
            fmt_on_save = true,
            lsp_as_default_formatter = false,
        })
    end,
}

running cat index.html | prettier --stdin-filepath index.html from the commandlne outputs the formatted file with no errors.

Any ideas what I can check/test to try and get this working?

scottmckendry commented 11 months ago

I've just tested this on my Linux machine and can not replicate the error - so it's isolated to Windows 11. The exact same issue is also occurring with stylua. Works on Linux, and doesn't spawn on Windows (which is why I'm using lsp format instead).

gofmt is working fine in both os

barrett-ruth commented 11 months ago

None of us have a windows device I believe. Attach a lot of debugging info and also try to fix the issue yourself. Make sure/check that the process is executable (vim.fn.executable), that you can start a job with it (uv.spawn), etc.

xiaoshihou514 commented 11 months ago

I personally don't see an obvious issue so :( And as you said it's your win machine only so if it's not a uv.spawn quirk then I am not sure how to approach this.

UnderCooled commented 8 months ago

I've just tested this on my Linux machine and can not replicate the error - so it's isolated to Windows 11. The exact same issue is also occurring with stylua. Works on Linux, and doesn't spawn on Windows (which is why I'm using lsp format instead).

gofmt is working fine in both os

You need to add .cmd to those Node.js based tools on Windows, uv.spawn won't see these .cmd scripts as executable as vim.fn.executable on Windows.

{
  cmd = "prettier.cmd", 
  args = { '--stdin-filepath' },
  fname = true,
  stdin = true,
}
barrett-ruth commented 8 months ago

@UnderCooled thanks for helping out with this. I looked at the executable() documentation and it seems like it automatically searches using the .cmd extension, so uv.spawn() failing instead does make sense.

I'm not accustomed to windows systems - isn't it true that some of the programs that, for example, guard-collection uses, could be .cmd, while others could be .exe?

If that's the case, I'm not sure how we could resolve this besides offering a warning to windows systems that no extension is specified.

UnderCooled commented 8 months ago

@barrett-ruth you can patch https://github.com/nvimdev/guard.nvim/blob/394317c25a6b0f0e064aebcfcf902e46fb0a04ba/lua/guard/spawn.lua#L38

local cmd = vim.fn.exepath(opt.cmd)
:echo exepath('npm')
D:\dev_env\lang\node\npm.CMD