Vim-prettier: Neovim unstable async formatting bugs

Created on 11 Jan 2018  路  19Comments  路  Source: prettier/vim-prettier

Hi, I noticed that after updating my vim-prettier that in some files, the output is not correctly inserted into my neovim. Sometimes there are some random line breaks for whatever reason. Checking out one commit before cddacf16253dcd8a0747202f22413c7cd3f0160a solves the problem for me. So I guess something is wrong there?

I have a larger javascript file and an export foo get's broken down to

t foo

Let me now if you need some exact file to reproduce this, then I can try to create one without my actual code in it that leads to the problem.

Most helpful comment

@mitermayer Thanks for the hard work.
I've just looked through the code at https://github.com/prettier/vim-prettier/blob/2ee343b23b5fa207d7fe75acc57008194bc8c9b4/autoload/prettier/job/async/neovim.vim#L22
It's seems that line break problem is not resolved, the l:out is not guaranteed to contain lines. See :h channel-lines

I would suggest to use buffered mode for one time job :h buffered, it's added in neovim version 0.2.3

All 19 comments

Hi @wwwdata,

Thanks for reporting this, the neovim async feature still experimental, could you provide me some steps on how to repro it ? also what version of neovim are you using and prettier ?

One of the goals for the next major release (1.0) is to have full support for neovim so your help now on helping figure out some of this issues is greatly appreciated, in the meantime I may disable and/or create a feature experimental flag to disable neovim Async while we work on it.

nvim 0.2.2 and prettier 1.9.1

It is really hard to create a file that also fails.. I cannot just copy paste the real code right now from one of our projects.. I will try to play around with a couple open source repos later and see if I can reliably break something in there. But it needs to be a large file with at least 160 LOC and some variable names with underscores _

Also when running prettier manually on the command line, everything is fine obviously :)

ok I have an example. Just download this file from the react repo and run it in neovim with prettier

https://raw.githubusercontent.com/facebook/react/master/packages/react-dom/src/test-utils/ReactTestUtils.js

For me this is breaking on line 275 which is a comment block and inserts a new line that should not be there.

  /**
   * Pass a mocked component module to this method to augment it with
   * useful methods that allow it to be used as a dummy React component.
   * Instead of rendering as usual, the component will become a simple
   * <div> containing any 
provided children.
   *
   * @param {object} module the mock function object exported from a
   *                        module that defines the component to be mocked

Thank you very much for this info @wwwdata , will look into this ASAP and keep you posted.

I'm also getting some issues with prettier, in vim works fine, in nvim I'm having some issues.

Imagine this piece of code:

<Col size="12" alignSelf="center" md="auto">
  <Link to="/">
    <Logo isDefault />
  </Link>
</Col>

Since I have updated now it doesn't save the file, it just changes the buffer. Which will raise eslint warnings.

Let's say I remove the Logo line entirely:

<Col size="12" alignSelf="center" md="auto">
  <Link to="/">
-    <Logo isDefault />
  </Link>
</Col>

hit :w

I will get

<Col size="12" alignSelf="center" md="auto">
+  <Link to="/" />
-    <Logo isDefault />
-  </Link>
</Col>

But that result is not saved is just on the buffer, the saved result it's still:

  <Link to="/">
-    <Logo isDefault />
  </Link>

Now eslint will run against the saved file and I will get two errors, one is because Logo is not being used anymore, which is a correct and valid warning, the other is about prettier.
screen shot 2018-01-15 at 14 31 02

If run :w again it will save the current buffer which has the changes from prettier, therefore making the error for prettier go way.

basically I'm forced to run :w two times

The command I used to trigger Prettier is:

autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql  PrettierAsync

Thank you very much for all the info, in the meantime I disabled nvim async on master while we work on fixing the above, if you still want to keep using nvim async in the meantime you can add this to your .vimrc

   g:prettier#nvim_unstable_async=1

Without the above flag nvim will be running sync in the meantime. I hope to get a fix for it in place soon. Thank you all for the info also feel free to submit PR's for it if you feel like adventuring on viml

How it was previously was it always sync? Can't we just use Vim async? sorry for my ignorance, my vim scripting is very bad. I even tried to do some sort of debugging to help you guys, but no luck.

It seems that BufWritePre doesn't wait for nvim async job to finish (which might be the objective?)

Hi @FabioAntunes , before it was always running sync even when async flag was set (we did not have support for neovim async until a week ago), we will revert to that behaviour while we can fix the new async path.

I create a new issue to track this on https://github.com/prettier/vim-prettier/issues/99

cc @chemzqm

Will close this issue for now since i will be tracking the async support on another issue, feel free to comment in here or on the next issue. Hopefuly will be able to fix the above reported bugs soon and do a new release

Tryied to replicate @wwwdata your use case with the file from the react repo. Both vim and neovim had same formatting output.

The reason why you may be getting diffren't outputs is not related to vim-prettier is related to the prettier version that is specified on the react repo itself. To confirm this can you try running prettier from the command line using the ./node_modules/.bin/prettier from the react folder instead of globally installed prettier that you may have ?

@FabioAntunes, I just confirmed the issue you have described. The reason is that the vim8 async formatting issues a save after formatting and the neovim format is not currently doing it. Will fix it now!

@mitermayer I just copied the one file and tested it with the globally installed prettier version, yes. But outside of neovim it was just formatting fine.

Also in the repo that we are using in the company, it formats fine, except when I run it inside neovim with the new async feature, then I had some occasional newlines that should not be in there. But outside of neovim, the output was always correct.

hi @wwwdata,

If you have some time could you give it a shot on our 1.x release branch ? We are currently working on the new 1.x major release! I believe the neovim on that branch should be working as expected

Yes I upgraded and everything is working fine now. Thanks!

@mitermayer Thanks for the hard work.
I've just looked through the code at https://github.com/prettier/vim-prettier/blob/2ee343b23b5fa207d7fe75acc57008194bc8c9b4/autoload/prettier/job/async/neovim.vim#L22
It's seems that line break problem is not resolved, the l:out is not guaranteed to contain lines. See :h channel-lines

I would suggest to use buffered mode for one time job :h buffered, it's added in neovim version 0.2.3

Hi @chemzqm,

Thanks for pointing that out! Do you think you could submit a PR for branch 1.x ? :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tugorez picture tugorez  路  7Comments

cdimitroulas picture cdimitroulas  路  5Comments

dedoyle picture dedoyle  路  11Comments

mitermayer picture mitermayer  路  6Comments

christopherball picture christopherball  路  4Comments