Skip to content

Fix buildifier error reporting. #104

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

Merged
merged 1 commit into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion autoload/codefmt/buildifier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ function! codefmt#buildifier#GetFormatter() abort
" Parse all the errors and stick them in the quickfix list.
let l:errors = []
for line in split(v:exception, "\n")
let l:tokens = matchlist(line, '\C\v^stdin:(\d+):(\d+):\s*(.*)')
if empty(l:fname)
let l:fname_pattern = 'stdin'
else
let l:fname_pattern = escape(l:fname, '\')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could just make the pattern start out something like '\C\v[^:]+:\(\d+)..., and assume that filenames never contained a :, but I suspect that wouldn't work everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could imagine corner cases with the filename being escaped/expanded/canonicalized that with this approach would lead to the error not being matched at all. It might be worth figuring out how to genericize it properly eventually (not a blocker since this does fix the major issue).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that buildifier returns exactly the string we pass to it via -path (after all, it doesn't actually get to read the file content: that's piped to stdin). I'll commit this as-is, and we can always improve it if we need to later.

endif
let l:tokens = matchlist(line,
\ '\C\v^\V'. l:fname_pattern . '\v:(\d+):(\d+):\s*(.*)')
if !empty(l:tokens)
call add(l:errors, {
\ "filename": @%,
Expand Down
42 changes: 42 additions & 0 deletions vroom/buildifier.vroom
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,45 @@ Errors are reported using the quickfix list.
:echomsg string(map(getqflist(),
|'v:val.lnum . "," . v:val.col . "," . v:val.text'))
~ ['2,15,syntax error near ]']

Note that if the file's path was passed to buildifier, error reports are in a
slightly different format, prefixed with the filename we passed.

@clear
:silent file /foo/bar/BUILD
:set filetype=bzl
% load()

> go
:FormatCode
! buildifier .*2> (.*)
$ echo '/foo/bar/BUILD:1:7: syntax error near )' >\1 (command)
$ 1 (status)
load()
@end

We'll be on column 6 because there's no column 7. (Buildifier appears to always
report one column past the actual error.)

:echomsg line('.') . ',' . col('.')
~ 1,6
:echomsg string(map(getqflist(),
|'v:val.lnum . "," . v:val.col . "," . v:val.text'))
~ ['1,7,syntax error near )']

If there are no errors, then the quickfix list is (currently, see
https://github.com/google/vim-codefmt/issues/58) left alone:

> godd
% load('@io_bazel_rules_go//proto:def.bzl','go_proto_library')
> go

:FormatCode
! buildifier -path /foo/bar/BUILD .*
$ load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

:echomsg line('.') . ',' . col('.')
~ 1,1
:echomsg len(getqflist())
~ 1
:set filetype=