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

bug: Incorrect file path when opening Avante chat in Python subproject with nested root

Open jonas0616 opened this issue 7 months ago • 3 comments

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
  1. Switch current working directory to python-project/mypkg/foo
  2. Open foo.py in Neovim
  3. Check LSP root (LspInfo) is set to mypkg/ instead of the Git root python-project/
  4. Trigger Avante chat with <leader>aa
  5. 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:

  1. the actual Git root, or
  2. 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


jonas0616 avatar May 18 '25 02:05 jonas0616

Same problem here. It seems the path recognition mechanism is a bit messed up with subproject under a mono-repo.

xarthurx avatar May 23 '25 07:05 xarthurx

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):

Image

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?

xarthurx avatar May 23 '25 07:05 xarthurx

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 avatar May 23 '25 18:05 hbergthaler

@hbergthaler what's the tl;dr on why that fixes it?

jeanlucthumm avatar Jun 01 '25 23:06 jeanlucthumm

@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.

xarthurx avatar Jun 02 '25 07:06 xarthurx

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

HadyMash avatar Jun 02 '25 18:06 HadyMash

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 cwd is 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 avatar Jun 15 '25 21:06 80avin

@80avin so this solution is no longer required?

lua vim.g.root_spec = { { ".git" }, "lsp", "cwd" } 

jeanlucthumm avatar Jun 17 '25 17:06 jeanlucthumm

@jeanlucthumm Right, it is not needed now.

80avin avatar Jun 17 '25 18:06 80avin