prettier / vim-prettier

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

Prettier: failed to parse buffer (PrettierAsync) for every JS file #171

Open ds82 opened 5 years ago

ds82 commented 5 years ago

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

What is the current/expected behavior? Using PrettierAsync always results in the error Prettier: failed to parse buffer. Prettier command works fine (on every file).

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

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

What is your prettier executable path - (output of :PrettierCliPath) ? /Users/dsaenger/code/titan-web/node_modules/.bin/prettier (local project path)

Did this work in previous versions of vim-prettier and/or prettier ? Did not use vim-prettier before

To be clear: This error always occurs with whatever JS file when using :PrettierAsync, even Hello World:

console.log('Hello World');

.. but :Prettier works fine.

I'm using https://github.com/skwp/dotfiles vim config .. maybe vim-prettier does not work with another plugin .. how can I debug this?

mitermayer commented 5 years ago

I will look into this ASAP

mitermayer commented 5 years ago

Could you try with the prettier 1.0 branch ?

to try with the 1.0 branch please update your .vimrc to from:

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

to:

Plug 'prettier/vim-prettier', { 'do': 'yarn install', branch: 'release/1.x' }

Curious to see if it still broken in there too

ds82 commented 5 years ago

Thanks for taking the time and looking into this! I tried the 1.0 branch but the result is exactly the same :/

mitermayer commented 5 years ago

HI @ds82 do you mind trying one more thing ?

on your vimrc :

try adding this

let g:prettier#config#config_precedence = 'file-override'
ds82 commented 5 years ago

I don't mind at all .. I'm happy that you try to fix my problem! Unfortunately, setting the variable makes no differences either :/

joerter commented 5 years ago

Hi @mitermayer, I had the same problem but switching to the release/1.x branch with 1.0.0-alpha version from the PrettierVersion command fixed the issue for me.

mitermayer commented 5 years ago

I will try to merge release/1.x branch to master ASAP, in the meantime feel free folks to use that branch directly

Plug 'prettier/vim-prettier', {
  \ 'do': 'yarn install',
  \ 'branch': 'release/1.x',
  \ 'for': [
    \ 'javascript',
    \ 'typescript',
    \ 'css',
    \ 'less',
    \ 'scss',
    \ 'json',
    \ 'graphql',
    \ 'markdown',
    \ 'vue',
    \ 'lua',
    \ 'php',
    \ 'python',
    \ 'ruby',
    \ 'html',
    \ 'swift' ] }
mitermayer commented 5 years ago

If anyone else could please try the release/1.x branch to confirm that it does fix this issue that would be great!

ds82 commented 5 years ago

Tried the branch again (hash cb598c1212dfe20f5a5a950bdee529d224d574d9), but still does not work :/

mitermayer commented 5 years ago

@ds82 thanks for trying, can you post your vim --version paste ? I am still trying to figure out how to repro

serebrov commented 5 years ago

I've just got this issue, trying the vim-prettier with release/1.x branch, to get the php support. Both in js and php files running :Prettier and :PrettierAsync resulted in Prettier: failed to parse buffer.

The cause appeared to be the ruby plugin, I've just removed it manually for now:

~/.vim/plugged/vim-prettier on release/1.x!
$ yarn remove @prettier/plugin-ruby

Sidenote: the error message is not informative enough, maybe consider adding some debug / verbose mode. To find the issue I added echo a:errors into this functiona:

function! prettier#job#runner#onError(errors) abort
  call prettier#logging#error#log('PARSING_ERROR')
  echo 'Errors'
  echo a:errors
  ...
endfunction

And got the error:

[
'/home/user/.vim/plugged/vim-prettier/node_modules/@prettier/plugin-ruby/src/nodes.js:5', 
'  ...require("./nodes/alias"),',
'  ^^^',
'SyntaxError: Unexpected token ...',
'    at createScript (vm.js:56:10)',
'    at Object.runInThisContext (vm.js:97:10)',
'    at Module._compile (module.js:542:28)',
'    at Object.Module._extensions..js (module.js:579:10)
',
'    at Module.load (module.js:487:32)',
'    at tryModuleLoad (module.js:446:12)',
'    at Function.Module._load (module.js:438:3)',
'    at Module.require (module.js:497:17)
',
'    at require (internal/module.js:20:19)',
'    at Object.<anonymous> (/home/user/.vim/plugged/vim-prettier/node_modules/@prettier/plugin-ruby/src/print.js:3:15)',
'    at Module._compile (module.js:570:32)',
'    at Object.Module._extensions..js (module.js:579:10)',
'    at Module.load (module.js:487:32)',
'    at tryModuleLoad (module.js:446:12)',
'    at Function.Module._load (module.js:438:3)',
'    at Module.require (module.js:497:17)'
]
mitermayer commented 5 years ago

