From 9c6bbb5d1125491c7c7547e15188ae3e42647143 Mon Sep 17 00:00:00 2001 From: Alexis Tacnet Date: Mon, 28 Apr 2025 14:11:00 +0200 Subject: fix: improve typescript server path search for ts-dependent lsp #3795 --- lua/lspconfig/configs/astro.lua | 7 +------ lua/lspconfig/configs/mdx_analyzer.lua | 7 +------ lua/lspconfig/configs/volar.lua | 7 +------ lua/lspconfig/util.lua | 12 ++++++++++++ 4 files changed, 15 insertions(+), 18 deletions(-) (limited to 'lua') diff --git a/lua/lspconfig/configs/astro.lua b/lua/lspconfig/configs/astro.lua index 106d247f..b979d711 100644 --- a/lua/lspconfig/configs/astro.lua +++ b/lua/lspconfig/configs/astro.lua @@ -1,10 +1,5 @@ local util = require 'lspconfig.util' -local function get_typescript_server_path(root_dir) - local project_root = vim.fs.dirname(vim.fs.find('node_modules', { path = root_dir, upward = true })[1]) - return project_root and (project_root .. '/node_modules/typescript/lib') or '' -end - return { default_config = { cmd = { 'astro-ls', '--stdio' }, @@ -15,7 +10,7 @@ return { }, on_new_config = function(new_config, new_root_dir) if vim.tbl_get(new_config.init_options, 'typescript') and not new_config.init_options.typescript.tsdk then - new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir) + new_config.init_options.typescript.tsdk = util.get_typescript_server_path(new_root_dir) end end, }, diff --git a/lua/lspconfig/configs/mdx_analyzer.lua b/lua/lspconfig/configs/mdx_analyzer.lua index 6ae65a2b..7c68cc81 100644 --- a/lua/lspconfig/configs/mdx_analyzer.lua +++ b/lua/lspconfig/configs/mdx_analyzer.lua @@ -1,10 +1,5 @@ local util = require 'lspconfig.util' -local function get_typescript_server_path(root_dir) - local project_root = vim.fs.dirname(vim.fs.find('node_modules', { path = root_dir, upward = true })[1]) - return project_root and (project_root .. '/node_modules/typescript/lib') or '' -end - return { default_config = { cmd = { 'mdx-language-server', '--stdio' }, @@ -17,7 +12,7 @@ return { }, on_new_config = function(new_config, new_root_dir) if vim.tbl_get(new_config.init_options, 'typescript') and not new_config.init_options.typescript.tsdk then - new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir) + new_config.init_options.typescript.tsdk = util.get_typescript_server_path(new_root_dir) end end, }, diff --git a/lua/lspconfig/configs/volar.lua b/lua/lspconfig/configs/volar.lua index ff52aed5..19070090 100644 --- a/lua/lspconfig/configs/volar.lua +++ b/lua/lspconfig/configs/volar.lua @@ -1,10 +1,5 @@ local util = require 'lspconfig.util' -local function get_typescript_server_path(root_dir) - local project_root = vim.fs.dirname(vim.fs.find('node_modules', { path = root_dir, upward = true })[1]) - return project_root and (project_root .. '/node_modules/typescript/lib') or '' -end - -- https://github.com/vuejs/language-tools/blob/master/packages/language-server/lib/types.ts local volar_init_options = { typescript = { @@ -24,7 +19,7 @@ return { and new_config.init_options.typescript and new_config.init_options.typescript.tsdk == '' then - new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir) + new_config.init_options.typescript.tsdk = util.get_typescript_server_path(new_root_dir) end end, }, diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index a8cf7439..2807edf3 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -69,6 +69,18 @@ function M.strip_archive_subpath(path) return path end +function M.get_typescript_server_path(root_dir) + local project_roots = vim.fs.find('node_modules', { path = root_dir, upward = true, limit = math.huge }) + for _, project_root in ipairs(project_roots) do + local typescript_path = project_root .. '/typescript' + local stat = vim.loop.fs_stat(typescript_path) + if stat and stat.type == 'directory' then + return typescript_path .. '/lib' + end + end + return '' +end + --- --- --- -- cgit v1.2.3-70-g09d2