avante.nvim icon indicating copy to clipboard operation
avante.nvim copied to clipboard

bug: Undefined variable: b:stw_count

Open DoDoENT opened this issue 7 months ago • 3 comments

Describe the bug

This happened when I invoked claude-3.7-sonnet via copilot to edit multiple files:

Error executing vim.schedule lua callback: ...al/share/nvim/lazy/avante.nvim/lua/avante/utils/init.lua:1061: Vim:Error executing lua callback: [string ":lua"]:1: function StripTrailingWhitespaceListener, line 1: Vim(if):E121: Undefined variable: b:stw_count
stack traceback:
        [C]: in function 'nvim_call_function'
        [string ":lua"]:1: in function <[string ":lua"]:1>
        [C]: in function 'nvim_buf_set_lines'
        ...al/share/nvim/lazy/avante.nvim/lua/avante/utils/init.lua:1061: in function 'update_buffer_lines'
        ...local/share/nvim/lazy/avante.nvim/lua/avante/sidebar.lua:1545: in function 'f'
        ...local/share/nvim/lazy/avante.nvim/lua/avante/sidebar.lua:1564: in function ''
        vim/_editor.lua: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
stack traceback:
        [C]: in function 'nvim_buf_set_lines'
        ...al/share/nvim/lazy/avante.nvim/lua/avante/utils/init.lua:1061: in function 'update_buffer_lines'
        ...local/share/nvim/lazy/avante.nvim/lua/avante/sidebar.lua:1545: in function 'f'
        ...local/share/nvim/lazy/avante.nvim/lua/avante/sidebar.lua:1564: in function ''
        vim/_editor.lua: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

To reproduce

  • Select model claude-3.7-sonnet on copilot provider.
  • tell it to implement a feature that requires multiple file use

Expected behavior

The error should not happen.

Installation method

Use lazy.nvim:

return {
    {
        'yetone/avante.nvim',
        event = "VeryLazy",
        opts = {
            provider = "copilot",
            copilot = {
                model = "gpt-4o",
            },
            vendors = {
                ["copilot-gemini"] = {
                    __inherited_from = "copilot",
                    model = "gemini-2.0-flash-001",
                },
                ["copilot-claude37-thought"] = {
                    __inherited_from = "copilot",
                    model = "claude-3.7-sonnet-thought",
                },
                ["copilot-claude35"] = {
                    __inherited_from = "copilot",
                    model = "claude-3.5-sonnet",
                },
                ["copilot-claude37"] = {
                    __inherited_from = "copilot",
                    model = "claude-3.7-sonnet",
                },
            }
        },
        build = "make",
        dependencies = {
            "nvim-treesitter/nvim-treesitter",
            "stevearc/dressing.nvim",
            "nvim-lua/plenary.nvim",
            "MunifTanjim/nui.nvim",
            --- The below dependencies are optional,
            "echasnovski/mini.pick",         -- for file_selector provider mini.pick
            "nvim-telescope/telescope.nvim", -- for file_selector provider telescope
            "hrsh7th/nvim-cmp",              -- autocompletion for avante commands and mentions
            "ibhagwan/fzf-lua",              -- for file_selector provider fzf
            "nvim-tree/nvim-web-devicons",   -- or echasnovski/mini.icons
            "zbirenbaum/copilot.lua",        -- for providers='copilot'
            {
                -- support for image pasting
                "HakonHarnes/img-clip.nvim",
                event = "VeryLazy",
                opts = {
                    -- recommended settings
                    default = {
                        embed_image_as_base64 = false,
                        prompt_for_file_name = false,
                        drag_and_drop = {
                            insert_mode = true,
                        },
                        -- required for Windows users
                        use_absolute_path = true,
                    },
                },
            },
            {
                -- Make sure to set this up properly if you have lazy=true
                'MeanderingProgrammer/render-markdown.nvim',
                opts = {
                    file_types = { "markdown", "Avante" },
                },
                ft = { "markdown", "Avante" },
            },
        },
    }
}

Environment

neovim: 0.11.1 MacOS

Repro

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
  spec = {
    -- add any other plugins here
  },
})

DoDoENT avatar May 14 '25 09:05 DoDoENT

+1 I get this too

jvsg avatar May 16 '25 17:05 jvsg

So this is actually a bug with another plugin vim-strip-trailing-whitespace which we both happen to use

Anyway, here's the patch to fix it

diff --git a/plugin/strip_trailing_whitespace.vim b/plugin/strip_trailing_whitespace.vim
index fba0e98..23f8c43 100644
--- a/plugin/strip_trailing_whitespace.vim
+++ b/plugin/strip_trailing_whitespace.vim
@@ -198,6 +198,8 @@ function s:OnBufEnter() abort
                " Parsing the lua Ex command can fail on broken Vim <8.2.1908 installs
                execute 'lua vim.api.nvim_buf_attach(0, false, {
                                        \ on_lines = function(_, bufnr, _, firstline, lastline, new_lastline)
+                                       \ vim.b.stw_root = vim.v.null
+                                       \ vim.b.stw_count = 0
                                        \ vim.api.nvim_call_function("StripTrailingWhitespaceListener", {bufnr, firstline + 1, lastline + 1, new_lastline - lastline,
                                        \ {{lnum = firstline + 1, ["end"] = lastline + 1, added = new_lastline - lastline, col = 1}}})
                                        \ end, })'

You can also switch to jvsg/vim-strip-trailing-whitespace and receive this update. The upstream hasn't been updated in years.</S>

My patch has a bug. The author of that plugin has a better fix https://github.com/axelf4/vim-strip-trailing-whitespace/pull/11#issuecomment-2888440169

jvsg avatar May 16 '25 19:05 jvsg

Great! I've updated the vim-strip-trailing-whitespace to use set-on_lines-buf branch until the patch gets merged to master.

DoDoENT avatar May 20 '25 07:05 DoDoENT

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jun 20 '25 02:06 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Jun 25 '25 02:06 github-actions[bot]