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

errors from Prettier overwrite code in vim integration #743

Closed
mvolkmann opened this issue Feb 19, 2017 · 14 comments
Closed

errors from Prettier overwrite code in vim integration #743

mvolkmann opened this issue Feb 19, 2017 · 14 comments
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program type:editor support Issues about tools Prettier provides for use inside editor integrations.

Comments

@mvolkmann
Copy link

I have my editor configured to automatically format the code with Prettier on save.
If there is a syntax error in the code, Prettier will replace the code with its error output.
Is there a recommended way to avoid this?

@rauchg
Copy link

rauchg commented Feb 19, 2017

Just ran into this and I immediately became curious as to how tools like gofmt handle this.

It seems like we should probably write plugins that handle this scenario. Notice that vim-go's logic is pretty involved, not only to account for error handling, but cleaner undo stack and whatnot.

https://github.com/fatih/vim-go/blob/468fa9823b13844412759f7f614bece364e6e006/autoload/go/fmt.vim#L77

@rauchg rauchg mentioned this issue Feb 19, 2017
@vjeux
Copy link
Contributor

vjeux commented Feb 20, 2017

This sounds like a terrible experience :( Note that prettier returns a non 0 exit code when there's a syntax error and writes the error on stderr. So the problem is inside of the editor integration. with prettier.

@vjeux vjeux added the type:editor support Issues about tools Prettier provides for use inside editor integrations. label Feb 20, 2017
@rauchg
Copy link

rauchg commented Feb 20, 2017

@vjeux FYI

▲  front (prettify) prettier test.js 

test.js: SyntaxError: Unexpected token (2:0)
  1 | console.log(
> 2 | 
    | ^
▲  front (prettify) echo $?
2
▲  front (eslint) eslint test.js 

/Users/rauchg/Documents/Projects/front/test.js
  2:1  error  Parsing error: Unexpected token

✖ 1 problem (1 error, 0 warnings)

▲  front (eslint) echo $?
1

@rauchg
Copy link

rauchg commented Feb 20, 2017

It seems intended, but I'm not sure I understand why:

https://github.com/jlongster/prettier/blob/5d131027ce1452c6c581dbc5a59ab8369af4469b/bin/prettier.js#L171

@rauchg
Copy link

rauchg commented Feb 20, 2017

Actually, not relevant :( Even with exit code 1 vi will replace the buffer with the output from prettier. Maybe there's something we can add to the suggested vi config in the README to handle > 0

image

image

@vjeux
Copy link
Contributor

vjeux commented Feb 20, 2017

If you know the right incantation to get it to work with vi, we should definitely update the instructions for it

@jlongster
Copy link
Member

@rauchg The emacs integration pops open a new buffer with the error and it's a nice experience. It points me exactly to where it errored and I usually can fix it quickly, reformat, and the error buffer goes away.

The Emacs integration was forked from refmt which derives from other various integrations. It looks like it just checks for a 0 exit status: https://github.com/jlongster/prettier/blob/master/editors/emacs/prettier-js.el#L204. If it's non-zero it shows it in a new buffer.

You can also customize where errors are shown, either in the echo area, a new buffer, or in the current buffer.

I agree that vim's integration could be a lot better. I'd love for someone to work on more thorough vim integration.

@vjeux vjeux added the type:bug Issues identifying ugly output, or a defect in the program label Feb 20, 2017
@greis
Copy link

greis commented Mar 16, 2017

A workaround for now is to create a script file named prettier.sh with the following content:

stdin=`tee`
formatted=`prettier --stdin <<< "$stdin" 2> /dev/null`

if [ $? -ne 0 ]; then
  formatted=$stdin
fi

printf %s "$formatted"

Add that file to your $PATH and change the vim setting:

autocmd FileType javascript set formatprg=prettier.sh

@vjeux vjeux changed the title errors from Prettier overwrite code errors from Prettier overwrite code in vim integration Apr 7, 2017
@yakschuss
Copy link

@greis - Unfortunately, I can't seem to get this to work. I'm not too familiar with vim/prettier inner workings, would something like :redir work for this?

josephfrazier added a commit to josephfrazier/prettier that referenced this issue May 9, 2017
josephfrazier added a commit to josephfrazier/prettier that referenced this issue May 9, 2017
vjeux pushed a commit that referenced this issue May 9, 2017
* Add warning about vanilla Vim integration

Fixes #743
Fixes #1191
Fixes #1466

* Recommend Neoformat over vanilla Vim integration

See #743
See #1191
See #1466

* Remove vanilla Vim integration from README

Since it's relatively broken, we don't want to recommend it.
See #1568 (comment)

This also adds a TOC link for the "Other `autocmd` events" section,
which was previously missing.
@TinyBuilder
Copy link

Hi, despite using Neoformat it seems that the file is still overwritten on errors, at least when using the instructions provided for custom prettier settings. I am using neovim, if that helps.

@vjeux vjeux reopened this May 18, 2017
@josephfrazier
Copy link
Collaborator

@TinyBuilder, can you show your .vimrc and tell what commands you are using? I'm not able to replicate this. You may also want to make sure you're using the most recent version of Neoformat (not sure if it had a bug before, but it can't hurt).

@vjeux
Copy link
Contributor

vjeux commented May 21, 2017

We no longer recommend this integration for vim because of this issue. Using neoformat should work fine.

@vjeux vjeux closed this as completed May 21, 2017
@TinyBuilder
Copy link

TinyBuilder commented May 22, 2017

Very sorry for taking a while, I have had no access to my configuration for the last couple of days.

filetype plugin indent on
syntax enable
set nowrap
set tabstop=2
set shiftwidth=2
set expandtab
set number
set cursorline
set showcmd
colorscheme slate
set colorcolumn=101
highlight ColorColumn ctermbg=235

"Install Vim Plug if not installed.
if empty(glob('~/.config/nvim/autoload/plug.vim'))
  silent !curl -flo ~/.config/nvim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall
endif

call plug#begin('~/.local/share/nvim/plugged')
Plug 'rust-lang/rust.vim'
Plug 'pangloss/vim-javascript'
Plug 'mxw/vim-jsx'
Plug 'elzr/vim-json'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'w0rp/ale'
Plug 'roxma/nvim-completion-manager'
Plug 'roxma/nvim-cm-tern', {'do': 'npm install'}
Plug 'tpope/vim-fugitive'
Plug 'sbdchd/neoformat'
call plug#end()

let g:ale_linters = { 'javascript': ['eslint'] }

let g:deoplete#enable_at_startup = 1

let g:airline_powerline_fonts = 1
let g:airline_skip_empty_sections = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline_theme='durant'

let g:javascript_plugin_jsdoc = 1
let g:javascript_plugin_flow = 1
autocmd FileType javascript.jsx,javascript setlocal formatprg=prettier\ --stdin\ --single-quote\ --trailing-comma\ all\ --print-width\ 100
autocmd BufWritePre,InsertLeave *.js Neoformat
autocmd BufWritepre,InsertLeave *.jsx Neoformat
autocmd BufWritePre *.js exe "normal! gggqG\<C-o>\<C-o>"
autocmd BufWritePre *.jsx exe "normal! gggqG\<C-o>\<C-o>"
let g:neoformat_try_formatprg = 1

@mitermayer
Copy link
Member

This issue has been solved in https://github.com/mitermayer/vim-prettier, when any syntax error occur a quickfix will open with the line and error by default (this behaviour can be updated with configuration).

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 7, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program type:editor support Issues about tools Prettier provides for use inside editor integrations.
Projects
None yet
Development

No branches or pull requests

9 participants