jose-elias-alvarez / typescript.nvim

A Lua plugin, written in TypeScript, to write TypeScript (Lua optional).
The Unlicense
497 stars 33 forks source link

BUG: Does not run `tsserver` #72

Open dsoltysiuk opened 1 year ago

dsoltysiuk commented 1 year ago

Hey @jose-elias-alvarez, thanks for a great plugin for Neovim!

I noticed when I use this, it does not launch the tsserver

require("typescript").setup({
    debug = false, -- enable debug logging for commands
    go_to_source_definition = {
        fallback = true, -- fall back to standard LSP definition on failure
    },
    -- server = { -- pass options to lspconfig's setup method
    --  on_attach = ...,
    -- },
})

However, when I do lspconfig.setup("tsserver", {}) right after this code, it launches perfectly fine

I looked into the source code, and it seems like you are doing just that https://github.com/jose-elias-alvarez/typescript.nvim/blob/f66d4472606cb24615dfb7dbc6557e779d177624/src/lsp.ts#L29 But for some reason, it does not work for me

jose-elias-alvarez commented 1 year ago

Does it work if you just use require("typescript").setup({})? Those additional options are just there as an example.

aswils commented 1 year ago

I am experiencing something very similar. If I open a file directly with neovim, $ nvim myfile.ts, tsserver does not start. However, this only occurs if it is the first buffer I open. If I start neovim with a separate file and then edit myfile.ts, tssserver starts as expected.

This behavior occurs with a simple require('typescript').setup({})

jose-elias-alvarez commented 1 year ago

If either of you (or anyone else experiencing this issue) can put together a minimal reproduction (meaning something like this) I can look into this more, otherwise there's not much I can do.

aswils commented 1 year ago

I have adapted the minimal script like so:

-- this template is borrowed from nvim-lspconfig
local on_windows = vim.loop.os_uname().version:match("Windows")

local function join_paths(...)
  local path_sep = on_windows and "\\" or "/"
  local result = table.concat({ ... }, path_sep)
  return result
end

vim.g.loaded_remote_plugins = ""
vim.cmd([[set runtimepath=$VIMRUNTIME]])

local temp_dir = vim.loop.os_getenv("TEMP") or "/tmp"

vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))

local package_root = join_paths(temp_dir, "nvim", "site", "pack")
local install_path = join_paths(package_root, "packer", "start", "packer.nvim")
local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")

local ts_config = function()
  local ts = require("typescript")
  -- add only what you need to reproduce your issue
  ts.setup({})
end

local function load_plugins()
  -- only add other plugins if they are necessary to reproduce the issue
  require("packer").startup({
    {
      "wbthomason/packer.nvim",
      {
        "jose-elias-alvarez/typescript.nvim",
        {
          "neovim/nvim-lspconfig"
        },
        config = ts_config,
      },
    },
    config = {
      package_root = package_root,
      compile_path = compile_path,
    },
  })
end

if vim.fn.isdirectory(install_path) == 0 then
  vim.fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
  load_plugins()
  require("packer").sync()
else
  load_plugins()
  require("packer").sync()
end

I then navigated to a very very minimal node project, including a package.json, a tsconfig.json and a typescript file.

I opened the file with $ nvim -u path/to/config.lua myFile.ts and can confirm that the tsserver lsp exists but does not attach.

dsoltysiuk commented 1 year ago

Thanks @aswils for bringing it further!

jose-elias-alvarez commented 1 year ago

I can reproduce your issue, but there's something weird about your minimal config, because I'm getting the same issue with nvim-lspconfig. This config works for me, both with this plugin and when using nvim-lspconfig directly:

local on_windows = vim.loop.os_uname().version:match("Windows")

local function join_paths(...)
    local path_sep = on_windows and "\\" or "/"
    local result = table.concat({ ... }, path_sep)
    return result
end

vim.g.loaded_remote_plugins = ""
vim.cmd([[set runtimepath=$VIMRUNTIME]])

local temp_dir = vim.loop.os_getenv("TEMP") or "/tmp"

vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))

local package_root = join_paths(temp_dir, "nvim", "site", "pack")
local install_path = join_paths(package_root, "packer", "start", "packer.nvim")
local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")

local ts_config = function()
    require("typescript").setup({}) -- works
    -- require("lspconfig").tsserver.setup({}) -- also works
end

local function load_plugins()
    require("packer").startup({
        {
            "wbthomason/packer.nvim",
            {
                "jose-elias-alvarez/typescript.nvim",
                requires = {
                    "neovim/nvim-lspconfig",
                },
                config = ts_config,
            },
        },
        config = {
            package_root = package_root,
            compile_path = compile_path,
        },
    })
end

if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
    load_plugins()
    require("packer").sync()
else
    load_plugins()
    require("packer").sync()
end
aswils commented 1 year ago

Aha yes, I accidentally left out the requires in the packer startup, good catch!

Anyways, with your minimal config I can't reproduce the issue. It must be something with how I have lspconfig setup. Thanks for looking into it.