prettier / vim-prettier

A Vim plugin for Prettier
MIT License
1.78k stars 138 forks source link

Prettier: failed to parse buffer #268

Closed dangvanthanh closed 1 year ago

dangvanthanh commented 3 years ago

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

hkennyv commented 3 years ago

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

LeducH commented 3 years ago

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
zirho commented 3 years ago

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 commented 3 years ago

I found the workaround.

put this in vim config

let g:prettier#config#single_quote = 'true'
let g:prettier#config#trailing_comma = 'all'
dangvanthanh commented 3 years ago

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.

hkennyv commented 3 years ago

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

sohel-sheikh commented 3 years ago

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

zirho commented 3 years ago

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

zirho commented 3 years ago

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.

smithbm2316 commented 3 years ago

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!

mikepqr commented 3 years ago

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.

michaeljones commented 3 years ago

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 commented 3 years ago

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

jgreens commented 3 years ago

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!

gadkadosh commented 3 years ago

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.

LeducH commented 3 years ago

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

dvbsknd commented 3 years ago

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.

aaaaargZombies commented 3 years ago

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

jikkujose commented 2 years ago

This is causing me issues for me now :(

Solutions I tried:

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.^@
nguyenvukhang commented 2 years ago

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
TimDaub commented 2 years ago

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.

louielyl commented 1 year ago

(removed)

JimmyAG commented 7 months ago

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