akinsho / flutter-tools.nvim

Tools to help create flutter apps in neovim using the native lsp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lsp does not resolve package within dependency code

tlvenn opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Whenever I navigate to a file within a dependency of my project and that file actually import something from a package, the lsp cannot lookup the dependency.

Expected Behavior

I should be able to navigate dependency files without having any lsp error regarding resolving dependencies of the file.

Steps To Reproduce

  1. Open a file within a dependency of your project that imports using 'package:...'
  2. LSP cannot resolve the dependency

Environment

- OS:OSX 13.4.1
- Flutter version: 3.10.6
- Is flutter in $PATH: yes
- neovim version: 0.9.1

Anything else?

CleanShot 2023-07-27 at 11 23 21@2x

I am probably overlooking something obvious but I can't seem to figure out what it could be.
Thanks in advance for your help.

I've got the same issue. I've pretty much tried everything I can think of to fix it:

  • lsp.settings.analysisExcludedFolders = {}
  • Set lsp.root_dir to current file's directory

Like the OP, LSP works perfectly for files in my project, but I can't get LSP to work within dependencies.

This is very useful for exploring the implementation of a dependency.

Just a quick word on this. The issue here is not the same as the analysisExcludedFolders issue. Since you are seeing diagnostics then the lsp is not disabled for that file. As to why the LSP can't resolve the path of the dependencies I have no idea. I've never needed to do that and am not currently working with flutter so not quite in the space nor have the time to dig in. Contributions/investigations v. welcome. @sidlatau any change you happen to have hit this or know anything about it maybe (sorry to pick on you 🙏🏾)

Hm, for me LSP in the package works, I can find references, go to definition, etc. - tested on the same dio package:
image

So I suspect something in the local configuration causes the issue.

I see, thanks for the clarifications guys. @sidlatau If you don't mind could you post your flutter-tools config? And are you also on a Mac? Mine is basically the default except for some customization around the debugger. I also lazily load flutter-tools based on filetype:

  { "akinsho/flutter-tools.nvim", ft = "dart", cmd = { "FlutterDevices" }, ... }
  ...
  require'flutter-tools'.setup {
    debugger = {
      enabled = true,
      exception_breakpoints = "default",
      register_configurations = function(paths)
        local my_launchers =
          my_config.launchers and
          my_config.launchers.dart
        if my_launchers then
          local launchers = vim.deepcopy(my_launchers)
          for _, launcher in ipairs(launchers) do
            if not launcher.dart_sdk then
              launcher.dart_sdk = paths.dart_sdk
            end
            if not launcher.flutter_sdk then
              launcher.flutter_sdk = paths.flutter_sdk
            end
          end
          require'dap'.configurations.dart = launchers
        else
          require'dap'.configurations.dart = {
            {
              type = "dart",
              request = "launch",
              name = "Launch app",
              dartSdkPath = paths.dart_sdk,
              flutterSdkPath = paths.flutter_sdk,
              program = "${workspaceFolder}/lib/main.dart",
              cwd = "${workspaceFolder}",
            },
            {
              type = "dart",
              request = "attach",
              name = "Connect to running app",
              dartSdkPath = paths.dart_sdk,
              flutterSdkPath = paths.flutter_sdk,
              program = "${workspaceFolder}/lib/main.dart",
              cwd = "${workspaceFolder}",
            },
          }
        end
      end,
      run_via_dap = true,
    },
    decorations = {
      statusline = {
        app_version = true,
        device = true,
        project_config = true,
      },
    },
    dev_log = {
      enabled = false,
    },
    dev_tools = {
      auto_open_browser = true,
      autostart = true,
    },
    ui = {
      border = "single",
    },
  }

Edit:

I snuck a look at your dotfiles and added:

image

The errors disappeared, but gd doesn't work.

Hmm, one possible difference could be that I am using FVM for flutter version management - are you using it too?
Also, could you try the plugin without custom lazy loading? It already does lazy loading here:

require("flutter-tools.lsp").attach()
, maybe that could be an issue?

Thanks @sidlatau. I've just set up FVM, and removed flutter-tools lazy loading in the plugin manager, but still no go to definition in packages. It seems like putting pub-cache in analysisExcludedFolders completely disables LSP for packages, and since go to definition is an LSP feature, then it makes sense that it doesn't work unless I'm missing something.

Edit:

Oh I see. It works now in my new project. I was testing out the configs in another project that had the flutter project in a subdirectory of the git repo, which was probably what was messing up the LSP.

Thank you for the hints!

Yeah the project is currently expected to be at the root, there's an issue for tracking more complex monorepo like setups elsewhere.