astrocommunity icon indicating copy to clipboard operation
astrocommunity copied to clipboard

MDX Error: Can't find typescript.js or tsserverlibrary.js

Open nickali opened this issue 1 year ago • 9 comments

Checklist

  • [X] I have searched through the AstroNvim documentation
  • [X] I have searched through the existing issues of this project
  • [X] I have searched the existing issues of plugins related to this issue
  • [X] I can replicate the bug with the minimal repro.lua provided below

Neovim version (nvim -v)

v0.10.1

Operating system/version

macOS 14.5

Terminal/GUI

Alacritty

Describe the bug

I get the following error at the bottom whenever I open a MDX file:

Error executing vim.schedule lua callback: .../neovim/0.10.1/share/nvim/runtime/lua/vim/lsp/client.lua:588: RPC[Error] code_name = InternalError, message = "Request initialize failed with message: Can't find typescript.js or tsserverlibrary.js in \"\""
stack traceback:
        [C]: in function 'assert'
        .../neovim/0.10.1/share/nvim/runtime/lua/vim/lsp/client.lua:588: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

If I escape out of it, everything seems to work fine.

If I do :Mason, I see marksman and mdx-analyzer installed.

Steps to Reproduce

Opening any file with the MDX extension, even if it is empty.

Expected behavior

No error displayed.

Screenshots

No response

Additional Context

No response

Minimal configuration

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
	vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	-- stylua: ignore
	vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable",
		lazypath })
end
vim.opt.rtp:prepend(vim.env.LAZY or lazypath)

-- install plugins
local plugins = {
	{ "AstroNvim/AstroNvim",      import = "astronvim.plugins" },
	{ "AstroNvim/astrocommunity", import = "astrocommunity.pack.mdx" },
	-- add any other plugins/customizations here
}
require("lazy").setup(plugins, {
	root = root .. "/plugins",
})

-- add anything else here (autocommands, vim.filetype, etc.)

nickali avatar Aug 06 '24 20:08 nickali

@nickali Please provide a repro.lua

Uzaaft avatar Aug 07 '24 11:08 Uzaaft

@Uzaaft, I updated the minimal configuration above.

nickali avatar Aug 07 '24 19:08 nickali

Appreciate it :)

Uzaaft avatar Aug 07 '24 19:08 Uzaaft

The same issue with 'tsserver' in Vue files if project doesn't exist TS


Error executing vim.schedule lua callback: .../neovim/0.10.1/share/nvim/runtime/lua/vim/lsp/client.lua:588: RPC[Error] code_name = InternalError, message = "Request initialize failed with message: Can't find typescript.js or tsserverlibrary.js in \"/Users/qmpwwsd/Desktop/smartgroups/smartchat-ui/node_modules/typescript/lib\""
stack traceback:
	[C]: in function 'assert'
	.../neovim/0.10.1/share/nvim/runtime/lua/vim/lsp/client.lua:588: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

@nickali did you find how fix it?

art1es23 avatar Sep 01 '24 20:09 art1es23

@nickali did you find how fix it?

No.

nickali avatar Sep 02 '24 18:09 nickali

Same issue here, every time I open an MDX file I get that error. I noticed the error message says:

...Can't find typescript.js or tsserverlibrary.js in \"......(my project root folder) /node_modules/node_modules/typescript/lib\"

Which appears to be one too many node_modules directories. But I can't figure out which LSP client/plugin/config where this path is being set...

mnchaser avatar Dec 22 '24 15:12 mnchaser

Seems related to https://github.com/williamboman/mason-lspconfig.nvim/issues/351 upstream issue?

ALameLlama avatar Jan 06 '25 21:01 ALameLlama

Seems related to williamboman/mason-lspconfig.nvim#351 upstream issue?

What a legend. Could be.

Uzaaft avatar Jan 07 '25 09:01 Uzaaft

Related to too many node_modules directories.

For my monorepo project

root
  | - pkgA
  | | - node_modules/ # [Empty Directory]
  | | - file.mdx
  | - pkgB
  | | - node_modules/ # [Empty Directory]
  | - node_modules/ # [All pkg goes here]

when file.mdx setting up lsp, inspect mdx_analyzer's on_new_config func, the input attr new_root_dir is root/pkgA and the return value is same. modify func get_typescript_server_path resolved the issue as it only detects node_modules directory exists or not. https://github.com/neovim/nvim-lspconfig/blob/9e932edb0af4e20880685ddb96a231669fbe8091/lua/lspconfig/configs/mdx_analyzer.lua#L3

Edit config to

local function get_typescript_server_path(root_dir)
  local project_root = vim.fs.dirname(vim.fs.find("node_modules/typescript/lib", { path = root_dir, upward = true })[1])
  return project_root and (project_root .. "/lib") or ""
end

require("lspconfig").mdx_analyzer.setup({
  on_attach = on_attach,
  capabilities = capabilities,
  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)
    end
  end,
})

VidocqH avatar Feb 24 '25 01:02 VidocqH