stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.25k stars 166 forks source link

Prettier formatting started failing after 8.2.0 (includes sorting imports with @trivago/prettier-plugin-sort-imports) #575

Closed torbjornvatn closed 1 week ago

torbjornvatn commented 1 week ago

Neovim version (nvim -v)

NVIM v0.10.2

Operating system/version

MacOs 14.6.1

Read debugging tips

Add the debug logs

Log file

2024-11-13 00:29:10[ERROR] Formatter 'prettier' error: [error] /Users/torbjorn/Development/carrot/dwh/dwh-importer/src/app.ts: Error: Cannot find package '@trivago/prettier-plugin-sort-imports' imported from /Users/torbjorn/Development/carrot/dwh/noop.js [error] at __nodeinternal (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:16460:11) [error] at new NodeError (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:16425:5) [error] at packageResolve (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:17366:9) [error] at moduleResolve (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:17406:18) [error] at defaultResolve (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:17497:16) [error] at resolve2 (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:17514:12) [error] at importFromFile (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:17529:16) [error] at importFromDirectory (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:22444:10) [error] at importPlugin (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:22456:12) [error] at async loadPluginWithoutCache (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:22460:18) [error] at async Promise.all (index 0) [error] at async Promise.all (index 1) [error] at async file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/index.mjs:22722:17 [error] at async getContextOptions (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/internal/cli.mjs:1677:23) [error] at async Context.pushContextPlugins (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/internal/cli.mjs:2566:21) [error] at async getOptionsForFile (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/internal/cli.mjs:3159:5) [error] at async formatStdin (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/internal/cli.mjs:3375:21) [error] at async main (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/internal/cli.mjs:3872:5) [error] at async Module.run (file:///Users/torbjorn/Development/carrot/dwh/dwh-importer/node_modules/prettier/internal/cli.mjs:3822:5)

Describe the bug

After conform.nvim got updated by lazy.nvim to the newest version (8.2.0) formatting using prettier has stopped working. It complains that it can't find the @trivago/prettier-plugin-sort-imports plugin anymore.

Maybe the fix in the upcoming 8.2.1 will fix this, but I'm not convinced it will by looking at the changes.

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

  1. Install https://github.com/trivago/prettier-plugin-sort-imports with npm install --save-dev @trivago/prettier-plugin-sort-imports
  2. Add these lines to .prettierrc
    "plugins": ["@trivago/prettier-plugin-sort-imports"],
    "importOrder": ["^[./]"],
  3. Try to format using prettier through conform

Expected Behavior

It worked just fine before this update

Minimal example file

No response

Minimal init.lua

No response

Additional context

No response

stevearc commented 1 week ago

Can you include enough of the logs for me to see both the command being executed and the CWD it is running in? And can you confirm that running the command directly in the CLI works as intended? Refer to the debugging tips for details.

torbjornvatn commented 1 week ago

Here's the whole log after setting log level debug:

2024-11-13 00:59:06[DEBUG] Running formatters on /Users/torbjorn/Development/carrot/dwh/.prettierrc: { "prettier" }
2024-11-13 00:59:06[INFO] Run prettier on /Users/torbjorn/Development/carrot/dwh/.prettierrc
2024-11-13 00:59:06[DEBUG] Run command: { "/Users/torbjorn/.local/share/nvim/mason/bin/prettier", "--stdin-filepath", "/Users/torbjorn/Development/carrot/dwh/.prettierrc" }
2024-11-13 00:59:06[DEBUG] Run CWD: /Users/torbjorn/Development/carrot/dwh
2024-11-13 00:59:06[DEBUG] prettier exited with code 0
2024-11-13 00:59:10[DEBUG] Running formatters on /Users/torbjorn/Development/carrot/dwh/.prettierrc: { "prettier" }
2024-11-13 00:59:10[INFO] Run prettier on /Users/torbjorn/Development/carrot/dwh/.prettierrc
2024-11-13 00:59:10[DEBUG] Run command: { "/Users/torbjorn/.local/share/nvim/mason/bin/prettier", "--stdin-filepath", "/Users/torbjorn/Development/carrot/dwh/.prettierrc" }
2024-11-13 00:59:10[DEBUG] Run CWD: /Users/torbjorn/Development/carrot/dwh
2024-11-13 00:59:10[INFO] prettier exited with code 2
2024-11-13 00:59:10[DEBUG] prettier stdout: { "" }
2024-11-13 00:59:10[DEBUG] prettier stderr: { "[error] /Users/torbjorn/Development/carrot/dwh/.prettierrc: Error: Cannot find package '@trivago/prettier-plugin-sort-imports' imported from /Users/torbjorn/Development/carrot/dwh/noop.js", "[error]     at __node_internal_ (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:16460:11)", "[error]     at new NodeError (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:16425:5)", "[error]     at packageResolve (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17366:9)", "[error]     at moduleResolve (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17406:18)", "[error]     at defaultResolve (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17497:16)", "[error]     at resolve2 (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17514:12)", "[error]     at importFromFile (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17529:16)", "[error]     at importFromDirectory (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22444:10)", "[error]     at importPlugin (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22456:12)", "[error]     at async loadPluginWithoutCache (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22460:18)", "[error]     at async Promise.all (index 0)", "[error]     at async Promise.all (index 1)", "[error]     at async file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22722:17", "[error]     at async getContextOptions (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:1677:23)", "[error]     at async Context.pushContextPlugins (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:2566:21)", "[error]     at async getOptionsForFile (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3159:5)", "[error]     at async formatStdin (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3375:21)", "[error]     at async main (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3872:5)", "[error]     at async Module.run (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3822:5)", "" }
2024-11-13 00:59:10[ERROR] Formatter 'prettier' error: [error] /Users/torbjorn/Development/carrot/dwh/.prettierrc: Error: Cannot find package '@trivago/prettier-plugin-sort-imports' imported from /Users/torbjorn/Development/carrot/dwh/noop.js
[error]     at __node_internal_ (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:16460:11)
[error]     at new NodeError (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:16425:5)
[error]     at packageResolve (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17366:9)
[error]     at moduleResolve (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17406:18)
[error]     at defaultResolve (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17497:16)
[error]     at resolve2 (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17514:12)
[error]     at importFromFile (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:17529:16)
[error]     at importFromDirectory (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22444:10)
[error]     at importPlugin (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22456:12)
[error]     at async loadPluginWithoutCache (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22460:18)
[error]     at async Promise.all (index 0)
[error]     at async Promise.all (index 1)
[error]     at async file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/index.mjs:22722:17
[error]     at async getContextOptions (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:1677:23)
[error]     at async Context.pushContextPlugins (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:2566:21)
[error]     at async getOptionsForFile (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3159:5)
[error]     at async formatStdin (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3375:21)
[error]     at async main (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3872:5)
[error]     at async Module.run (file:///Users/torbjorn/.local/share/nvim/mason/packages/prettier/node_modules/prettier/internal/cli.mjs:3822:5)
torbjornvatn commented 1 week ago

Formatting with the CLI works just fine.

I should also mention that the repo is kind of a monorepo, with subfolders containing separate package.json files, but the .prettierrc file is located at the root

stevearc commented 1 week ago

So this command works for you?

cd /Users/torbjorn/Development/carrot/dwh && cat /Users/torbjorn/Development/carrot/dwh/.prettierrc | /Users/torbjorn/.local/share/nvim/mason/bin/prettier --stdin-filepath /Users/torbjorn/Development/carrot/dwh/.prettierrc

This sequence of events is very puzzling to me

2024-11-13 00:59:06[DEBUG] Running formatters on /Users/torbjorn/Development/carrot/dwh/.prettierrc: { "prettier" }
2024-11-13 00:59:06[INFO] Run prettier on /Users/torbjorn/Development/carrot/dwh/.prettierrc
2024-11-13 00:59:06[DEBUG] Run command: { "/Users/torbjorn/.local/share/nvim/mason/bin/prettier", "--stdin-filepath", "/Users/torbjorn/Development/carrot/dwh/.prettierrc" }
2024-11-13 00:59:06[DEBUG] Run CWD: /Users/torbjorn/Development/carrot/dwh
2024-11-13 00:59:06[DEBUG] prettier exited with code 0
2024-11-13 00:59:10[DEBUG] Running formatters on /Users/torbjorn/Development/carrot/dwh/.prettierrc: { "prettier" }
2024-11-13 00:59:10[INFO] Run prettier on /Users/torbjorn/Development/carrot/dwh/.prettierrc
2024-11-13 00:59:10[DEBUG] Run command: { "/Users/torbjorn/.local/share/nvim/mason/bin/prettier", "--stdin-filepath", "/Users/torbjorn/Development/carrot/dwh/.prettierrc" }
2024-11-13 00:59:10[DEBUG] Run CWD: /Users/torbjorn/Development/carrot/dwh
2024-11-13 00:59:10[INFO] prettier exited with code 2

It shows two identical runs, 4 seconds apart. One succeeds, one fails. Do you know of any difference between those two runs? Did anything change in the project between them? Were they both run with the same options?

torbjornvatn commented 1 week ago

So I did a new one, one an actual Typescript file in one of the subfolders this time. Now it only ran once, but still failed with the same error. And the CLI command cd /Users/torbjorn/Development/carrot/dwh && cat /Users/torbjorn/Development/carrot/dwh/.prettierrc | /Users/torbjorn/.local/share/nvim/mason/bin/prettier --stdin-filepath /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts fails with the same error

2024-11-13 01:17:06[DEBUG] Running formatters on /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts: { "prettier" }
2024-11-13 01:17:06[INFO] Run prettier on /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts
2024-11-13 01:17:06[DEBUG] Run command: { "/Users/torbjorn/Development/carrot/dwh/data-api/node_modules/.bin/prettier", "--stdin-filepath", "/Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts" }
2024-11-13 01:17:06[DEBUG] Run CWD: /Users/torbjorn/Development/carrot/dwh
2024-11-13 01:17:06[INFO] prettier exited with code 2
2024-11-13 01:17:06[DEBUG] prettier stdout: { "" }
2024-11-13 01:17:06[DEBUG] prettier stderr: { "[error] /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts: Error: Cannot find package '@trivago/prettier-plugin-sort-imports' imported from /Users/torbjorn/Development/carrot/dwh/noop.js", "[error]     at __node_internal_ (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:16460:11)", "[error]     at new NodeError (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:16425:5)", "[error]     at packageResolve (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17366:9)", "[error]     at moduleResolve (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17406:18)", "[error]     at defaultResolve (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17497:16)", "[error]     at resolve2 (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17514:12)", "[error]     at importFromFile (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17529:16)", "[error]     at importFromDirectory (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22444:10)", "[error]     at importPlugin (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22456:12)", "[error]     at async loadPluginWithoutCache (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22460:18)", "[error]     at async Promise.all (index 0)", "[error]     at async Promise.all (index 1)", "[error]     at async file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22722:17", "[error]     at async getContextOptions (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:1677:23)", "[error]     at async Context.pushContextPlugins (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:2566:21)", "[error]     at async getOptionsForFile (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3159:5)", "[error]     at async formatStdin (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3375:21)", "[error]     at async main (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3872:5)", "[error]     at async Module.run (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3822:5)", "" }
2024-11-13 01:17:06[ERROR] Formatter 'prettier' error: [error] /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts: Error: Cannot find package '@trivago/prettier-plugin-sort-imports' imported from /Users/torbjorn/Development/carrot/dwh/noop.js
[error]     at __node_internal_ (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:16460:11)
[error]     at new NodeError (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:16425:5)
[error]     at packageResolve (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17366:9)
[error]     at moduleResolve (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17406:18)
[error]     at defaultResolve (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17497:16)
[error]     at resolve2 (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17514:12)
[error]     at importFromFile (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:17529:16)
[error]     at importFromDirectory (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22444:10)
[error]     at importPlugin (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22456:12)
[error]     at async loadPluginWithoutCache (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22460:18)
[error]     at async Promise.all (index 0)
[error]     at async Promise.all (index 1)
[error]     at async file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/index.mjs:22722:17
[error]     at async getContextOptions (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:1677:23)
[error]     at async Context.pushContextPlugins (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:2566:21)
[error]     at async getOptionsForFile (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3159:5)
[error]     at async formatStdin (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3375:21)
[error]     at async main (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3872:5)
[error]     at async Module.run (file:///Users/torbjorn/Development/carrot/dwh/data-api/node_modules/prettier/internal/cli.mjs:3822:5)
stevearc commented 1 week ago

nit: you'll want the cat file to be the same one you pass in as the --stdin-filepath, like so

cd /Users/torbjorn/Development/carrot/dwh && cat /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts | /Users/torbjorn/.local/share/nvim/mason/bin/prettier --stdin-filepath /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts

However, I don't think that would have changed the fact that it errors. Can you find an invocation that works? Perhaps by changing the CWD? Conform can't do much unless there's a known good formatting command.

torbjornvatn commented 1 week ago

Ah now I get it...

So I ran cat /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts | /Users/torbjorn/.local/share/nvim/mason/bin/prettier --stdin-filepath /Users/torbjorn/Development/carrot/dwh/data-api/src/app.ts inside the /data-api/ folder and that worked just fine. But running it in the root folder fails with the before mentioned error.

And trying to do a format of the same file in neovim also gives the same error. CWD is Run CWD: /Users/torbjorn/Development/carrot/dwh and there are no package.json file in that root folder.

ro0gr commented 1 week ago

I'm trying to reproduce it w/o luck currently. My steps to reproduce:

I've checked the binary via both cwds:

$ cat ./examples/example.js | ~/.local/share/nvim/mason/bin/prettier --stdin-filepath ./examples/example.js

and

$ cd ./examples && cat ./example.js | ~/.local/share/nvim/mason/bin/prettier --stdin-filepath ./example.js                  

and I can see no error.

ConformInfo does also look fine for me:

Log file: /Users/RHR/.local/state/nvim/conform.log
          2024-11-13 11:37:12[INFO] Run prettier on /private/tmp/prettier-plugin-sort-imports/examples/example.js
          2024-11-13 11:37:12[DEBUG] Run command: { "/private/tmp/prettier-plugin-sort-imports/node_modules/.bin/prettier", "--stdin-filepath", "/private/tmp/prettier-plugin-sort-imports/examples/example.js" }
          2024-11-13 11:37:12[DEBUG] Run CWD: /private/tmp/prettier-plugin-sort-imports/examples
          2024-11-13 11:37:14[DEBUG] prettier exited with code 0
          2024-11-13 11:37:30[DEBUG] Running formatters on /private/tmp/prettier-plugin-sort-imports/examples/example.js: { "prettier" }
          2024-11-13 11:37:30[INFO] Run prettier on /private/tmp/prettier-plugin-sort-imports/examples/example.js
          2024-11-13 11:37:30[DEBUG] Run command: { "/private/tmp/prettier-plugin-sort-imports/node_modules/.bin/prettier", "--stdin-filepath", "/private/tmp/prettier-plugin-sort-imports/examples/example.js" }
          2024-11-13 11:37:30[DEBUG] Run CWD: /private/tmp/prettier-plugin-sort-imports/examples
          2024-11-13 11:37:32[DEBUG] prettier exited with code 0

Formatters for this buffer:
LSP: ts_ls
prettier ready (javascript) /private/tmp/prettier-plugin-sort-imports/node_modules/.bin/prettier
ro0gr commented 1 week ago

I'd be glad to help, cause most likely your issue has been caused by my recent prettier related changes. I'd suggest to focus on making the reproduction steps reproducible, so I can see the issue.

Maybe you can provide your minimal init.lua(like suggested in the Bug template) and a branch in the https://github.com/trivago/prettier-plugin-sort-imports which shows the issue?

torbjornvatn commented 1 week ago

It's not the code in https://github.com/trivago/prettier-plugin-sort-imports I'm having problems formatting, it's just that I use that prettier plugin in my own project.

Here's a small example repo where I'm able to reproduce the error: https://github.com/torbjornvatn/conform-monorepo-example/tree/main

  1. Clone the repo and run npm install in the sub-module folder
  2. Open the repo root in neovim
  3. Open the index.mjs file and try to format the file. I use <leader>cf since I've set up neovim with Lazyvim.org
ro0gr commented 1 week ago

Thanks for the reproduction, that makes it clearer.

With the recent changes the cwd detection logic has changed, so now it points to the repo root in your case, where the actual prettier config is located. In turn, this prettier config references the prettier-plugin-sort-imports , but there is no package.json with the dependency specified on the same level or above. That's why prettier fails even if run it manually from the repo root.

Though the recent prettier changes in conform turn out to be breaking for your case, I think this is actually a fix. I'd suggest to consider adjusting your repo setup, so the "prettier-plugin-sort-imports" is accesible from your prettier config.

There are few alternatives:

  1. We could work it around on the conform side to encounter for the prettier being present in the dependencies. However, I believe this is a wrong behavior, and I've even specifically tested against this case:

https://github.com/stevearc/conform.nvim/blob/1a7ff54dcfbe1af139b11829c6d58f5ffab87707/tests/formatters/prettierd_spec.lua#L98

  1. if your repo setup should stay unchanged for some reason, you could update your conform config to maintain the old behavior:
  cwd = util.root_file({
    -- https://prettier.io/docs/en/configuration.html
    ".prettierrc",
    ".prettierrc.json",
    ".prettierrc.yml",
    ".prettierrc.yaml",
    ".prettierrc.json5",
    ".prettierrc.js",
    ".prettierrc.cjs",
    ".prettierrc.mjs",
    ".prettierrc.toml",
    "prettier.config.js",
    "prettier.config.cjs",
    "prettier.config.mjs",
    "package.json",
  }),
torbjornvatn commented 1 week ago

Ok, thanks for the explanation

I'll try to change my repo, or look into tweaking the conform config