Vim-prettier: Error on `:wq`

Created on 4 Nov 2019  路  14Comments  路  Source: prettier/vim-prettier

Do you want to request a feature or report a bug?
bug
What is the current/expected behavior?

Current behavior

When I save and quit with :wq, I get the following error:

Error detected while processing function <lambda>5[1]..<SNR>125_onExit:
line    3:
E684: list index out of range: -1
E15: Invalid expression: a:out[len(a:out) - 1]

Expected behavior

At least there should not be no error. It would be great if the file is formatted before quitting.

What version of vim-prettier are you using - (output of :PrettierVersion) ?
1.0.0-alpha

What version of prettier are you using - (output of :PrettierCliVersion) ?
1.18.2
What is your prettier executable path - (output of :PrettierCliPath) ?
prettier
Did this work in previous versions of vim-prettier and/or prettier ?
I have been getting this error for at least several months.

The cause of this error is the following part in s:onExit that runs when prettier process exists.
The function is supposed to be run when prettier finishes executing but I suppose when I type :wq the prettier process exits even though it has not finished formatting yet. Thus a:out can become empty array and thus causing index-out-of-range error.

function! s:onExit(status, info, out, err) abort
  ...
  let l:last = a:out[len(a:out) - 1]
  ...
endfunction

Possible fix and question

The error can be fixed by checking emptiness of the array before accessing the array. This checking is currently done below after the line causing the error, which I want to know why.
If I can simply move the lines below to the top of the function, then I will submit a PR so let me know what you think.

  " we have no prettier output so lets exit
  if len(a:out) == 0 | return | endif

Though the emptiness check removes this error, it will not format the file because prettier has not finish the formatting at the moment of quitting. I am not sure how to realize this. Any idea?

Most helpful comment

I'm not 100% if this would be an acceptable solution, but just copying the if len(l:out) check from further down in the function and doing an early exit when there's no prettier problems fixes it for me:

diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim
index 7f69f4e..35e5f82 100644
--- a/autoload/prettier/job/async/neovim.vim
+++ b/autoload/prettier/job/async/neovim.vim
@@ -42,6 +42,8 @@ endfunction
 "
 "  note that somehow we exectuing both async and sync on nvim when using the autoformat
 function! s:onExit(status, info, out, err) abort
+  if len(a:out) == 0 | return | endif
+
   let l:currentBufferNumber =  bufnr('%')
   let l:isInsideAnotherBuffer = a:info.buf_nr != l:currentBufferNumber ? 1 : 0
   let l:last = a:out[len(a:out) - 1]

All 14 comments

Any thoughts/activities on this? I'm experiencing the same issue here

Hi All,

Could you please post the output of vim --version

thanks :)

Would be great to know the vim version you folks are experiencing while running this issue in order for me to send a fix for it

@mitermayer This happens on my personal machine, from which I'm away right now. Will be posting the version info here in two or three hours

vim: 8.2
neovim: 0.4.3

Thanks!

Will upgrade my vim to those versions and try it out! looks like there could be some upstream changes in vim itself

Greetings, fellow vimmers/vimians! I have also encountered this behavior. I tried editing the script autoload/prettier/job/async/neovim.vim but my vimscript isn't good enough to really understand - I think I fixed it but I wasn't sure if I was just hiding the problem.

If this helps at all, doing :w and :q as two separate steps in sequence does not yield an error; it's only when performing :wq

Error

Error detected while processing function <lambda>6[1]..<SNR>176_onExit: 
line    3:
E684: list index out of range: -1
E15: Invalid expression: a:out[len(a:out) - 1]

Diagnostic Info

$ uname -r
4.19.0-8-amd64

$ lsb_release -cs
buster

$ nvim --version
NVIM v0.3.4
Build type: Release
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -g -O2 -fdebug-prefix-map=/build/neovim-RiUqEm/neovim-0.3.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=1 -DDISABLE_LOG -Wdate-time -D_FORTIFY_SOURCE=1 -Wconversion -O2 -DNDEBUG -DMIN_LOG_LEVEL=3 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fdiagnostics-color=auto -Wno-array-bounds -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -I/build/neovim-RiUqEm/neovim-0.3.4/build/config -I/build/neovim-RiUqEm/neovim-0.3.4/src -I/usr/include -I/build/neovim-RiUqEm/neovim-0.3.4/build/src/nvim/auto -I/build/neovim-RiUqEm/neovim-0.3.4/build/include
Compiled by [email protected]

Features: +acl +iconv -jemalloc +tui 
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

~/.vim/plugged/vim-prettier $ git fetch --all; git status
Fetching origin
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Hi,
Same issue here using NVIM 0.5 (on debian 10)

