-
Notifications
You must be signed in to change notification settings - Fork 6k
Add trailing spaces presubmit check #5733
Conversation
travis/format.sh
Outdated
|
|
||
| for f in $FILES_TO_CHECK; do | ||
| set +e | ||
| TRAILING_SPACES="$(grep -P " +$" $f)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd look for trailing tab characters too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use \s to match all whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, use single quotes in the grep invocation in the subshell, and add double quotes around the "$f".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I'd use "-E" instead of "-P", since that doesn't exist on macOS grep: you can use the blank character class too (which is space and tab).
grep -E '[[:blank:]]+$' .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid both -E and -P with \s unless [[:blank:]] matches a superset of \s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks for the suggestion!
travis/format.sh
Outdated
|
|
||
| if [[ $FAILED_CHECKS -ne 0 ]]; then | ||
| echo "" | ||
| echo "ERROR: Some files have trailing spaces. To fix, try something like \`find . -name "*.dart" -print0 | xargs -0 perl -pi -e 's/ +$//'\`." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Patch up as described in person, should be significantly shorter too :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
travis/format.sh
Outdated
| for f in $FILES_TO_CHECK; do | ||
| set +e | ||
| TRAILING_SPACES="$(grep -P " +$" $f)" | ||
| TRAILING_SPACES="$(grep '\s\+$' $f)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that \s also matches return, vertical tab, form feed and, technically, newline. I'd just use [[:blank:]], since that just matches actual space and tab characters, and the others, while we probably want to remove them if they're in the code, it's more likely that they're intentionally there as part of a """ string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, also \s (which is a synonym for [[:space:]]) is locale dependent and will match any whitespace characters in the locale, and those (besides space and tab) are even more likely to be intentional, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if we're sure we want to eliminate ALL whitespace at EOL, then I'm fine with \s, since there are workarounds if you really want whitespace at EOL in a """ string anyhow (which now that I think about it more is probably a bad practice in any case).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, ignore me. Listen to Chris. :-)
|
FYI, I made a PR for a similar check for the flutter repo: flutter/flutter#19329 |
|
@cbracken : please review the changed the |
travis/format.sh
Outdated
| FILETYPES="*.c *.cc *.cpp *.h *.m *.mm *.dart" | ||
| FILES_TO_CHECK="$(git diff $DIFF_OPTS -- master $FILETYPES)" | ||
|
|
||
| for f in $FILES_TO_CHECK; do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd actually suggest you do this with xargs: it results in fewer invocations of grep (so it's marginally faster), it's more compact, and it avoids problems where the command line size limit is reached.
TRAILING_SPACES=$(git diff $DIFF_OPTS master -- $FILETYPES | xargs grep --line-number --with-filename '\s\+$')There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
travis/format.sh
Outdated
|
|
||
| for f in $FILES_TO_CHECK; do | ||
| set +e | ||
| TRAILING_SPACES="$(grep --line-number '\s\+$' $f)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll also want to add --with-filename so that the grep line includes the filename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Previously, the git diff always returns empty because everything after `--` is considered <path>. (So master is parsed as <path> instead of <branch>.)
Otherwise, the script will exit with error if grep finds no match.
|
@gspencergoog : |
|
Works for me. LGTM (again). |
This reverts commit a28c3e7.
|
This is giving me unexpected results on https://travis-ci.org/flutter/engine/builds/408311343?utm_source=github_status&utm_medium=notification. I have run |
|
We don't run clang-format for dart so this guards dart. I think there might
be some windows new lines in path_win.cc and that's why it failed this
script? The `sed` command should fix that.
…On Wed, Jul 25, 2018, 6:18 PM Chinmay Garde ***@***.***> wrote:
This is giving me unexpected results on
https://travis-ci.org/flutter/engine/builds/408311343?utm_source=github_status&utm_medium=notification.
I have run clang-format over the file to make sure. Also, why are we
reinventing clang-format? Both #5734
<#5734> and #5708
<#5708> would have been caught if
we had ensured that clang-format was run over the diff. There is also a
python script that performs clang-format over just the diff instead of over
the entire source tree.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#5733 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AV7DMPMuViYeeCdmJpeXAXFOWegwmBHUks5uKRjtgaJpZM4VN8uZ>
.
|
|
Can we just run this on Dart files then? I'll try the sed command again.
What's weird is that I was editing that file on my Mac.
Chinmay
…On Wed, Jul 25, 2018 at 7:51 PM liyuqian ***@***.***> wrote:
We don't run clang-format for dart so this guards dart. I think there might
be some windows new lines in path_win.cc and that's why it failed this
script? The `sed` command should fix that.
On Wed, Jul 25, 2018, 6:18 PM Chinmay Garde ***@***.***>
wrote:
> This is giving me unexpected results on
>
https://travis-ci.org/flutter/engine/builds/408311343?utm_source=github_status&utm_medium=notification
.
> I have run clang-format over the file to make sure. Also, why are we
> reinventing clang-format? Both #5734
> <#5734> and #5708
> <#5708> would have been caught if
> we had ensured that clang-format was run over the diff. There is also a
> python script that performs clang-format over just the diff instead of
over
> the entire source tree.
>
> —
> You are receiving this because you modified the open/close state.
> Reply to this email directly, view it on GitHub
> <#5733 (comment)>,
or mute
> the thread
> <
https://github.com/notifications/unsubscribe-auth/AV7DMPMuViYeeCdmJpeXAXFOWegwmBHUks5uKRjtgaJpZM4VN8uZ
>
> .
>
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub
<#5733 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AACsNd3OKNafNOOhFF2fLMGdkvXKJAwwks5uKS6dgaJpZM4VN8uZ>
.
|
|
Confirmed that there were CLRF new lines:
#5871
Those probably are not caused by your Mac, but by the original author who
wrote the code on Windows.
Considering this new line issue that the clang-format doesn't catch, maybe
we should leave this script as is?
On Wed, Jul 25, 2018 at 8:09 PM Chinmay Garde <notifications@github.com>
wrote:
… Can we just run this on Dart files then? I'll try the sed command again.
What's weird is that I was editing that file on my Mac.
Chinmay
On Wed, Jul 25, 2018 at 7:51 PM liyuqian ***@***.***> wrote:
> We don't run clang-format for dart so this guards dart. I think there
might
> be some windows new lines in path_win.cc and that's why it failed this
> script? The `sed` command should fix that.
>
> On Wed, Jul 25, 2018, 6:18 PM Chinmay Garde ***@***.***>
> wrote:
>
> > This is giving me unexpected results on
> >
>
https://travis-ci.org/flutter/engine/builds/408311343?utm_source=github_status&utm_medium=notification
> .
> > I have run clang-format over the file to make sure. Also, why are we
> > reinventing clang-format? Both #5734
> > <#5734> and #5708
> > <#5708> would have been caught
if
> > we had ensured that clang-format was run over the diff. There is also a
> > python script that performs clang-format over just the diff instead of
> over
> > the entire source tree.
> >
> > —
> > You are receiving this because you modified the open/close state.
> > Reply to this email directly, view it on GitHub
> > <#5733 (comment)>,
> or mute
> > the thread
> > <
>
https://github.com/notifications/unsubscribe-auth/AV7DMPMuViYeeCdmJpeXAXFOWegwmBHUks5uKRjtgaJpZM4VN8uZ
> >
> > .
> >
>
> —
> You are receiving this because your review was requested.
> Reply to this email directly, view it on GitHub
> <#5733 (comment)>,
or mute
> the thread
> <
https://github.com/notifications/unsubscribe-auth/AACsNd3OKNafNOOhFF2fLMGdkvXKJAwwks5uKS6dgaJpZM4VN8uZ
>
> .
>
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#5733 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AV7DMPu_taPQLT9uS20vJWkilcjrHptQks5uKTMCgaJpZM4VN8uZ>
.
|
|
I am not sure this was really an issue. I'd rather have only one tool that
enforces formatting since it is easier to integrate into code editors and
lots of IDEs already have clang-format support built in. So from a
development perspective, there isn't much to do to get started. Now, if you
feel strongly about having uniform newlines, I am sure you can patch
clang-format itself. Similarly, for Dart code, I am sure patching the Dart
formatter to deal with newlines is the way to go.
…On Wed, Jul 25, 2018 at 8:25 PM liyuqian ***@***.***> wrote:
Confirmed that there were CLRF new lines:
#5871
Those probably are not caused by your Mac, but by the original author who
wrote the code on Windows.
Considering this new line issue that the clang-format doesn't catch, maybe
we should leave this script as is?
On Wed, Jul 25, 2018 at 8:09 PM Chinmay Garde ***@***.***>
wrote:
> Can we just run this on Dart files then? I'll try the sed command again.
> What's weird is that I was editing that file on my Mac.
>
> Chinmay
>
> On Wed, Jul 25, 2018 at 7:51 PM liyuqian ***@***.***>
wrote:
>
> > We don't run clang-format for dart so this guards dart. I think there
> might
> > be some windows new lines in path_win.cc and that's why it failed this
> > script? The `sed` command should fix that.
> >
> > On Wed, Jul 25, 2018, 6:18 PM Chinmay Garde ***@***.***>
> > wrote:
> >
> > > This is giving me unexpected results on
> > >
> >
>
https://travis-ci.org/flutter/engine/builds/408311343?utm_source=github_status&utm_medium=notification
> > .
> > > I have run clang-format over the file to make sure. Also, why are we
> > > reinventing clang-format? Both #5734
> > > <#5734> and #5708
> > > <#5708> would have been caught
> if
> > > we had ensured that clang-format was run over the diff. There is
also a
> > > python script that performs clang-format over just the diff instead
of
> > over
> > > the entire source tree.
> > >
> > > —
> > > You are receiving this because you modified the open/close state.
> > > Reply to this email directly, view it on GitHub
> > > <#5733 (comment)
>,
> > or mute
> > > the thread
> > > <
> >
>
https://github.com/notifications/unsubscribe-auth/AV7DMPMuViYeeCdmJpeXAXFOWegwmBHUks5uKRjtgaJpZM4VN8uZ
> > >
> > > .
> > >
> >
> > —
> > You are receiving this because your review was requested.
> > Reply to this email directly, view it on GitHub
> > <#5733 (comment)>,
> or mute
> > the thread
> > <
>
https://github.com/notifications/unsubscribe-auth/AACsNd3OKNafNOOhFF2fLMGdkvXKJAwwks5uKS6dgaJpZM4VN8uZ
> >
> > .
> >
>
> —
> You are receiving this because you modified the open/close state.
> Reply to this email directly, view it on GitHub
> <#5733 (comment)>,
or mute
> the thread
> <
https://github.com/notifications/unsubscribe-auth/AV7DMPu_taPQLT9uS20vJWkilcjrHptQks5uKTMCgaJpZM4VN8uZ
>
> .
>
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub
<#5733 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AACsNbqlA7wjtx4J6NVRVpjZEbEHKctGks5uKTaggaJpZM4VN8uZ>
.
|
|
Thanks @chinmaygarde for the feedback! I agree that using a single tool such as clang-format is a better way to go. I guess that we don't clang-format dart now because most of our dart files don't comply with clang-format. So if we're using clang-format for dart, we have to disable every other check except trailing spaces (and potentially new lines if we choose to do so). |
|
@liyuqian, |
|
@gspencergoog it does format Dart file for me: |
So we'll no longer need #5734 and #5708