Skip to content

Fix PrettierAsync ignored file contents mangling#142

Merged
mitermayer merged 2 commits intoprettier:masterfrom
SamHowie:master
Jun 18, 2018
Merged

Fix PrettierAsync ignored file contents mangling#142
mitermayer merged 2 commits intoprettier:masterfrom
SamHowie:master

Conversation

@SamHowie
Copy link
Copy Markdown
Collaborator

Summary
This pull request fixes #140 where the PrettierAsync command causes the buffer contents to become garbled if the file is ignored by prettier.

It was found to be an issue with more recent versions of prettier that echo the stdin[1] of ignored files back to stdout[2].

  1. https://github.com/prettier/prettier/blob/fb74dc54c337b92e2be6dfffe147ab42143e4e48/src/cli/util.js#L313
  2. https://github.com/prettier/prettier/blob/fb74dc54c337b92e2be6dfffe147ab42143e4e48/src/cli/util.js#L126

Unfortunately, Vim channels do not like this very much. Vim writes NUL bytes to Prettier's stdin to indicate line endings. However, Prettier is echoing them back and then Vim is having trouble reading them back from what it thinks is a string, leading to undefined behaviour.

A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read or write from a buffer. See |in_io-buffer| and |out_io-buffer|.

https://github.com/vim/vim/blob/master/runtime/doc/channel.txt#L407

The work around is to do what the vim documentation suggests and let the job write to a hidden buffer. This doesn't seem to be effected by the segmentation fault issue the input buffer was causing.

I also was having a weird rendering issue if the file contents had not changed. I added a redraw! to that code path and it fixed it. I am not sure if this issue was pre-existing.

Test Plan

  1. Make sure you have the latest Prettier installed
  2. Create a new directory
  3. Create a file called foo.js
  4. Create a .prettierignore file
  5. Add foo.js to the .prettierignore file
  6. Add several lines of text to the foo.js file
  7. Save the foo.js file
  8. Notice the file contents are not garbled on the last line

@SamHowie
Copy link
Copy Markdown
Collaborator Author

I have checked this on Vim 8. @mitermayer could you check on Vim 7.4 and NeoVim?

@mitermayer
Copy link
Copy Markdown
Member

Hi @SamHowie,

Thanks for looking into this! This is great, will do some sanity testing and comment on this issue

@mitermayer mitermayer merged commit 827eb1e into prettier:master Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

saving a file ignored in .prettierignore outputs part of the file back to the buffer

2 participants