NVIM v0.5.0-445-gca02db1f9
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/gcc-5 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/home/travis/build/neovim/bot-ci/build/neovim/build/config -I/home/travis/build/neovim/bot-ci/build/neovim/src -I/home/travis/build/neovim/bot-ci/build/neovim/.deps/usr/include -I/usr/include -I/home/travis/build/neovim/bot-ci/build/neovim/build/src/nvim/auto -I/home/travis/build/neovim/bot-ci/build/neovim/build/include
Compiled by travis@travis-job-41e98c7d-18cc-46e0-90e2-721b8f0d476a

Features: +acl +iconv +tui

I'm also seeing the same issue. Error only crops up when vim-prettier is configured to format on save. As @acidDrain said above, this is not an issue when :w and :q are run as separate commands, only when they are issued together like :wq.

Error

Error detected while processing function <lambda>4[1]..<SNR>108_onExit:
line    3:
E684: list index out of range: -1
E15: Invalid expression: a:out[len(a:out) - 1]

Diagnostic info

$ uname -r
5.3.0-46-generic

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:    18.04
Codename:   bionic

$ nvim --version
NVIM v0.3.8
Build type: Release
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -g -O2 -fdebug-prefix-map=/build/neovim-lpKDO8/neovim-0.3.8=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=1 -DDISABLE_LOG -Wdate-time -D_FORTIFY_SOURCE=1 -Wconversion -O2 -DNDEBUG -DMIN_LOG_LEVEL=3 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fdiagnostics-color=auto -Wno-array-bounds -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -I/build/neovim-lpKDO8/neovim-0.3.8/build/config -I/build/neovim-lpKDO8/neovim-0.3.8/src -I/usr/include -I/build/neovim-lpKDO8/neovim-0.3.8/build/src/nvim/auto -I/build/neovim-lpKDO8/neovim-0.3.8/build/include
Compiled by [email protected]

Features: +acl +iconv -jemalloc +tui

I'm not 100% if this would be an acceptable solution, but just copying the if len(l:out) check from further down in the function and doing an early exit when there's no prettier problems fixes it for me:

diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim
index 7f69f4e..35e5f82 100644
--- a/autoload/prettier/job/async/neovim.vim
+++ b/autoload/prettier/job/async/neovim.vim
@@ -42,6 +42,8 @@ endfunction
 "
 "  note that somehow we exectuing both async and sync on nvim when using the autoformat
 function! s:onExit(status, info, out, err) abort
+  if len(a:out) == 0 | return | endif
+
   let l:currentBufferNumber =  bufnr('%')
   let l:isInsideAnotherBuffer = a:info.buf_nr != l:currentBufferNumber ? 1 : 0
   let l:last = a:out[len(a:out) - 1]

I'm not 100% if this would be an acceptable solution, but just copying the if len(l:out) check from further down in the function and doing an early exit when there's no prettier problems fixes it for me:

diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim
index 7f69f4e..35e5f82 100644
--- a/autoload/prettier/job/async/neovim.vim
+++ b/autoload/prettier/job/async/neovim.vim
@@ -42,6 +42,8 @@ endfunction
 "
 "  note that somehow we exectuing both async and sync on nvim when using the autoformat
 function! s:onExit(status, info, out, err) abort
+  if len(a:out) == 0 | return | endif
+
   let l:currentBufferNumber =  bufnr('%')
   let l:isInsideAnotherBuffer = a:info.buf_nr != l:currentBufferNumber ? 1 : 0
   let l:last = a:out[len(a:out) - 1]

I can confirm that this fixes the issue for me on Neovim 0.4.3 without any apparent changes in Prettier's behavior.

Please feel free to submit a PR and I can merge it

:wq

Error detected while processing function <lambda>6[1]..<SNR>177_onExit:
line    3:
E684: list index out of range: -1
E15: Invalid expression: a:out[len(a:out) - 1]

NVIM v0.5.0-b39edb5

I'm not 100% if this would be an acceptable solution, but just copying the if len(l:out) check from further down in the function and doing an early exit when there's no prettier problems fixes it for me:

diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim
index 7f69f4e..35e5f82 100644
--- a/autoload/prettier/job/async/neovim.vim
+++ b/autoload/prettier/job/async/neovim.vim
@@ -42,6 +42,8 @@ endfunction
 "
 "  note that somehow we exectuing both async and sync on nvim when using the autoformat
 function! s:onExit(status, info, out, err) abort
+  if len(a:out) == 0 | return | endif
+
   let l:currentBufferNumber =  bufnr('%')
   let l:isInsideAnotherBuffer = a:info.buf_nr != l:currentBufferNumber ? 1 : 0
   let l:last = a:out[len(a:out) - 1]

This fixed it for me. Wait for updating.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dedoyle picture dedoyle  路  11Comments

christopherball picture christopherball  路  4Comments

Luobata picture Luobata  路  7Comments

rkulla picture rkulla  路  3Comments

ajcrites picture ajcrites  路  8Comments