Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: With format_on_save, timeout_ms must be set really high to have prettier working #401

Closed
1 task done
caneta opened this issue May 13, 2024 · 3 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@caneta
Copy link

caneta commented May 13, 2024

Neovim version (nvim -v)

v0.10.0-dev

Operating system/version

WSL (Windows Subsystem Linux), Ubuntu 20.04.6 LTS

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

15:14:08[DEBUG] Running formatters on /home/user/my-project/Test.tsx: { "prettier" }
15:14:08[INFO] Run prettier on /home/user/my-project/Test.tsx
15:14:08[DEBUG] Run command: { "/home/user/my-project/node_modules/.bin/prettier", "--stdin-filepath", "/home/user/my-project/Test.tsx" }
15:14:08[DEBUG] Run CWD: /home/user/my-project
15:14:09[WARN] Formatter 'prettier' timeout
15:14:09[INFO] prettier exited with code 143
15:14:09[DEBUG] prettier stdout: { "" }
15:14:09[DEBUG] prettier stderr: { "" }

Describe the bug

I was expecting that the suggested conf of 500 ms had worked just fine, but I had a timeout.
The same result setting timeout_ms = nil, or removing it (so getting the default 1 sec, I guess.
It seems that like just the async option is not set to false as documented.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. nvim /hove/user/my-project/Test.tsx
  2. mess up the file
  3. :w

Expected Behavior

I was expecting to have the file formatted by prettier and next saved by nvim. Only save succeed, and Conform reports the errors above. The only way to make it work is set to a very hight value like timeout_ms = 999999

@caneta caneta added the bug Something isn't working label May 13, 2024
@stevearc
Copy link
Owner

The default value of 500ms should be plenty of time for most formatters, and since formatting freezes the UI you probably don't want it to be larger than 500ms anyway. The quick fix for you that I would recommend is try using format_after_save instead of format_on_save (these are documented here).

Both format_on_save and format_after_save are just convenience wrappers that set up some autocmds for you. format_on_save uses a BufWritePre autocmd, which means that it executes in between the :w and Neovim actually writing the buffer to disk. For this reason, we must complete the formatting synchronously and block all processing in the meantime, otherwise Neovim will write the unformatted buffer to disk while we are still formatting.

format_after_save uses the BufWritePost autocmd. After Neovim writes the buffer to disk, we try to format the file. This happens asynchronously and does not need to block the UI. Once formatting is completed, it applies the changes to the buffer and re-saves the buffer to disk. If the user has modified the buffer at all between formatting start and formatting completion, we abandon the changes (to avoid overwriting your edits).

When your formatter takes a long time to run, the recommended way to handle this is via async formatting (such as format_after_save).

@caneta
Copy link
Author

caneta commented May 14, 2024

Crystal-clear @stevearc, thank you so much for your detailed answer. It works like a charm!

@ic-768
Copy link

ic-768 commented Jul 22, 2024

I was struggling with this same issue, and what fixed this for me was using prettierd instead of prettier.
Much faster than prettier, and cleaner than running after save.

    conform.setup({
      formatters_by_ft = {
        javascript = { "eslint_d","prettierd" },
        typescript = { "eslint_d","prettierd" },
      }
     })

reddit post that turned me on to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants