Vim-prettier: Prettier: failed to parse buffer

Created on 11 Nov 2020  Â·  16Comments  Â·  Source: prettier/vim-prettier

Do you want to request a feature or report a bug?

In NeoVim show error Prettier: failed to parse buffer in I save JavaScript file.

This is my init.vim configure

language en_US

" Easy split navigation
nnoremap <C-w> <C-w>w
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

set nocompatible 
set encoding=UTF-8
filetype plugin indent on

syntax on
set termguicolors
colorscheme gruvbox
set background=dark
set guifont=Iosevka\ Slab:h16
set laststatus=2

set clipboard^=unnamed,unnamedplus
set t_RV=
set t_Co=256
set number
set showmatch
set visualbell

set hlsearch
set smartcase
set ignorecase
set incsearch

set autoindent
set shiftwidth=2
set smartindent
set smarttab
set softtabstop=2

set ruler
set undolevels=1000
set backspace=indent,eol,start

set nobackup
set nowritebackup
set noswapfile
set noundofile

set cmdheight=2
set updatetime=300
set shortmess+=c
set signcolumn=yes

" Plugins
call plug#begin('~/.config/nvim/plugged')

Plug 'morhetz/gruvbox'

" Frontend
Plug 'othree/html5.vim'
Plug 'elzr/vim-json'
Plug 'JulesWang/css.vim'
Plug 'ap/vim-css-color'
Plug 'prettier/vim-prettier', { 'do': 'npm install' }
Plug 'pangloss/vim-javascript'
Plug 'posva/vim-vue'

" Elm
Plug 'elmcast/elm-vim'

" Elixir
Plug 'elixir-lang/vim-elixir', { 'for': 'elixir' }
Plug 'slashmili/alchemist.vim', { 'for': 'elixir' }

" Svelte
Plug 'evanleck/vim-svelte'

" Themes
Plug 'itchyny/lightline.vim'

" Miscellaneous
Plug 'tpope/vim-sensible'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-commentary'
Plug 'airblade/vim-gitgutter'
Plug 'w0rp/ale'
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'mileszs/ack.vim'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'editorconfig/editorconfig-vim'
Plug 'sheerun/vim-polyglot'
Plug 'mattn/emmet-vim'
Plug 'sbdchd/neoformat'
Plug 'neoclide/coc.nvim', {'branch': 'release'}

call plug#end()

" Python
let g:python_host_prog = '/usr/local/bin/python'
let g:python3_host_prog = '/usr/local/bin/python3'

" Nerdtree
let g:NERDTreeIgnore = ['.DS_Store', '.cache$', 'node_modules$', '.git$', 'debug$', '\~$']
let g:NERDTreeShowHidden = 1
let g:NERDTreeMinimalUI = 1
let g:NERDTreeStatusline = ''
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
map <F2> :NERDTreeToggle<CR>

" Elm
"let g:polyglot_disabled = ['elm']
let g:elm_setup_keybindings = 0
let g:elm_detailed_complete = 1
let g:elm_format_autosave = 1

" Vue
let g:vue_disable_pre_processors = 1

" Prettier
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql PrettierAsync
let g:prettier#autoformat = 1
let g:prettier#exec_cmd_async = 1
let g:prettier#config#parser = 'babylon'

" Ale
let g:ale_fixers = {
\  'elixir': ['mix_format'],
\  'javascript': ['prettier'],
\  'typescript': ['prettier'],
\  'vue': ['prettier'],
\  'scss': ['prettier'],
\  'html': ['prettier'],
\  'svelte': ['prettier']
\}
let g:ale_fix_on_save = 1
let g:ale_completion_autoimport = 1

" Fzf
let $FZF_DEFAULT_COMMAND = 'rg --files --hidden --follow --glob "!{node_modules/*,debug/*,.git/*}"'
nnoremap <C-b> :Buffers<CR>
nnoremap <leader><leader> :Commands<CR>
nnoremap <C-p> :Files<CR>

" Emmet
autocmd FileType html,css EmmetInstall
let g:user_emmet_install_global = 0
let g:user_emmet_leader_key=','

" Commentary
autocmd FileType apache setlocal commentstring=#\ %s

What is the current/expected behavior?

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

