dpp.vimでavante.nvimを入れたらちょっと詰まった
「「Cursorっぽい感じのAI支援機能、Neovimにも欲しい!!!」」
になったので avante.nvim というプラグインを入れてみました。
入れるとこんな感じになります。
提案を受け入れるかどうかをマージエディタみたいに選択できて良い感じです ✌️
起きたこと
avante.nvim のウィンドウは開くものの、インサートモードに入ると以下のようなエラーが出ました。
ModeChanged Autocommands for "*:i" の処理中にエラーが検出されました:
lua コールバックの実行中にエラーが発生しました: ...rs/arrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/avante/llm.lua:47: Make sure to build avante (missing avante_templates)
stack traceback:
[C]: in function 'error'
...s/arrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/avante/path.lua:96: in function 'get'
...rs/arrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/avante/llm.lua:47: in function 'generate_prompts'
...rs/arrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/avante/llm.lua:126: in function 'calculate_tokens'
...rrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/avante/sidebar.lua:2122: in function 'show_hint'
結論
dpp.vim のベースディレクトリ内の /nvim/.dpp/build
に、avante.nvim のリポジトリ内の /build
ディレクトリへのシンボリックリンクを作成すると動きました。
ln -s $HOME/.cache/nvim/dpp/repos/github.com/yetone/avante.nvim/build $HOME/.cache/nvim/dpp/nvim/.dpp/build
以下、解決するまでの作業ログです 🏗️
導入してみる
記事タイトルにもあるように、プラグインマネージャーに dpp.vim を使っていて、設定はTOMLで管理しています。
copilot.lua の設定など、一部省略している部分もありますが設定はこんな感じで書きました。
[[plugins]]
repo = "stevearc/dressing.nvim"
[[plugins]]
repo = "MunifTanjim/nui.nvim"
[[plugins]]
repo = "yetone/avante.nvim"
build = "make"
depends = [
"dressing.nvim",
"plenary.nvim",
"nui.nvim",
"nvim-web-devicons",
"copilot.lua",
]
lua_source = """
require('avante_lib').load()
require('avante').setup({
provider = "copilot",
})
"""
これで起動したところ、起きたこと のエラーが発生しました。
Issueを漁る
"Make sure to build avante (missing avante_templates)" で Issue を漁ると、「makeを実行すればいいかも!」みたいなコメントを見つけました。
Sorry, the specific reason was not found because theoretically everything should work fine if you have build = "make" in your lazy config. I also couldn't reproduce it locally using Lazy.nvim, but you can manually fix this issue with the following steps:
https://github.com/yetone/avante.nvim/issues/612#issuecomment-2375729928
TOMLの設定の build = "make"
で既に実行されているはずですが、一応試してみます。
# dpp.vimで管理してるプラグインがあるところ
cd $HOME/.cache/nvim/dpp/repos/github.com/yetone/avante.nvim
make
ビルド成果物もちゃんとあります。
ls ./build
.rwxr-xr-x@ 4.4M arrow2nd 6 2 21:20 avante_html2md.dylib
.rwxr-xr-x@ 24M arrow2nd 6 2 21:20 avante_repo_map.dylib
.rwxr-xr-x@ 2.2M arrow2nd 6 2 21:20 avante_templates.dylib
.rwxr-xr-x@ 14M arrow2nd 6 2 21:20 avante_tokenizers.dylib
この段階でNeovimを再起動しましたが、状況は変わらず同じエラーが出ました。
挙動を追ってく
なのでプラグインの動作を print デバッグで追っていきます 🦍
エラーに ...s/arrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/avante/path.lua:96: in function 'get'
とあったので、その辺りを見てみます。
---@return AvanteTemplates|nil
P._init_templates_lib = function()
if templates ~= nil then return templates end
local ok, module = pcall(require, "avante_templates")
-- この ok が false になってそうなので、module の中身をみてみる
print("init_templates_lib", ok, module)
---@cast module AvanteTemplates
---@cast ok boolean
if not ok then return nil end
templates = module
return templates
end
こうなりました。
init_templates_lib false module 'avante_templates' not found:
no field package.preload['avante_templates']
no file './avante_templates.lua'
no file '/Users/arrow2nd/workspace/github.com/neovim/neovim/.deps/usr/share/luajit-2.1/avante_templates.lua'
no file '/usr/local/share/lua/5.1/avante_templates.lua'
no file '/usr/local/share/lua/5.1/avante_templates/init.lua'
no file '/Users/arrow2nd/workspace/github.com/neovim/neovim/.deps/usr/share/lua/5.1/avante_templates.lua'
no file '/Users/arrow2nd/workspace/github.com/neovim/neovim/.deps/usr/share/lua/5.1/avante_templates/init.lua'
no file './avante_templates.so'
no file '/usr/local/lib/lua/5.1/avante_templates.so'
no file '/Users/arrow2nd/workspace/github.com/neovim/neovim/.deps/usr/lib/lua/5.1/avante_templates.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file '/Users/arrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/../build/avante_templates.dylib'
no file '/Users/arrow2nd/.cache/nvim/dpp/nvim/.dpp/lua/../build/avante_templates.dylib'
☝️ これが怪しそうなので、パスが通るようにシンボリックリンクを貼ってみます!
($HOME/.cache/nvim/dpp
の部分は多分 dpp.vim の設定次第で変わるかと思います)
ln -s $HOME/.cache/nvim/dpp/repos/github.com/yetone/avante.nvim/build $HOME/.cache/nvim/dpp/nvim/.dpp/build
これでNeovimを再起動したら動きました 🙌
うごいた~
(おまけ) システムプロンプトをカスタムする
READMEにシステムプロンプトのカスタムに関する記載がありますが、最新版では機能しなくなっているようです。
同じIssue内にあるコメント を参考に、メソッドを上書きすることで実現できました。
local Path = require("plenary.path")
local avante_path = require("avante.path")
-- これを任意の場所にすると良さそう
local cache_path = vim.fn.expand("~/.config/nvim/avanterules")
-- デフォルトプロンプトを上書きする
-- NOTE: @see https://github.com/yetone/avante.nvim/issues/874
avante_path.prompts.get = function()
local static_dir = Path:new(cache_path)
if not static_dir:exists() then
error("Static directory does not exist: " .. static_dir:absolute(), 2)
end
return static_dir:absolute()
end
avante.nvim/lua/avante/templates 以下にあるテンプレートを ~/.config/nvim/avavanterules/
にコピーして改変しています。
これで私はのじゃCopilotを実現しています 🦊
はかどるのぅ
Discussion