diff --git a/CHANGELOG.md b/CHANGELOG.md index 6426e33795..2de689fa9e 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 ` + were not recognized due to the extra comment. ## [v1.40.0] - 2024-09-04 diff --git a/private/bufpkg/bufcheck/client.go b/private/bufpkg/bufcheck/client.go index 6cc3d2924d..b258823a92 100644 --- a/private/bufpkg/bufcheck/client.go +++ b/private/bufpkg/bufcheck/client.go @@ -491,27 +491,25 @@ 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 space and the ruleID of the check. // -// We currently do not support multiple rules per comment ignore: +// All of the following comments are valid, ignoring SERVICE_PASCAL_CASE and this rule only: // -// Invalid: -// // buf:lint:ignore SERVICE_SUFFIX, SERVICE_PASCAL_CASE +// // 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 // -// 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 4fb9c98129..ad62fe5c08 100644 --- a/private/bufpkg/bufcheck/lint_test.go +++ b/private/bufpkg/bufcheck/lint_test.go @@ -1189,6 +1189,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