bug: Incorrect file path when opening Avante chat in Python subproject with nested root
Describe the bug
When working on a Python project located in a subdirectory of a Git repository, the LSP server automatically sets the root directory to that subdirectory rather than the root of the Git repository. This seems to cause Avante to incorrectly resolve file paths when opening the chat.
When I open Avante chat using <leader>aa, it automatically adds the current buffer to the selected file list. However, the file path it uses appears to be incorrect, resulting in a No such file or directory error.
To reproduce
This issue can be reproduced with the following directory structure:
python-project/
└── mypkg
├── foo
│ └── foo.py
├── Makefile
└── pyproject.toml
3 directories, 3 files
- Switch current working directory to
python-project/mypkg/foo - Open
foo.pyin Neovim - Check LSP root (
LspInfo) is set tomypkg/instead of the Git rootpython-project/ - Trigger Avante chat with
<leader>aa - Observe the error:
error reading file: /python-project/foo/foo.py: No such file or directory
As a workaround, I have to manually remove the incorrectly selected file and add the correct one each time I open Avante chat, just to fix the path for that session.
Expected behavior
Avante should correctly resolve the file path relative to either:
- the actual Git root, or
- the buffer's absolute file path without incorrect prefixing.
Installation method
Use lazy.nvim:
{
"yetone/avante.nvim",
event = "VeryLazy",
lazy = false,
version = false, -- set this if you want to always pull the latest change
opts = {
provider = "copilot",
copilot = {
endpoint = "https://api.githubcopilot.com",
model = "claude-3.7-sonnet",
proxy = nil, -- [protocol://]host[:port] Use this proxy
allow_insecure = false, -- Allow insecure server connections
timeout = 30000, -- Timeout in milliseconds
temperature = 0,
max_tokens = 64000,
reasoning_effort = "high",
},
behaviour = {
jump_result_buffer_on_finish = true,
},
file_selector = {
provider = "snacks",
},
windows = {
width = 50,
ask = {
start_insert = true,
},
edit = {
border = "rounded",
},
},
system_prompt = function()
local hub = require("mcphub").get_hub_instance()
if hub then
return hub:get_active_servers_prompt()
else
return nil
end
end,
custom_tools = function()
return {
require("mcphub.extensions.avante").mcp_tool(),
}
end,
web_search_engine = {
provider = "google",
},
mappings = {
toggle = {
hint = "<leader>aH",
},
},
},
-- if you want to build from source then do `make BUILD_FROM_SOURCE=true`
build = "make",
-- build = "powershell -ExecutionPolicy Bypass -File Build.ps1 -BuildFromSource false" -- for windows
dependencies = {
"nvim-treesitter/nvim-treesitter",
"stevearc/dressing.nvim",
"nvim-lua/plenary.nvim",
"MunifTanjim/nui.nvim",
},
}
Environment
NVIM v0.11.1 Build type: Release LuaJIT 2.1.1741730670
Repro
Same problem here. It seems the path recognition mechanism is a bit messed up with subproject under a mono-repo.
Additionally, I have a python + nodejs mixed project (backend + frontend).
It seems avante is using the root directory of the lsp server (in my case, ts_ls):
While copilot can recognize the actual root of the whole project.
Is there some settings that can tell avante which root to use? For instance, the .git folder, or the one detected by lsp?
Hey, I had the same issue with Avante using the LSP root instead of the project root in my Python/Node.js project.
Fixed it by adding this before the Avante setup:
lua vim.g.root_spec = { { ".git" }, "lsp", "cwd" }
@hbergthaler what's the tl;dr on why that fixes it?
@hbergthaler what's the tl;dr on why that fixes it?
avante seems to use lsp's root when adding files to its system by pressing <leader>aa.
The above setting will tell it to use the folder where .git exist.
This will help when you have multiple projects in different language within the same git project.
Same issue opened before here: https://github.com/yetone/avante.nvim/issues/1195
My current workaround is using mini.pick as it correctly picks the root directory.
@hbergthaler what's the tl;dr on why that fixes it?
this should answer the question: https://github.com/yetone/avante.nvim/issues/1195#issuecomment-2683972365
Similar issue: https://github.com/yetone/avante.nvim/issues/2143
I debugged it and found that the issue happens because
- Avante tries to guess the project_root multiple times when adding a file. During first time, it finds that
cwdis not the root of added file, hence uses the current buffer ( which is the file being added ) to detect the project root. It then stores the file as path relative to the project root. - However, when it tries to detect the project root second time, the focus has changed to the chat buffer. As such, trying to guess the project root using active buffer doesn't give anything and it assumes cwd as the project root.
- With this assumption, it tries to join the relative path it stored with the new project root (cwd) and fails as the file doesn't exist.
I think the simple solution of storing absolute path of file will work. And project_root algorithm also has to be improvised. I feel it should try searching upwards from the cwd and not use the current buffer.
@80avin so this solution is no longer required?
lua vim.g.root_spec = { { ".git" }, "lsp", "cwd" }
@jeanlucthumm Right, it is not needed now.