-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
vet: add check for trailing spaces #7576
Conversation
Will fix the existing files before getting this in
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #7576 +/- ##
==========================================
+ Coverage 81.88% 81.91% +0.03%
==========================================
Files 361 361
Lines 27813 27813
==========================================
+ Hits 22775 22784 +9
+ Misses 3845 3841 -4
+ Partials 1193 1188 -5 |
Release notes are for users. Only gRPC-Go developers will care about vet. |
scripts/vet.sh
Outdated
@@ -184,4 +184,11 @@ revive -formatter plain ./... >"${REV_OUT}" || true | |||
# TODO: Remove `|| true` to unskip linter failures once existing issues are fixed. | |||
(noret_grep -v "unused-parameter" "${REV_OUT}" | not grep -v "\.pb\.go:") || true | |||
|
|||
# Collection of trailing spaces analysis checks | |||
TRAIL_SPACE_OUT="$(mktemp)" |
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.
Let's use a pipe instead of creating a file whenever possible, please.
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
scripts/vet.sh
Outdated
find . -type f -exec grep -Hn "[[:blank:]]$" {} \; >"${TRAIL_SPACE_OUT}" | ||
|
||
# Error for anything other than in .git directory, vendor directory and *.md files. | ||
noret_grep -v "./.git" "${TRAIL_SPACE_OUT}" | noret_grep -v "vendor" | not grep -v ".md" |
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.
Can we avoid scanning these files instead of excluding them later? find
has a -prune
flag that may be useful.
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 dont think we have a vendor directory?
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
27de5b1
to
7a274af
Compare
ALL FIXED in follow up commit |
scripts/vet.sh
Outdated
@@ -184,4 +184,7 @@ revive -formatter plain ./... >"${REV_OUT}" || true | |||
# TODO: Remove `|| true` to unskip linter failures once existing issues are fixed. | |||
(noret_grep -v "unused-parameter" "${REV_OUT}" | not grep -v "\.pb\.go:") || true | |||
|
|||
# Error if trailing spaces found in any files excluding files in .git directory and *.md files | |||
$(find . -path ./.git -prune -o -type f ! -name "*.md" -exec grep -Hn "[[:blank:]]$" {} \;) | fail_on_output |
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.
Why exclude markdown files? I think trailing whitespaces should always be disallowed?
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.
FIxed
scripts/vet.sh
Outdated
@@ -184,4 +184,7 @@ revive -formatter plain ./... >"${REV_OUT}" || true | |||
# TODO: Remove `|| true` to unskip linter failures once existing issues are fixed. | |||
(noret_grep -v "unused-parameter" "${REV_OUT}" | not grep -v "\.pb\.go:") || true | |||
|
|||
# Error if trailing spaces found in any files excluding files in .git directory and *.md files | |||
$(find . -path ./.git -prune -o -type f ! -name "*.md" -exec grep -Hn "[[:blank:]]$" {} \;) | fail_on_output |
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.
Why surround the command in $()
? This will cause the output of grep to be executed, which isn't what we want. I believe we can remove the surrounding $()
find . -path ./.git -prune -o -type f ! -name "*.md" -exec grep -Hn "[[:blank:]]$" {} \; | fail_on_output
However, this will not show all the violations because fail_on_output
is implemented to exit early with a non-zero exit code if it sees any output. So it closes the read side of the pipe causing errors similar to the following if grep is still writing more results.
find . -path ./.git -prune -o -type f ! -name "*.md" -exec grep -Hn "[[:blank:]]$" {} \; | fail_on_output
./features/advancedtls/creds/server_cert.pem:52: X509v3 Subject Key Identifier:
./features/advancedtls/creds/server_cert.pem:54: X509v3 Authority Key Identifier:
./features/advancedtls/creds/server_cert.pem:56: X509v3 Basic Constraints:
./features/advancedtls/creds/server_cert.pem:58: X509v3 Key Usage:
./features/advancedtls/creds/server_cert.pem:60: X509v3 Subject Alternative Name:
find: ‘grep’ terminated by signal 13
find: ‘grep’ terminated by signal 13
A workaround would be to define a version of fail_on_output
that reads the entire input before exiting, the following code from an LLM demonstrates this:
fail_on_output() {
output=$(tee /dev/stderr) # Read the entire output
if [ -n "$output" ]; then
return 1
fi
}
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 missed it. Good catch. The current command does show the violations though
scripts/vet.sh
Outdated
@@ -184,4 +184,7 @@ revive -formatter plain ./... >"${REV_OUT}" || true | |||
# TODO: Remove `|| true` to unskip linter failures once existing issues are fixed. | |||
(noret_grep -v "unused-parameter" "${REV_OUT}" | not grep -v "\.pb\.go:") || true | |||
|
|||
# Error if trailing spaces found in any files excluding files in .git directory and *.md files | |||
find . -path ./.git -prune -o -type f -exec grep -Hn "[[:blank:]]$" {} \; | fail_on_output |
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.
Does git grep "[[:blank:]]$" | fail_on_output
not work? That seems a lot simpler.
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 for some reason grep fails fail_on_output
even when there are no violations of trailing whitespace. We probably need to modify fail_on_output()
function or use something else
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 gave it a try on my end, and it seems to be working fine
~/grpc-go pr/purnesh42H/7576*
❯ git grep "[[:blank:]]$" | fail_on_output
~/grpc-go pr/purnesh42H/7576*
❯ echo $?
0
~/grpc-go pr/purnesh42H/7576*
❯ git grep "[[:blank:]]" | fail_on_output
~/grpc-go pr/purnesh42H/7576*
❯ echo $?
1
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 I don't i see Success at the end when running scripts/vet.sh
with this command even though no violations. That's why I am not sure.
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.
See the other places we are doing git grep
in here then... E.g.
# - Ensure that the deprecated protobuf dependency is not used.
not git grep "\"github.com/golang/protobuf/*" -- "*.go" ':(exclude)reflection/test/grpc_testing_not_regenerate/*'
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.
"git grep '[[:blank:]]$' returns an exit status of 1 when no trailing spaces are found.. Hence, we have to negate it. Let me know if this looks good now
db745d0
to
54a8599
Compare
scripts/vet.sh
Outdated
@@ -67,6 +67,9 @@ not git grep "\"github.com/golang/protobuf/*" -- "*.go" ':(exclude)reflection/te | |||
# - Ensure all usages of grpc_testing package are renamed when importing. | |||
not git grep "\(import \|^\s*\)\"google.golang.org/grpc/interop/grpc_testing" -- "*.go" | |||
|
|||
# Ensure that no trailing spaces are found. |
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'm not sure why, exactly, but all the section comments start with -
; please continue the pattern here.
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
54a8599
to
72dd06c
Compare
RELEASE NOTES: None