olimorris / codecompanion.nvim

✨ AI-powered coding, seamlessly in Neovim. Supports Anthropic, Copilot, Gemini, Ollama, OpenAI and xAI LLMs
MIT License
1.31k stars 89 forks source link

[Bug]: Non-modifiable buffer on second inline command #157

Closed S1M0N38 closed 3 months ago

S1M0N38 commented 3 months ago

Your minimal.lua config

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)

-- Your CodeCompanion setup
local plugins = {
  {
    "olimorris/codecompanion.nvim",
    dependencies = {
      { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate" },
      "nvim-lua/plenary.nvim",
      "nvim-telescope/telescope.nvim",
      {
        "stevearc/dressing.nvim",
        opts = {},
      },
    },
    opts = {
      -- Refer to: https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua
      strategies = { -- Change the adapters as required
        chat = { adapter = "copilot" },
        inline = { adapter = "copilot" },
        agent = { adapter = "copilot" },
      },
      opts = {
        log_level = "TRACE",
      },
    },
  },
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

-- setup Tree-sitter
local ok, treesitter = pcall(require, "nvim-treesitter.configs")
if ok then
  treesitter.setup({
    ensure_installed = { "lua", "markdown", "markdown_inline" },
    highlight = { enable = true },
  })
end

Error messages

Error executing vim.schedule lua callback: ...decompanion.nvim/lua/codecompanion/strategies/inline.lua:541: Buffer is not 'modifiable'
stack traceback:
        [C]: in function 'nvim_buf_set_text'
        ...decompanion.nvim/lua/codecompanion/strategies/inline.lua:541: in function 'append_to_buf'
        ...decompanion.nvim/lua/codecompanion/strategies/inline.lua:392: in function <...decompanion.nvim/lua/codecompanion/strategies/inline.lua:391>

Health check output

==============================================================================
codecompanion: require("codecompanion.health").check()

codecompanion.nvim report ~
- Log file: /Users/simo/.local/state/lazyvim/codecompanion.log
- OK plenary.nvim installed
- OK nvim-treesitter installed
- OK telescope.nvim installed
- OK dressing.nvim installed
- OK curl installed

Log output

Initiating Inline with args: {
  context = {
    bufnr = 1,
    buftype = "",
    cursor_pos = { 9, 0 },
    end_col = 12,
    end_line = 9,
    filename = "/Users/simo/Developer/codecompanion.nvim/lua/codecompanion/init.lua",
    filetype = "lua",
    is_normal = false,
    is_visual = true,
    lines = { "local M = {}" },
    mode = "v",
    start_col = 1,
    start_line = 9,
    winnr = 1000
  },
  prompts = { {
      content = <function 1>,
      opts = {
        tag = "system_tag",
        visible = false
      },
      role = "system"
    } }
}

...

Initiating Inline with args: {
  context = {
    bufnr = 1,
    buftype = "",
    cursor_pos = { 9, 0 },
    end_col = 12,
    end_line = 9,
    filename = "/Users/simo/Developer/codecompanion.nvim/lua/codecompanion/init.lua",
    filetype = "lua",
    is_normal = false,
    is_visual = true,
    lines = { "local M = {}" },
    mode = "v",
    start_col = 1,
    start_line = 9,
    winnr = 1000
  },
  prompts = { {
      content = <function 1>,
      opts = {
        tag = "system_tag",
        visible = false
      },
      role = "system"
    } }
}

...

Initiating Inline with args: {
  context = {
    bufnr = 1,
    buftype = "",
    cursor_pos = { 1, 0 },
    end_col = 60,
    end_line = 1,
    filename = "/Users/simo/Developer/codecompanion.nvim/lua/codecompanion/init.lua",
    filetype = "lua",
    is_normal = false,
    is_visual = true,
    lines = { 'local context_utils = require("codecompanion.utils.context")' },
    mode = "v",
    start_col = 1,
    start_line = 1,
    winnr = 1000
  },
  prompts = { {
      content = <function 1>,
      opts = {
        tag = "system_tag",
        visible = false
      },
      role = "system"
    } }
}

...

Initiating Inline with args: {
  context = {
    bufnr = 1,
    buftype = "",
    cursor_pos = { 4, 0 },
    end_col = 48,
    end_line = 4,
    filename = "/Users/simo/Developer/codecompanion.nvim/lua/codecompanion/init.lua",
    filetype = "lua",
    is_normal = false,
    is_visual = true,
    lines = { 'local util = require("codecompanion.utils.util")' },
    mode = "v",
    start_col = 1,
    start_line = 4,
    winnr = 1000
  },
  prompts = { {
      content = <function 1>,
      opts = {
        tag = "system_tag",
        visible = false
      },
      role = "system"
    } }
}

Describe the bug

The first chat buffer is unlocked (it works). However, when creating a new chat (new inline command), the chat buffer becomes locked and is only updated when the llm request finishes.

Reproduce the bug

https://github.com/user-attachments/assets/ff58c9f2-bc48-4ea8-95f1-183845922e89

Final checks

olimorris commented 3 months ago

That seems super odd. append_to_buf shouldn't be called as the classification is going straight to the chat strategy.

Could you share all of the logs? I'd expect line 306 to write a log entry of where the LLM thinks your prompt should be placed.

S1M0N38 commented 3 months ago

codecompanion.log

S1M0N38 commented 3 months ago

btw 2 things that can be added in the issue template about sharing logs:

olimorris commented 3 months ago

btw 2 things that can be added in the issue template about sharing logs:

  • logs are stored in .repro/state/nvim/codecompanion.log (not in ~/.config/nvim/state...)
  • tell the user to remove secrets (e.g. api_key for copilot)

Mighty good spot, thank you

olimorris commented 3 months ago

And to check, you're on the latest version of the plugin?

S1M0N38 commented 3 months ago

yep, clone from 1.8.1

S1M0N38 commented 3 months ago

ive only changed the adapter from openai to copilot

olimorris commented 3 months ago

https://github.com/user-attachments/assets/2c7af0fa-3e2a-490e-894c-522f22a8ddf7

I can't recreate this at all with your minimal.lua config. I pushed some minor updates to inline.lua earlier but can't believe that's sorted it.

S1M0N38 commented 3 months ago

I think I may have found the reason for this issue.

I was running nvim --clean -u minimal.lua inside the cloned repo olimorris/codecompanion.nvim. It's possible that when the CodeCompanion plugin was looking for some files, it first looked inside the cloned repo (my current working directory) and not in the location of the plugin installed with lazy (as shown in the first part of the video). There might be some global variables or states that are set in the plugin installed with lazy but not in the cloned version.

I've tried to reproduce the bug by running nvim --clean ... from another location, and I wasn't able to: everything works fine. I apologize for wasting your time on this non-existent bug.

I know that this is a kind of edge case, but should users be warned in the ISSUE template not to run nvim --clean ... inside the codecompanion.nvim directory?

olimorris commented 3 months ago

That's a very point as well. I'll amend the bug report template