Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettier: failed to parse buffer #268

Closed
dangvanthanh opened this issue Nov 11, 2020 · 23 comments
Closed

Prettier: failed to parse buffer #268

dangvanthanh opened this issue Nov 11, 2020 · 23 comments

Comments

@dangvanthanh
Copy link

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
Copy link
Contributor

hkennyv commented Nov 16, 2020

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
Copy link

LeducH commented Nov 19, 2020

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
Copy link

zirho commented Nov 21, 2020

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
Copy link

zirho commented Nov 21, 2020

I found the workaround.

put this in vim config

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

@dangvanthanh
Copy link
Author

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
Copy link
Contributor

hkennyv commented Nov 22, 2020

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

@sohel-sheikh
Copy link

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

@zirho
Copy link

zirho commented Nov 23, 2020

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

@zirho
Copy link

zirho commented Nov 23, 2020

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
Copy link

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
Copy link

mikepqr commented Nov 26, 2020

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
Copy link

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
Copy link

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.

"@prettier/plugin-lua": "0.0.1",
"@prettier/plugin-php": "^0.10.2",
"@prettier/plugin-ruby": "^0.8.0",
"@prettier/plugin-xml": "^0.7.2",

@jgreens
Copy link

jgreens commented Dec 16, 2020

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
Copy link

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.

hkennyv added a commit to hkennyv/vim-prettier that referenced this issue Dec 22, 2020
@LeducH
Copy link

LeducH commented Mar 4, 2021

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
Copy link

dvbsknd commented Jun 23, 2021

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
Copy link

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
Copy link

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.^@

@nguyenvukhang
Copy link

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
Copy link

TimDaub commented Mar 10, 2022

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

This comment was marked as outdated.

@JimmyAG
Copy link

JimmyAG commented Mar 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests