nvim-flutter / flutter-tools.nvim

Tools to help create flutter apps in neovim using the native lsp
MIT License
998 stars 80 forks source link

[BUG] Can't spawn Dart LSP #369

Closed jpgtzg closed 1 week ago

jpgtzg commented 1 month ago

Is there an existing issue for this?

Current Behavior

When trying to code using Dart and Flutter, I get the error:

...lar/neovim/0.10.1/share/nvim/runtime/lua/vim/lsp/rpc.lua:800: Spawning language server with cmd: `{ "./bin/dart", "language-server", "--protocol=lsp"
}` failed. The language server is either not installed, missing from PATH, or not executable.

Even though I have flutter sourced to path:

. "$HOME/.cargo/env"
export PATH=$HOME/development/flutter/bin:$PATH

Expected Behavior

Dart LSP should start without any issues, running whenever nvim starts in a flutter project

Steps To Reproduce

Using the following minimal config in nvim/lua/plugins/flutter.lua

1 return {
  2   { "stevearc/dressing.nvim" },
  3   {
  4     'akinsho/flutter-tools.nvim',
  5     lazy = false,
  6     dependencies = {
  7       'nvim-lua/plenary.nvim',
  8       'stevearc/dressing.nvim', 
  9     },
 10     config = function ()
 11      require("flutter-tools").setup {
 12           flutter_lookup_cmd = "dirname $(which flutter)",
 13       }
 14     end,
 15   }
 16 }

And the aforementioned path, Dart LSP does not start.

Running dirname $(which flutter)outputs: ~/development/flutter/bin

Environment

- OS: MacOS
- Flutter version: 3.24.0
- Is flutter in $PATH: YES
- neovim version: v0.10.1

Anything else?

No response

jpgtzg commented 1 month ago

I've looked around. Apparently, this issue arises when I add the line:

dirname $(which flutter)" in the setup function.

This arose a question. where should I call the require("flutter-tools").setup {} ? Because I currently having running on my lsp_config section inside of nvim/lua/plugins/lsp-config.lua:


  {
    "neovim/nvim-lspconfig",
    config = function()
      local capabilities = require('cmp_nvim_lsp').default_capabilities()

      local lspconfig = require("lspconfig")
      lspconfig.lua_ls.setup({
        capabilities = capabilities
      })
      lspconfig.rust_analyzer.setup {
        capabilities = capabilities,
        settings = {
          ['rust-analyzer'] = {},
        },
      }

      require("flutter-tools").setup {
        flutter_lookup_cmd = "dirname $(which flutter)"

      } -- use default_capabilities

      vim.keymap.set("n", "K", vim.lsp.buf.hover, {})
      vim.keymap.set("n", "<leader>gd", vim.lsp.buf.definition, {})
      vim.keymap.set("n", "<leader>gr", vim.lsp.buf.references, {})
      vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, {})
    end
  },

And my flutter_tools in the same foldeer under flutter.lua:


return {
  'akinsho/flutter-tools.nvim',
  lazy = false,
  dependencies = {
    'nvim-lua/plenary.nvim',
    'stevearc/dressing.nvim', -- optional for vim.ui.select
  },
  config = true,
}
jpgtzg commented 1 month ago

When I don't add the require statement, the projects load and everything runs without issues; DartLSP even gets loaded. The only issue is that all flutter related code and files don't get recognized so I get a bunch of errors

sidlatau commented 2 weeks ago

Could it be, that you setup the plugin twice? First setup with config = true, which uses default config and default path to flutter, and then:

require("flutter-tools").setup {
        flutter_lookup_cmd = "dirname $(which flutter)"

      } -- use default_capabilities

Maybe that is an issue?

jpgtzg commented 1 week ago

Could it be, that you setup the plugin twice? First setup with config = true, which uses default config and default path to flutter, and then:


require("flutter-tools").setup {

        flutter_lookup_cmd = "dirname $(which flutter)"

      } -- use default_capabilities

Maybe that is an issue?

So should I only declare it at the place where I set config = true?

sidlatau commented 1 week ago