Hi @ serebrov

The above error seems to come from prettier/ruby cc @kddeisz

mitermayer commented 5 years ago

On the sidenote i will add to release/1.x a task to have errors a bit more clear when they are non syntax related

kddnewton commented 5 years ago

What's the minimum node version required for this? I specified 8 in the ruby plugin, but maybe I need to support older versions. This is using the spread operator which I guess this doesn't understand.

mitermayer commented 5 years ago

Hi @kddeisz ,

We currently do not have any specified node version https://github.com/prettier/vim-prettier/blob/master/package.json

kddnewton commented 5 years ago

Alright I just released v0.8.0 for @prettier/plugin-ruby which supports the obsolete node versions. Let me know if people still have problems.

mitermayer commented 5 years ago

Thanks @kddeisz !

mitermayer commented 5 years ago

Just updated release/1.x to have the latest ruby prettier plugin https://github.com/prettier/vim-prettier/commit/18a42315acc254755b122b4afaf3896466227b6c

@serebrov do you mind trying it again ?

serebrov commented 5 years ago

@mitermayer yes, works for me now on node 6.10.

kddnewton commented 5 years ago

Is this closable? It's just still on my radar so just want to make sure it's all fixed up.

mitermayer commented 5 years ago

This issue is closed on release/1.x but since master still behind i will leave it open until this weekend when i will do the release/1.x release!

kddnewton commented 5 years ago

Sounds great!

mitermayer commented 5 years ago

This issue has been solved on release/1.x branch will close this to help me track fixed issues for the release changelog

stiofand commented 4 years ago

This issue is NOT solved in release/1.x

1.0.0 alpha still fails

mitermayer commented 4 years ago

Will reopen this to investigate issue reported above

kelly-ry4n commented 4 years ago

I just ran into this message - sounds slightly different then what others are experiencing, but maybe one of these steps will help somebody else who lands here.

In my case, running :PrettierCliPath in vimh told me where my executables were, and then running that executable directly against any JS file revealed that I had an invalid .prettierrc for the version of prettier which installed with vim-prettier (but not my system version).

/Users/me/.vim/plugged//vim-prettier/node_modules/.bin/prettier myfile.tsx

resulted in

