vim-prettier icon indicating copy to clipboard operation
vim-prettier copied to clipboard

Prettier: failed to parse buffer

Open dangvanthanh opened this issue 4 years ago • 21 comments

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

dangvanthanh avatar Nov 11 '20 13:11 dangvanthanh

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

hkennyv avatar Nov 16 '20 06:11 hkennyv

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

LeducH avatar Nov 19 '20 16:11 LeducH

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.

zirho avatar Nov 21 '20 16:11 zirho

I found the workaround.

put this in vim config

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

zirho avatar Nov 21 '20 16:11 zirho

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.

dangvanthanh avatar Nov 22 '20 01:11 dangvanthanh

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

hkennyv avatar Nov 22 '20 06:11 hkennyv

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

sohel-sheikh avatar Nov 22 '20 15:11 sohel-sheikh

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

zirho avatar Nov 23 '20 23:11 zirho

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.

zirho avatar Nov 23 '20 23:11 zirho

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!

smithbm2316 avatar Nov 25 '20 06:11 smithbm2316

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.

mikepqr avatar Nov 26 '20 21:11 mikepqr

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.

michaeljones avatar Nov 30 '20 10:11 michaeljones

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

michaeljones avatar Nov 30 '20 11:11 michaeljones

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!

jgreens avatar Dec 16 '20 17:12 jgreens

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.

gadkadosh avatar Dec 18 '20 11:12 gadkadosh

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

LeducH avatar Mar 04 '21 15:03 LeducH

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 stumbled across this issue after searching for the Prettier: failed to parse buffer error and my issue was indeed my Node version. My shell was running version 8 via nvm rather than my default version 16 when I started vim. After exiting, switching and trying again it worked.

dvbsknd avatar Jun 23 '21 09:06 dvbsknd

I found the workaround.

put this in vim config

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

This works for me but I want to run prettier with the defaults so it's not an ideal solution.

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 stumbled across this issue after searching for the Prettier: failed to parse buffer error and my issue was indeed my Node version. My shell was running version 8 via nvm rather than my default version 16 when I started vim. After exiting, switching and trying again it worked.

What's the best way to check which version of node vim-prettier is running and is there a way to configure a version for vim-prettier? NVM is currently set to current LTS v14.17.4

aaaaargZombies avatar Aug 08 '21 07:08 aaaaargZombies

This is causing me issues for me now :(

Solutions I tried:

  • I tried to use the prettier config mentioned (though I don't see how it affects the parser!)

My versions:

  1. PrettierVersion: 1.0.0-beta
  2. PrettierCliPath: ~/.vim/plugged/vim-prettier/node_modules/.bin/prettier
  3. PrettierCli: [error] No parser and no file path given, couldn't infer a parser.^@

jikkujose avatar Nov 29 '21 10:11 jikkujose

In my experience this issue is caused by me renaming the root directory of a directory containing node packages.

My fix has been to delete the node_modules folder and run npm or yarn to refresh it:

rm -rf node_modules
yarn

nguyenvukhang avatar Dec 10 '21 15:12 nguyenvukhang

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.

This fixed it for me.

TimDaub avatar Mar 10 '22 18:03 TimDaub

(removed)

louielyl avatar Apr 07 '23 10:04 louielyl

I found the workaround.

put this in vim config

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

I tried this but it did not work for me on my _vimrc file on windows, surprisingly enough :Prettier or my map <Leader><Leader>p works just fine and .ts/tsx files work just fine on save!

autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.svelte,*.yaml,*.html PrettierAsync

I don't face this issue on my linux machine however

JimmyAG avatar Mar 16 '24 18:03 JimmyAG