stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.58k stars 136 forks source link

bug: Fails running command that works fine on the shell #414

Closed artfulrobot closed 1 month ago

artfulrobot commented 1 month ago

Neovim version (nvim -v)

v0.9.5

Operating system/version

Fedora 39 Linux

Add the debug logs

Log file

17:16:43[DEBUG] Running formatters on /home/rich/conformbug/test.php: { "phpcbf" }
17:16:43[INFO] Run phpcbf on /home/rich/conformbug/test.php
17:16:43[DEBUG] Creating temp file /home/rich/conformbug/conform.7289672.test.php
17:16:43[DEBUG] Run command: { "/home/rich/.local/share/nvim/mason/bin/phpcbf", "--standard=/home/rich/nobackup/civicrm-coder/coder_sniffer/Drupal" }
17:16:43[INFO] phpcbf exited with code 3
17:16:43[DEBUG] phpcbf stdout: nil
17:16:43[DEBUG] phpcbf stderr: { "" }
17:16:43[DEBUG] Cleaning up temp file /home/rich/conformbug/conform.7289672.test.php
17:16:43[ERROR] Formatter 'phpcbf' error:

Describe the bug

Fails to run the command, yet running the command on the command line (outside of vim) works.

What is the severity of this bug?

blocking (cannot use plugin)

Steps To Reproduce

  1. Open a PHP file
  2. Save.
  3. Crash message

Expected Behavior

It should format/fix the formatting in the file.

e.g. taking the command from ConformInfo and pasting it onto the command line works:

~/conformbug% /home/rich/.local/share/nvim/mason/bin/phpcbf --standard=/home/rich/nobackup/civicrm-coder/coder_sniffer/Drupal test.php                                                                                                           

PHPCBF RESULT SUMMARY                                                                                                                                                                                                                                          
----------------------------------------------------------------------                                                                                                                                                                                         
FILE                                                  FIXED  REMAINING                                                                                                                                                                                         
----------------------------------------------------------------------                                                                                                                                                                                         
/home/rich/conformbug/test.php                        4      0                                                                                                                                                                                                 
----------------------------------------------------------------------                                                                                                                                                                                         
A TOTAL OF 4 ERRORS WERE FIXED IN 1 FILE                                                                                                                                                                                                                       
----------------------------------------------------------------------                                                                                                                                                                                         

Time: 45ms; Memory: 10MB                                                                                                                                                                                                                                       

Minimal example file

test.php

<?php                                                                                                                                                                                                                                                          

if(0)                                                                                                                                                                                                                                                          
{                                                                                                                                                                                                                                                              
        print "no";                                                                                                                                                                                                                                            
  } 

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
        formatters = {
          phpcbf = {
            command = '/home/rich/.local/share/nvim/mason/bin/phpcbf',
            args = { "--standard=/home/rich/nobackup/civicrm-coder/coder_sniffer/Drupal" }
          }
        },
        formatters_by_ft = {
          php = { "phpcbf" },
        },
        format_on_save = function(bufnr)
          -- Disable with a global or buffer-local variable
          if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
            print("format_on_save is disabled")
            return
          end
          return { timeout_ms = 10000, lsp_fallback = true, async = false }
        end,
      })

      vim.api.nvim_create_user_command("FormatDisable", function(args)
        if args.bang then
          -- FormatDisable! will disable formatting for all files.
          vim.g.disable_autoformat = true
        else
          -- FormatDisable just affects this buffer.
          vim.b.disable_autoformat = true
        end
      end, {
        desc = "Disable autoformat-on-save",
        bang = true,
      })
      vim.api.nvim_create_user_command("FormatEnable", function()
        vim.b.disable_autoformat = false
        vim.g.disable_autoformat = false
      end, {
        desc = "Re-enable autoformat-on-save",
      })
      vim.keymap.set({ "n", "v" }, "<leader>mp", function()
        conform.format({
          lsp_fallback = true,
          async = false,
          timeout_ms = 500,
        })
      end, { desc = "Format file or range (in visual mode)" })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

It references /home/rich/nobackup/civicrm-coder/coder_sniffer/Drupal

You get that by: git clone https://github.com/civicrm/coder civicrm-coder

stevearc commented 1 month ago

According to this comment an exit code of 3 indicates that an invalid standard was given. I have no idea what that means, but perhaps it makes sense to someone more familiar with phpcbf.

It is possible that phpcbf behaves differently because of the temp file. Try copying test.php to conform.7289672.test.php and re-run the command on the CLI targeting that file.

It's also possible that the cwd could affect it. Make sure you're opening vim from the same directory that you're running the command from.

You can also try running the command directly with jobstart to see if that's where the problem is coming from

vim.fn.jobstart(
  {
    "/home/rich/.local/share/nvim/mason/bin/phpcbf",
    "--standard=/home/rich/nobackup/civicrm-coder/coder_sniffer/Drupal",
    "test.php",
  },
  {
    on_exit = function(_, code)
      print("Exited with code", code)
    end,
  }
)
artfulrobot commented 1 month ago

@stevearc thanks so much for your time on this.

Try copying test.php to conform.7289672.test.php and re-run the command on the CLI targeting that file.

CLI called command runs fine, corrects the conform.7289672.test.php file.

It's also possible that the cwd could affect it. Make sure you're opening vim from the same directory that you're running the command from.

Done and makes no difference.

try running the command directly with jobstart ...

Same.

artfulrobot commented 1 month ago

But I've found the problem!

conform - as configured by me - was not passing in the filename to be worked on.

Altering my config to this fixed it:

        formatters = {
          phpcbf = {
            command = '/home/rich/.local/share/nvim/mason/bin/phpcbf',
-            args = { "--standard=/home/rich/nobackup/civicrm-coder/coder_sniffer/Drupal" }
+           args = { "--standard=/home/rich/nobackup/civicrm-coder/coder_sniffer/Drupal", '$FILENAME' }
          }
        },

Thanks for the ace plugin and your help here. @stevearc