[error] Unable to expand glob patterns: hooks/useMeasure.tsx !**/node_modules/** !./node_modules/** !**/.{git,svn,hg}/** !./.{git,svn,hg}/**
[error] Invalid trailingComma value. Expected "all", "none", "php5" or "php7.2", but received "es5".

From here, fixing was easy, just fix errors in my .prettierrc until it stopped complaining. Now it's working great!

gabrielhpugliese commented 4 years ago

I had the same issue after updating node to lts (v12.17.0 in my case). I upgraded to newest prettier version (2.0.5) and it works now.

winkee01 commented 4 years ago

I encountered the same issue. on Sep 15, 2020

khaitranhq commented 4 years ago

I met same issue, Step 18, 2020 This is my configuration: https://github.com/lioaslan/nvim And I run :Prettier with a simple JS code

const x = 5;
            console.log(x);

It showed that error

masdarzulfikar commented 4 years ago

I had the same issue. All I did just npm i -g prettier, and it works now.

khaitranhq commented 4 years ago

I had the same issue. All I did just npm i -g prettier, and it works now.

It worked for me. Thanks

PROgram52bc commented 4 years ago

I had the same issue. All I did just npm i -g prettier, and it works now.

Thanks! It worked for me, too. I wonder why the local version of prettier doesn't work, though. Is that a bug?

iryan2 commented 4 years ago

For me, it apparently turned out to be something wrong with the system-installed version of nodejs (which I've never touched because nvm). Installed the LTS from nodejs.org and vim-prettier started working.

MachineLearning-Nerd commented 3 years ago

npm i -g prettier

This also worked for me.

mainangethe commented 3 years ago

I've tried all of the above to no fail. :disappointed:

" Set prettier to run automatically
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.sass,*.json,*.graphql,*.md,*.vue,*.yml, *.yaml, *.html PrettierAsync
let g:prettier#autoformat = 1
let g:prettier#exec_cmd_async = 1
let g:prettier#config#parser = 'babylon'
let g:prettier#config#single_quote = 'true'
let g:prettier#config#trailing_comma = 'all'

Even tried the npm i -g prettier - still failed

Prettier Version: 1.0.0-beta Prettier CLI Version: 2.2.1

Not sure what to do anymore

PROgram52bc commented 3 years ago

I've tried all of the above to no fail.

" Set prettier to run automatically
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.sass,*.json,*.graphql,*.md,*.vue,*.yml, *.yaml, *.html PrettierAsync
let g:prettier#autoformat = 1
let g:prettier#exec_cmd_async = 1
let g:prettier#config#parser = 'babylon'
let g:prettier#config#single_quote = 'true'
let g:prettier#config#trailing_comma = 'all'

Even tried the npm i -g prettier - still failed

Prettier Version: 1.0.0-beta Prettier CLI Version: 2.2.1

Not sure what to do anymore

For me, switching to 'release/1.x' branch worked. You might also try out ale, though I haven't tested it.

andrescuco commented 3 years ago

I had this issue, :Prettier was working as expected but when I was using it on save it failed. I was using this configuration to auto format on save:

let g:prettier#autoformat = 1
let g:prettier#autoformat_require_pragma = 0

Then reading :h prettier I realized that this was outdated, and changed it to:

let g:prettier#autoformat = 0
autocmd BufWritePre *.js Prettier

Now it works. Something to note is that if I use PrettierAsync instead of Prettier on BufWritePre the issue persists. Another thing that may help is that I used yarn instead of npm when installing release/1.x. Plug 'prettier/vim-prettier', { 'do': 'yarn install', 'branch': 'release/1.x' }

mainangethe commented 3 years ago

I had this issue, :Prettier was working as expected but when I was using it on save it failed. I was using this configuration to auto format on save:

let g:prettier#autoformat = 1
let g:prettier#autoformat_require_pragma = 0

Then reading :h prettier I realized that this was outdated, and changed it to:

let g:prettier#autoformat = 0
autocmd BufWritePre *.js Prettier

Now it works. Something to note is that if I use PrettierAsync instead of Prettier on BufWritePre the issue persists. Another thing that may help is that I used yarn instead of npm when installing release/1.x. Plug 'prettier/vim-prettier', { 'do': 'yarn install', 'branch': 'release/1.x' }

This worked for me even without going to 'release/1.x' Seems the commands are outdated. Thanks @andrescuco

skylarmb commented 3 years ago

This happened to me when i added a new rule to the prettier config that had invalid syntax e.g. "arrowParens", "avoid", instead of "arrowParens": "avoid",

zirho commented 3 years ago

In my case, this is due to the node version in the session that you run vim with is not compatible with prettier that installed locally for the project.

try

./node_modules/.bin/prettier -v

if this errors out, try to lower the node version to around v10.* in my case,

nvm use v10

got my problem solved.

jmcgover commented 1 year ago

I'm hitting the issue with PrettierAsync, but not Prettier. It doesn't matter if I run PrettierAsync manually or on file save.

$ npm -v
9.5.1
$ ~/.vim/bundle/node_modules/.bin/prettier -v
2.8.8
$ cat ~/.vim/bundle/vim-prettier/package.json 
{
  "name": "vim-prettier",
  "author": "M* R* <m*@*.com>",
  "version": "1.0.0-beta",
  "description": "Vim plugin for prettier",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "git://github.com/prettier/vim-prettier.git"
  },
  "scripts": {
    "test": "vim --version && LOG_LEVEL=error jest",
    "lint": "vint --version && vint ."
  },
  "dependencies": {
    "@prettier/plugin-lua": "0.0.1",
    "@prettier/plugin-php": "^0.16.3",
    "@prettier/plugin-ruby": "^0.8.0",
    "@prettier/plugin-xml": "^0.7.2",
    "prettier-plugin-svelte": "^2.3.1",
    "prettier": "^2.3.1"
  },
  "devDependencies": {
    "colors": "^1.3.2",
    "jest": "^23.6.0",
    "vim-driver": "^1.0.0"
  }
}