0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

NevoVim の ts_ls を pnpm workspace で使う

0
Posted at

nvim-lspconfig では多くの language server に対する標準設定が提供されています。この中に TypeScript の language server (ts_ls) の定義がありますが、標準の設定では pnmp workspace では上手く動かなかったため修正方法を考えました。

この記事の例では nvim-lspconfig と NeoVim 0.11 以降の NeoVim 標準 LSP を利用しています

pnpm workspace を使った構成でパッケージ内のファイルを開くと以下のエラーにより language server の起動に失敗します。

Request initialize failed with message: Could not find a valid TypeScript installation. Please ensure that the "typescript" dependency is installed in the workspace or that a valid tsserver.path is specified. Exiting.

TypeScript の依存関係をインストールしているにも関わらず見つからないというエラーです。これは language server がワークスペースの node_modules 内で typescript パッケージを探すためです。pnpm はパッケージではなくワークスペースディレクトリに node_modules を作成するため、typescript を発見できないという問題が発生します (Node.js のモジュール解決では先祖ディレクトリに遡って node_modules 内を探します)。

そのため ad-hoc には以下のようにして直近の node_modules を使用させるようにすることができます。

ts_ls.lua
---@type vim.lsp.Config
return {
    root_markers = { "package.json" },
    workspace_required = true,
    init_options = {
      tsserver = {
        path = vim.fs.joinpath(vim.fs.root(0, { "node_modules" }), "node_modules", "typescript", "lib")
      },
    },
}

完全な解決策は以下を参照してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?