What version of prettier are you using - (output of :PrettierCliVersion) ?
2.1.2

What is your prettier executable path - (output of :PrettierCliPath) ?
Users/dangvanthanh/.config/nvim/plugged/vim-prettier/node_modules/.bin/prettier

Most helpful comment

I found the workaround.

put this in vim config

let g:prettier#config#single_quote = 'true'
let g:prettier#config#trailing_comma = 'all'

All 16 comments

Hi, I'm receiving the same error.

.vimrc

" specify dir for plugins
call plug#begin('~/.vim/plugged')

Plug 'prettier/vim-prettier', { 'do' : 'yarn install' }

" colorschemes
Plug 'morhetz/gruvbox'

" initialize plugin system
call plug#end()

:PrettierVersion 1.0.0-beta
:PrettierCliVersion 2.0.4
:PrettierCliPath /Users/khuynh/.vim/plugged/vim-prettier/node_modules/.bin/prettier

MacVim 8.2

Same here

call plug#begin('~/.vim/plugged')
Plug 'prettier/vim-prettier', { 'do': 'yarn install' }
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
call plug#end()

More details

:PrettierVersion      1.0.0-beta
:PrettierCliVersion   2.0.4
:PrettierCliPath      home/spongebob/.vim/plugged/vim-prettier/no

I had the same issue with old versions.
It got resolved when I removed .prettierrc
After upgrading to the latest, even without a config file, I get this error leaving me unable to use the plugins.

I found the workaround.

put this in vim config

let g:prettier#config#single_quote = 'true'
let g:prettier#config#trailing_comma = 'all'

I found the workaround.

put this in vim config

let g:prettier#config#single_quote = 'true'
let g:prettier#config#trailing_comma = 'all'

Thanks @zirho

I was removed

let g:prettier#config#parser = 'babylon'

then put your config. It's working for me.

i can confirm that @zirho 's solution works for me!

I too confirm @zirho 's solution worked for me!

this might be related to error
[error] Invalid --trailing-comma value. Expected "all", "none", "php5" or "php7.2", but received "es5".

possibly related to NVM and the default version of node being set in the system.
Prettier is being run with the default version and that could lead to this error.

I can confirm that @zirho's solution also worked for me :), really enjoying this plugin, thank you to the devs for creating this it is wonderful to get Prettier support inside of Neovim!

FWIW and in case it's a clue for the root cause, I only needed one line:

let g:prettier#config#trailing_comma = 'all'

from @zirho's solution to get rid of Prettier: failed to parse buffer. That was with vim-prettier 1.0.0-beta and prettier installed by the plugin at version 2.0.4.

It is potentially an issue with the php plugin, though I've no idea. Maybe it is overwriting the available trailing comma options? I'm getting the same issue on my Mac setup. If I remove the plugins from the package.json file (as I don't need any of them) then delete node_modules and yarn install again then it starts working.

I would also deeply appreciate some way of getting more information out of prettier when seeing the "failed to parse buffer" error. I didn't see any option but is there some kind of verbose logging mode to help debug this? I ended up adding echom join(a:errors, "\n") to the onError function in job/runner.vim.

Possibly also because the package.json specifies 0.10.2 for the php plugin and that was released back in February 2019. There have been a number of fixes (probably including this) since then.

It might make sense to specify a much looser constraint.

https://github.com/prettier/vim-prettier/blob/bc7ae99c38a1d0f58380347515b212f93df5e68e/package.json#L16-L19

I found the workaround.

put this in vim config

let g:prettier#config#single_quote = 'true'
let g:prettier#config#trailing_comma = 'all'

Thank you so much – this finally fixed my issue!

This is indeed related to prettier-php version being too old. The problem was fixed:
https://github.com/prettier/plugin-php/issues/964
Updating to a newer version than plugin-php 0.11.1 should solve the problem.

This is indeed related to prettier-php version being too old. The problem was fixed:
prettier/plugin-php#964
Updating to a newer version than plugin-php 0.11.1 should solve the problem.

Not fixed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gko picture gko  Â·  5Comments

mitermayer picture mitermayer  Â·  8Comments

Luobata picture Luobata  Â·  7Comments

mitermayer picture mitermayer  Â·  5Comments

christopherball picture christopherball  Â·  4Comments