[[📜2024-02-20 prettierdが特定ディレクトリの配下で実行したときだけCannot find module parser-babel.jsエラーになる問題の調査]]
## 事象
[[none-ls.nvim]]で[[prettierd]]を指定しても、`esbuild.config.mjs`でファイル保存したときに自動フォーマットがかからない。
- [[prettierd]]ではなく[[Prettier]]を設定すると動く
- `*.js`ファイルでも `*.ts`ファイルでも フォーマットがかからない
- 別のプロジェクト ([[🦉Various Complements]]) ではなぜか動く
- [[🦉Silhouette]]だと動かない
### バージョン
`esbuild.config.mjs`の中身は関係ない。
```console
$ prettierd --version
prettierd 0.25.2
$ prettier --version
zsh: command not found: prettier
$ npx prettierd --version
prettierd 0.25.2
$ npx prettier --version
3.2.5
```
### 設定
#### LspInfo
```
Language client log: /home/tadashi-aikawa/.local/state/nvim/lsp.log
Detected filetype: javascript
2 client(s) attached to this buffer:
Client: tsserver (id: 1, bufnr: [1])
filetypes: javascript, javascriptreact, javascript.jsx, typescript, typescriptreact, typescript.tsx
autostart: true
root directory: /home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette
cmd: /home/tadashi-aikawa/.local/share/mise/installs/node/20/bin/typescript-language-server --stdio
Client: null-ls (id: 2, bufnr: [1])
filetypes: lua, luau, python, go, sh, rust, css, markdown.mdx, json, yaml, javascript, javascriptreact, handlebars, vue, graphql, jsonc, html, markdown, typescript, svelte, less, scss, typescriptreact
autostart: false
root directory: /home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette
cmd: <function>
```
#### NullLsInfo
```
Logging
* current level: warn
* path: /home/tadashi-aikawa/.cache/nvim/null-ls.log
Active source(s)
* name: prettierd
* filetypes: css | markdown.mdx | json | yaml | javascript | javascriptreact | handlebars | vue | graphql | jsonc | html | markdown | typescript | svelte | less | scss | typescriptreact
* methods: formatting | range_formatting
```
#### null-ls.nvim
```lua
{
"nvimtools/none-ls.nvim",
config = function()
local null_ls = require("null-ls")
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
null_ls.setup({
sources = {
null_ls.builtins.formatting.biome.with({
only_local = "node_modules/.bin",
condition = function(utils)
return utils.root_has_file({ "biome.json" })
end,
}),
null_ls.builtins.formatting.prettierd.with({
prefer_local = "node_modules/.bin",
extra_filetypes = { "svelte" },
disabled_filetypes = { "markdown" },
condition = function(utils)
return not utils.root_has_file({ "biome.json" })
end,
}),
null_ls.builtins.formatting.stylua,
null_ls.builtins.formatting.ruff,
null_ls.builtins.formatting.gofumpt,
null_ls.builtins.formatting.goimports,
null_ls.builtins.code_actions.shellcheck,
null_ls.builtins.formatting.shfmt,
null_ls.builtins.formatting.rustfmt,
},
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format({
async = false,
filter = function(c)
return c.name == "null-ls"
end,
})
end,
})
end
end,
})
end,
}
```
### デバッグログ
[[none-ls.nvim]]で[[DEBUG・TRACEログを出力 (none-ls.nvim)|DEBUG・TRACEログを出力]]してみる。
```console
[TRACE Tue Feb 20 22:21:33 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:106: received LSP request for method shutdown
[TRACE Tue Feb 20 22:21:33 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method exit
[DEBUG Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/sources.lua:85: not registering conditional source biome
[DEBUG Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/sources.lua:82: registering conditional source prettierd
[TRACE Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/client.lua:114: starting null-ls client
[TRACE Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:106: received LSP request for method initialize
[DEBUG Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/client.lua:182: unable to notify client for method textDocument/didOpen (client not active): {
textDocument = {
uri = "file:///home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette/esbuild.config.mjs"
}
}
[TRACE Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method initialized
[TRACE Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method textDocument/didOpen
[TRACE Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_OPEN
[DEBUG Tue Feb 20 22:21:35 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/generators.lua:24: no generators available
[TRACE Tue Feb 20 22:21:36 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method textDocument/didChange
[TRACE Tue Feb 20 22:21:36 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS
[DEBUG Tue Feb 20 22:21:36 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/generators.lua:24: no generators available
[TRACE Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:106: received LSP request for method textDocument/formatting
[TRACE Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_FORMATTING
[DEBUG Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:329: spawning command "prettierd" at /home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette with args { "/home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette/esbuild.config.mjs" }
[TRACE Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:207: error output: Error: Cannot find module './parser-babel.js'
require stack:
- /home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette/node_modules/prettier/index.js
- /home/tadashi-aikawa/.local/share/mise/installs/node/20.11.0/lib/node_modules/@fsouza/prettierd/dist/service.js
- /home/tadashi-aikawa/.local/share/mise/installs/node/20.11.0/lib/node_modules/@fsouza/prettierd/node_modules/core_d/lib/server.js
- /home/tadashi-aikawa/.local/share/mise/installs/node/20.11.0/lib/node_modules/@fsouza/prettierd/node_modules/core_d/lib/daemon.js
[TRACE Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:208: output: nil
[TRACE Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:218: ignoring stderr due to generator options
[TRACE Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method textDocument/didSave
[TRACE Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_SAVE
[DEBUG Tue Feb 20 22:21:37 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/generators.lua:24: no generators available
```
`node_modules/prettier/index.js`は存在しなかったが `index.cjs` や `index.mjs` はある。
また `package.json` の `exports` に以下の記述が...
```json
"exports": {
"./parser-babel.js": "./plugins/babel.js",
}
```
つまり `./parser-babel.js` はエントリポイントとして利用でき `./plugins/babel.js` を参照する。`./plugins/babel.js` もちゃんと存在するが、そもそも `./parser-babel.js` を解決できていない。
```
[TRACE Tue Feb 20 22:35:03 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/loop.lua:155: 🐈
[TRACE Tue Feb 20 22:35:03 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/loop.lua:156: /home/tadashi-aikawa/.local/share/mise/installs/node/20/bin/prettierd
[TRACE Tue Feb 20 22:35:03 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/loop.lua:157: {
args = { "/home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette/esbuild.config.mjs" },
cwd = "/home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette",
stdio = { <userdata 1>, <userdata 2>, <userdata 3> }
}
[TRACE Tue Feb 20 22:35:03 2024] /home/tadashi-aikawa/.local/share/nvim/lazy/none-ls.nvim/lua/null-ls/loop.lua:158: 🐈
```
`~/git/github.com/tadashi-aikawa/silhouette` で以下が再現した。
```console
$ cat /home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette/esbuild.config.mjs | /home/tadashi-aikawa/.local/share/mise/installs/node/20/bin/prettierd /home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette/esbuild.config.mjs
Error: Cannot find module './parser-babel.js'
Require stack:
- /home/tadashi-aikawa/git/github.com/tadashi-aikawa/silhouette/node_modules/prettier/index.js
- /home/tadashi-aikawa/.local/share/mise/installs/node/20.11.0/lib/node_modules/@fsouza/prettierd/dist/service.js
- /home/tadashi-aikawa/.local/share/mise/installs/node/20.11.0/lib/node_modules/@fsouza/prettierd/node_modules/core_d/lib/server.js
- /home/tadashi-aikawa/.local/share/mise/installs/node/20.11.0/lib/node_modules/@fsouza/prettierd/node_modules/core_d/lib/daemon.js
```
なお `~/git/github.com/tadashi-aikawa/obsidian-various-complements-plugin` 配下で全く同じコマンドを実行すると成功する。
## 原因
該当パスで起動した`prettierd`が意図せず動いたままになっており、不整合が生じていたから。
<div class="link-card">
<div class="link-card-header">
<img src="https://publish-01.obsidian.md/access/35d05cd1bf5cc500e11cc8ba57daaf88/favicon-64.png" class="link-card-site-icon"/>
<span class="link-card-site-name">minerva.mamansoft.net</span>
</div>
<div class="link-card-body">
<div class="link-card-content">
<p class="link-card-title">📜2024-02-20 prettierdが特定ディレクトリの配下で実行したときだけCannot find module parser-babel.jsエラーになる問題の調査</p>
</div>
<img src="https://publish-01.obsidian.md/access/35d05cd1bf5cc500e11cc8ba57daaf88/Notes/attachments/minerva-image.webp" class="link-card-image" />
</div>
<a class="internal-link" data-href="Notes/📜2024-02-20 prettierdが特定ディレクトリの配下で実行したときだけCannot find module parser-babel.jsエラーになる問題の調査.md"></a>
</div>
## 解決方法
`prettierd`プロセスをkillする。
```console
pkill prettierd
```
> [!info]
> できれば無駄なデーモンの起動を止めたい。以下のIssueで議論や回避策が検討されている。
>
> - [The process doesn't terminate · Issue \#645 · fsouza/prettierd](https://github.com/fsouza/prettierd/issues/645)