stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.72k stars 142 forks source link

bug: ctx.dirname in the passed to the args function isn't set anymore #355

Closed cryptomilk closed 4 months ago

cryptomilk commented 4 months ago

Neovim version (nvim -v)

0.9.5

Operating system/version

Fedora 39

Add the debug logs

Log file

args ctx:                                                                                                                                                                           
{                                                                                                                                                                                   
  args = <function 1>,                                                                                                                                                              
  command = "shfmt",                                                                                                                                                                
  meta = {                                                                                                                                                                          
    description = "A shell parser, formatter, and interpreter with `bash` support.",                                                                                                
    url = "https://github.com/mvdan/sh"                                                                                                                                             
  },                                                                                                                                                                                
  stdin = true                                                                                                                                                                      
}                                                                                                                                                                                   
E5108: Error executing lua /home/asn/conform/repro.lua:44: bad argument #1 to 'find' (string expected, got nil)                                                                     
stack traceback:                                                                                                                                                                    
        [C]: in function 'find'                                                                                                                                                     
        /home/asn/conform/repro.lua:44: in function 'computed_args'                                                                                                                 
        ...nform/.repro/plugins/conform.nvim/lua/conform/runner.lua:29: in function 'build_cmd'                                                                                     
        ...nform/.repro/plugins/conform.nvim/lua/conform/runner.lua:280: in function 'run_formatter'                                                                                
        ...nform/.repro/plugins/conform.nvim/lua/conform/runner.lua:617: in function 'format_lines_sync'                                                                            
        ...nform/.repro/plugins/conform.nvim/lua/conform/runner.lua:573: in function 'format_sync'                                                                                  
        ...conform/.repro/plugins/conform.nvim/lua/conform/init.lua:450: in function 'format'                                                                                       
        [string ":lua"]:1: in main chunk

Describe the bug

I've used the ctx.dirname passed to the args function of the formatter to change the way how it works. This stopped working. It worked well in the past.

What is the severity of this bug?

tolerable (can work around it)

Steps To Reproduce

  1. nvim -u repro.lua samba/wurst.sh
  2. :lua require('conform').format()

Expected Behavior

ctx.dirname should be set

Minimal example file

#!/bin/bash

wurst ()
    {
        echo "What a wurst!"
            echo "Yes"
    }

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

        -- Conform checks if the commands are executable
        formatters_by_ft = {
            sh = { 'shfmt' },
        },
        formatters = {
            shfmt = vim.tbl_extend('force', require('conform.formatters.shfmt'), {
                -- You can set the args dynamically by providing a callback
                args = function(ctx)
                    vim.print("args ctx:")
                    vim.print(ctx)

                    if string.find(ctx.dirname, 'samba', 1, true) then
                        return {
                            '--posix',
                            '--indent',
                            '0',
                            '-filename',
                            '$FILENAME',
                        }
                    else
                        return { '--indent', '4', '-filename', '$FILENAME' }
                    end
                end,
            }),
        },
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"

vim.print("Run :lua require('conform').format()")

Additional context

No response

stevearc commented 4 months ago

Interesting, you should have been getting deprecation messages for a long time now. I changed the format of the args and other functions, but added a backwards-compatibility shim with a deprecation message. This was removed recently in 8d91cf1fb5c53860f137439e2b4cb27cbe9f6a22

You'll need to define your function as args = function(self, ctx) now