zapling / mason-conform.nvim

Automatically install formatters registered with conform.nvim via mason.nvim
Apache License 2.0
41 stars 1 forks source link

Plugin breaks with function in table #8

Open TabulateJarl8 opened 1 month ago

TabulateJarl8 commented 1 month ago

Hey! I started using your plugin today and it works great, however I believe I've discovered a bug. I'm not sure if this is something you're actually able to fix on your side, but here's an example snipped from my configuration for conform which triggers the error:

local prettier = function(bufnr)
    if require("conform").get_formatter_info("prettier", bufnr).available then
        return { "prettier" }
    else
        return {}
    end
end

require("conform").setup({
    formatters_by_ft = {
        javascript = prettier,
        typescript = prettier,
        vue = prettier,
    },
})

It throws this error on startup:

Error detected while processing BufEnter Autocommands for "*":
Error executing vim.schedule lua callback: ...ed/mason-conform.nvim/lua/mason-conform/auto_install.lua:10: bad argument #1 to 'pairs' (table expected, got function)
stack traceback:
        [C]: in function 'pairs'
        ...ed/mason-conform.nvim/lua/mason-conform/auto_install.lua:10: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
        [C]: in function 'wait'
        ...oload/plugged/nvim-tree.lua/lua/nvim-tree/git/runner.lua:171: in function '_wait'
        ...oload/plugged/nvim-tree.lua/lua/nvim-tree/git/runner.lua:221: in function 'run'
        ...utoload/plugged/nvim-tree.lua/lua/nvim-tree/git/init.lua:243: in function 'load_project_status'
        ...ad/plugged/nvim-tree.lua/lua/nvim-tree/explorer/init.lua:49: in function '_load'
        ...ad/plugged/nvim-tree.lua/lua/nvim-tree/explorer/init.lua:41: in function 'new'
        ...im/autoload/plugged/nvim-tree.lua/lua/nvim-tree/core.lua:20: in function 'init'
        .../nvim-tree.lua/lua/nvim-tree/actions/root/change-dir.lua:88: in function 'f'
        .../nvim-tree.lua/lua/nvim-tree/actions/root/change-dir.lua:77: in function 'force_dirchange'
        ...ig/nvim/autoload/plugged/nvim-tree.lua/lua/nvim-tree.lua:117: in function <...ig/nvim/autoload/plugged/nvim-tree.lua/lua/nvim-tree.lua:105>

If i replace the function in the table with just {"prettier"} the issue goes away. Also, if I call the function like prettier() instead of just putting the function object in the table, I believe the issue also goes away. Now that I'm using this plugin, I don't really need to worry about needing this specific function, but it may come up in the future for others so I figured I'd document it here at least. Thanks for your work, and let me know if you need any more info

zapling commented 1 month ago

Thanks for the report :smile:

I'm not sure if this is something you're actually able to fix on your side

I did not know that you could provide a function to conform in this manner.

Now that I'm using this plugin, I don't really need to worry about needing this specific function

Yea that is my take on this issue as well. Configuring a function like this with conform does not really make sense in the context of this plugin, at least not to me.

I'd say this is very low on my priority list right now.

marty-oehme commented 2 weeks ago

I have stumbled upon this as well when selecting one of multiple formatters, followed by running more formatters.

With the release of 7.0.0, conform.nvim switched to a new configuration format which deprecates the nested table configuration style for running one formatter out of a pool and subsequently another formatter:

require("conform").setup({
    formatters_by_ft = {
        markdown = { { "prettier", "prettierd" }, "cbfmt" },
    },
})

This produces a warning with the new version. The manual instead recommends the following code recipe:

---@param bufnr integer
---@param ... string
---@return string
local function first(bufnr, ...)
    local conform = require("conform")
    for i = 1, select("#", ...) do
        local formatter = select(i, ...)
        if conform.get_formatter_info(formatter, bufnr).available then
            return formatter
        end
    end
    return select(1, ...)
end

require("conform").setup({
    formatters_by_ft = {
        markdown = function(bufnr)
            return { first(bufnr, "prettierd", "prettier"), "cbfmt" }
        end,
    },
})

This seems to pose the same issue again.

I agree that it's not a priority issue but with the documentation recommending this way of configuration I think more reports are going to appear which stumble upon the same error.

zapling commented 2 weeks ago

With the release of 7.0.0, conform.nvim switched to a new configuration format

Thanks for the heads up, we should definitely support this going forward then.