From 545555a74baa7265b164dceda459c6b6a669bd60 Mon Sep 17 00:00:00 2001 From: Oliver Sun Date: Fri, 6 Sep 2024 14:09:44 -0400 Subject: [PATCH 1/5] Allow comment ignore with trailing comment --- private/bufpkg/bufcheck/client.go | 23 ++++++++----------- private/bufpkg/bufcheck/lint_test.go | 8 +++++++ .../a.proto | 4 ++++ .../buf.yaml | 4 ++++ 4 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/a.proto create mode 100644 private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/buf.yaml diff --git a/private/bufpkg/bufcheck/client.go b/private/bufpkg/bufcheck/client.go index d6b77773c7..a90caed35e 100644 --- a/private/bufpkg/bufcheck/client.go +++ b/private/bufpkg/bufcheck/client.go @@ -494,27 +494,24 @@ func ignoreLocation( } // checkCommentLineForCheckIgnore checks that the comment line starts with the configured -// comment ignore prefix, and the rest of the string is the ruleID of the check. +// comment ignore prefix, a number of spaces (at least one), the ruleID of the check. // -// We currently do not support multiple rules per comment ignore: +// All of the following comments ignore SERVICE_PASCAL_CASE and this rule only: // -// Invalid: -// // buf:lint:ignore SERVICE_SUFFIX, SERVICE_PASCAL_CASE +// // buf:lint:ignore SERVICE_PASCAL_CASE, SERVICE_SUFFIX +// // buf:lint:ignore SERVICE_PASCAL_CASE +// // buf:lint:ignore SERVICE_PASCAL_CASE some other comment // -// Valid: -// // buf:lint:ignore SERVICE_SUFFIX -// // buf:lint:ignore SERVICE_PASCAL_CASE +// While the following is invalid and a nop +// +// // buf:lint:ignoreSERVICE_PASCAL_CASE func checkCommentLineForCheckIgnore( commentLine string, commentIgnorePrefix string, ruleID string, ) bool { - if after, ok := strings.CutPrefix(commentLine, commentIgnorePrefix); ok { - if strings.TrimSpace(after) == ruleID { - return true - } - } - return false + fullIgnorePrefix := commentIgnorePrefix + " " + ruleID + return strings.HasPrefix(commentLine, fullIgnorePrefix) } type lintOptions struct { diff --git a/private/bufpkg/bufcheck/lint_test.go b/private/bufpkg/bufcheck/lint_test.go index e08fb0a1f7..f9e2123a73 100644 --- a/private/bufpkg/bufcheck/lint_test.go +++ b/private/bufpkg/bufcheck/lint_test.go @@ -1188,6 +1188,14 @@ func TestCommentIgnoresOnlyRule(t *testing.T) { ) } +func TestCommentIgnoresWithTrailingComment(t *testing.T) { + t.Parallel() + testLint( + t, + "comment_ignores_with_trailing_comment", + ) +} + func TestRunLintCustomPlugins(t *testing.T) { t.Parallel() testLint( diff --git a/private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/a.proto b/private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/a.proto new file mode 100644 index 0000000000..9f0c7f5756 --- /dev/null +++ b/private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/a.proto @@ -0,0 +1,4 @@ +syntax = "proto3"; + +// buf:lint:ignore PACKAGE_DIRECTORY_MATCH trailing comment after the ignore comment is also OK +package a; diff --git a/private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/buf.yaml b/private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/buf.yaml new file mode 100644 index 0000000000..b03e3f8a6d --- /dev/null +++ b/private/bufpkg/bufcheck/testdata/lint/comment_ignores_with_trailing_comment/buf.yaml @@ -0,0 +1,4 @@ +version: v2 +lint: + use: + - PACKAGE_DIRECTORY_MATCH From a2d58ba3923e386a604a3bda8c71eb611147ab8a Mon Sep 17 00:00:00 2001 From: Oliver Sun Date: Fri, 6 Sep 2024 14:18:19 -0400 Subject: [PATCH 2/5] update comment --- private/bufpkg/bufcheck/client.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/private/bufpkg/bufcheck/client.go b/private/bufpkg/bufcheck/client.go index cee078372c..ae54f0872a 100644 --- a/private/bufpkg/bufcheck/client.go +++ b/private/bufpkg/bufcheck/client.go @@ -491,13 +491,14 @@ func ignoreLocation( } // checkCommentLineForCheckIgnore checks that the comment line starts with the configured -// comment ignore prefix, a number of spaces (at least one), the ruleID of the check. +// comment ignore prefix, a space and the ruleID of the check. // -// All of the following comments ignore SERVICE_PASCAL_CASE and this rule only: +// All of the following comments are valid, ignoring SERVICE_PASCAL_CASE and this rule only: // // // buf:lint:ignore SERVICE_PASCAL_CASE, SERVICE_SUFFIX // // buf:lint:ignore SERVICE_PASCAL_CASE -// // buf:lint:ignore SERVICE_PASCAL_CASE some other comment +// // buf:lint:ignore SERVICE_PASCAL_CASEsome other comment +// // buf:lint:ignore SERVICE_PASCAL_CASE some other comment // // While the following is invalid and a nop // From f516a9c597e110b619aed297cde8d96e723b3f3a Mon Sep 17 00:00:00 2001 From: Oliver Sun Date: Fri, 6 Sep 2024 14:22:59 -0400 Subject: [PATCH 3/5] fix comment --- private/bufpkg/bufcheck/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/private/bufpkg/bufcheck/client.go b/private/bufpkg/bufcheck/client.go index ae54f0872a..b258823a92 100644 --- a/private/bufpkg/bufcheck/client.go +++ b/private/bufpkg/bufcheck/client.go @@ -495,7 +495,7 @@ func ignoreLocation( // // All of the following comments are valid, ignoring SERVICE_PASCAL_CASE and this rule only: // -// // buf:lint:ignore SERVICE_PASCAL_CASE, SERVICE_SUFFIX +// // buf:lint:ignore SERVICE_PASCAL_CASE, SERVICE_SUFFIX (only SERVICE_PASCAL_CASE is ignored) // // buf:lint:ignore SERVICE_PASCAL_CASE // // buf:lint:ignore SERVICE_PASCAL_CASEsome other comment // // buf:lint:ignore SERVICE_PASCAL_CASE some other comment From 7406b5dd83dd466b50686a4d5f102faad67d0f02 Mon Sep 17 00:00:00 2001 From: Oliver Sun Date: Fri, 6 Sep 2024 15:33:18 -0400 Subject: [PATCH 4/5] add changelog entry --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6426e33795..989d9463d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## [Unreleased] -- No changes yet. +- Fix issue with `buf lint` where comment ignores in the shape of `// buf:lint:ignore ` + are not recognized due to the extra comment. ## [v1.40.0] - 2024-09-04 From 78c78e6e49151ff5d133f49ab14533deae75336b Mon Sep 17 00:00:00 2001 From: Oliver Sun Date: Fri, 6 Sep 2024 17:17:35 -0400 Subject: [PATCH 5/5] fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 989d9463d2..2de689fa9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## [Unreleased] - Fix issue with `buf lint` where comment ignores in the shape of `// buf:lint:ignore ` - are not recognized due to the extra comment. + were not recognized due to the extra comment. ## [v1.40.0] - 2024-09-04