Yes, the plugin should be set up only once.

jpgtzg commented 1 week ago

Okay, I now have the following setup:

nvim/lua/plugins/flutter-tools.lua:

return {
  'akinsho/flutter-tools.nvim',
  lazy = false,
  dependencies = {
    'nvim-lua/plenary.nvim',
    'stevearc/dressing.nvim', -- optional for vim.ui.select
  },
  config = function ()
    require("flutter-tools").setup {
      flutter_lookup_cmd = "/Users/jpgtzg/development/flutter/bin/flutter",
      lsp = {
        cmd = { "/Users/jpgtzg/development/flutter/bin/dart", "language-server", "--protocol=lsp" },
      },
      init_options = {
        onlyAnalyzeProjectsWithOpenFiles = true,
        suggestFromUnimportedLibraries = true,
        closingLabels = true,
      },
      ui = {
        -- the border type to use for all floating windows, the same options/formats
        -- used for ":h nvim_open_win" e.g. "single" | "shadow" | {<table-of-eight-chars>}
        border = "rounded",
        -- This determines whether notifications are show with `vim.notify` or with the plugin's custom UI
        -- please note that this option is eventually going to be deprecated and users will need to
        -- depend on plugins like `nvim-notify` instead.
      },
    }
  end
}

And nvim/lua/plugins/lsp-config.lua:

return {
  {
    "williamboman/mason.nvim",

    config = function()
      require("mason").setup()
    end
  },
 {
    "williamboman/mason-lspconfig.nvim",
    opts = {
      auto_install = true,
    },
    config = function()
      require("mason-lspconfig").setup({
        ensure_installed = {
          "lua_ls" ,
          "ast_grep",
        }
      })
    end
  },
  {
    "neovim/nvim-lspconfig",
    config = function()
      local capabilities = require('cmp_nvim_lsp').default_capabilities()

      local lspconfig = require("lspconfig")
      lspconfig.lua_ls.setup({
        capabilities = capabilities
      })
      lspconfig.rust_analyzer.setup {
        capabilities = capabilities,
        settings = {
          ['rust-analyzer'] = {},
        },
      }

      vim.keymap.set("n", "K", vim.lsp.buf.hover, {})
      vim.keymap.set("n", "<leader>gd", vim.lsp.buf.definition, {})
      vim.keymap.set("n", "<leader>gr", vim.lsp.buf.references, {})
      vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, {})
    end
  },
}

But when running :FlutterRun I get:

Error executing Lua callback: .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:108: Manage your Flutter app de
velopment./bin/flutter: Executable not found
stack traceback:
        .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:108: in function 'new'
        ...tter-tools.nvim/lua/flutter-tools/runners/job_runner.lua:27: in function 'run'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:194: in function 'get'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:177: in function 'run'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:202: in function 'select_project_config'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:202: in function 'run'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:101: in function 'run_command'
        ...share/nvim/lazy/flutter-tools.nvim/lua/flutter-tools.lua:28: in function <...share/nvim/lazy/flutter-tools.nvi
m/lua/flutter-tools.lua:28>
stack traceback:
        [C]: in function 'error'
        .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:108: in function 'new'
        ...tter-tools.nvim/lua/flutter-tools/runners/job_runner.lua:27: in function 'run'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:194: in function 'get'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:177: in function 'run'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:202: in function 'select_project_config'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:202: in function 'run'
        ...m/lazy/flutter-tools.nvim/lua/flutter-tools/commands.lua:101: in function 'run_command'
        ...share/nvim/lazy/flutter-tools.nvim/lua/flutter-tools.lua:28: in function <...share/nvim/lazy/flutter-tools.nvi
m/lua/flutter-tools.lua:28>
sidlatau commented 1 week ago
Error executing Lua callback: .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:108: Manage your Flutter app de
velopment./bin/flutter: Executable not found

The path to Flutter is incorrect based on the error. Please check the documentation, the function for flutter_lookup_cmd is expected, but I see a string in the configuration.

sidlatau commented 1 week ago

Looks like this is not a plugin problem.