Skip to content

[BUG]: Setting root via LSP seems not to work #40

@pitkling

Description

@pitkling

Issue Checks

  • I am running on the latest version of project.nvim
  • I have checked for existing issues related to this one, whether open or closed
  • I have read the pertinent documentation (README.md, :h project-nvim, etc.)

What does the problem you encountered relate to?

  • The plugin is not detecting directories correctly
  • The plugin is not changing directories
  • Logging issues
  • UI errors
  • Another plugin interferes with project.nvim
  • Project history broke
  • The Telescope picker is having issues
  • fzf-lua integration issues
  • Windows is causing undesirable behaviour
  • Documentation needs a revision
  • Other

What neovim version are you using? (nvim -v)

v0.11.5

Operating system and, if possible, what version?

macOS Tahoe 26.2

Output Of :ProjectConfig!

{
  allow_different_owners = false,
  allow_patterns_for_lsp = false,
  before_attach = <function 1>,
  datapath = "/Users/peter/.local/share/nvim",
  disable_on = {
    bt = { "help", "nofile", "nowrite", "terminal" },
    ft = { "", "NvimTree", "TelescopePrompt", "TelescopeResults", "alpha", "checkhealth", "lazy", "log", "ministarter", "neo-tree", "notify", "nvim-pack", "packer", "qf" }
  },
  enable_autochdir = false,
  exclude_dirs = {},
  expand_excluded = <function 2>,
  fzf_lua = {
    enabled = false
  },
  historysize = 100,
  ignore_lsp = {},
  log = {
    enabled = false,
    logpath = "/Users/peter/.local/state/nvim",
    max_size = 1.1
  },
  manual_mode = false,
  on_attach = <function 3>,
  patterns = { ".git", ".github", "_darcs", ".hg", ".bzr", ".svn", "Pipfile", "pyproject.toml", ".pre-commit-config.yaml", ".pre-commit-config.yml", ".csproj", ".sln", ".nvim.lua" },
  scope_chdir = "global",
  show_hidden = false,
  silent_chdir = true,
  telescope = {
    disable_file_picker = false,
    mappings = {
      i = {
        ["<C-b>"] = "browse_project_files",
        ["<C-d>"] = "delete_project",
        ["<C-f>"] = "find_project_files",
        ["<C-r>"] = "recent_project_files",
        ["<C-s>"] = "search_in_project_files",
        ["<C-w>"] = "change_working_directory"
      },
      n = {
        b = "browse_project_files",
        d = "delete_project",
        f = "find_project_files",
        r = "recent_project_files",
        s = "search_in_project_files",
        w = "change_working_directory"
      }
    },
    prefer_file_browser = false,
    sort = "newest"
  },
  use_lsp = true
}

Output of :checkhealth project

==============================================================================
project: 1 ⚠️

Setup ~

  • ✅ OK setup() has been called!
  • ✅ OK nvim version is at least v0.11 (v0.11.5)
  • ✅ OK fd executable in PATH

Current Project ~

  • Current project: /Users/peter/Downloads/test-git-nvim/project
    My Method used: "texlab" lsp
    Last project: /Users/peter/Downloads/test-git-nvim

Detection Methods ~

Active Sessions ~

    1. /Users/peter/Downloads/test-git-nvim/project
    1. /Users/peter/Downloads/test-git-nvim

History ~

  • ✅ OK datapath: /Users/peter/.local/share/nvim
  • ✅ OK projectpath: /Users/peter/.local/share/nvim/project_nvim
  • ✅ OK historyfile: /Users/peter/.local/share/nvim/project_nvim/project_history

Fzf-Lua ~

  • ⚠️ WARNING fzf-lua integration is disabled.

    This doesn't represent an issue necessarily!

Config ~

  • ✅ OK - show_hidden: false
  • ✅ OK - scope_chdir: "global"
  • ✅ OK - log:
    enabled: false
    max_size: 1.1
    logpath: "/Users/peter/.local/state/nvim"
  • ✅ OK - historysize: 100
  • ✅ OK - disable_on:
    bt:
    [1]: "help"
    [2]: "nofile"
    [3]: "nowrite"
    [4]: "terminal"
    ft:
    [1]: ""
    [2]: "NvimTree"
    [3]: "TelescopePrompt"
    [4]: "TelescopeResults"
    [5]: "alpha"
    [6]: "checkhealth"
    [7]: "lazy"
    [8]: "log"
    [9]: "ministarter"
    [10]: "neo-tree"
    [11]: "notify"
    [12]: "nvim-pack"
    [13]: "packer"
    [14]: "qf"
  • ✅ OK - manual_mode: false
  • ✅ OK - use_lsp: true
  • ✅ OK - patterns:
    [1]: ".git"
    [2]: ".github"
    [3]: "_darcs"
    [4]: ".hg"
    [5]: ".bzr"
    [6]: ".svn"
    [7]: "Pipfile"
    [8]: "pyproject.toml"
    [9]: ".pre-commit-config.yaml"
    [10]: ".pre-commit-config.yml"
    [11]: ".csproj"
    [12]: ".sln"
    [13]: ".nvim.lua"
  • ✅ OK - allow_different_owners: false
  • ✅ OK - silent_chdir: true
  • ✅ OK - expand_excluded: function
  • ✅ OK - exclude_dirs: {}
  • ✅ OK - verify: function
  • ✅ OK - ignore_lsp: {}
  • ✅ OK - enable_autochdir: false
  • ✅ OK - allow_patterns_for_lsp: false
  • ✅ OK - datapath: "/Users/peter/.local/share/nvim"
  • ✅ OK - on_attach: function
  • ✅ OK - before_attach: function

Recent Projects ~

  • 1. /Users/peter/Downloads/test-git-nvim
  • 2. … (removed some more entries)

Describe the bug

I have a git repository with multiple subdirectories, where some subdirectories contain a LaTeX project. I use a LaTeX language server for root detection, which works fite (checked separately via :LSPInfo).

When loading such a project from a subdirectory, project.nvim sets the CWD to the git repository instead of the subdirectory. Note that I have set use_lsp = true, which I understood should ensure that the LSP takes precedence.

Steps To Reproduce

  1. Have a git repository with a subdirectory containing a LaTeX project.
  2. Have a LaTeX LSP configured that recognizes the subdirectory as the LaTeX project root.
  3. Configure project.nvim with use_lsp = true.
  4. Load a file from the LaTeX project.

(Any other LSP based project with LSP root detection should work too.)

Expected Behavior

The CWD should be set to the subdirectory. Instead, it is set to the git repository.

How did you setup the plugin?

Additional Info and/or Screenshots

I did some debugging myself and looked at the method Api.get_project_root in lua/project/api.lua. The roots variable contains

{ { "/Users/peter/Downloads/test-git-nvim/project", '"texlab" lsp', "lsp" }, { "/Users/peter/Downloads/test-git-nvim", "pattern .git", "pattern" } }

after the for loop in lines 302 to 309. This seems correct.

I then took a look at the decision logic in the following lines and got confused. Maybe I'm misunderstanding something, but that logic seems to always prefer the root found via the traditional pattern matching. In particular due to the final for loop in lines 321 to 325.

I replaced that whole logic for a simple

  if vim.tbl_isempty(roots) then
    return
  else
    return roots[1][1], roots[1][2]
  end

which works for me (not tested thoroughly). From my (very quick) glances at the rest of the code, I got the impression that roots is filled such that the first entry should always be the one preferred. If use_lsp is set, then it will be the LSP entry, otherwise the traditional pattern matching entry.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixedThe Issue has been addressed

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions