Skip to content
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

Add protovalidate lint rules #2473

Merged
merged 39 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
40e989e
basic
oliversun9 Sep 27, 2023
8bf543b
comment
oliversun9 Sep 27, 2023
2afea78
make protosource.File expose a protodescriptor.FileDescriptor
oliversun9 Sep 28, 2023
9de109d
fix type loading for field
oliversun9 Sep 29, 2023
0bfcf63
remove remote package, generate validate.pb.go locally
oliversun9 Sep 29, 2023
9833c7c
name
oliversun9 Sep 29, 2023
3698f50
pointer
oliversun9 Sep 29, 2023
aa2ef7d
style
oliversun9 Sep 29, 2023
c1aee25
test
oliversun9 Oct 2, 2023
abc0e8d
field name
oliversun9 Oct 2, 2023
5119bce
test
oliversun9 Oct 2, 2023
3dc3dbf
use celext
oliversun9 Oct 2, 2023
69f9cba
lint
oliversun9 Oct 2, 2023
f84b653
Merge branch 'main' into osun/buf-lint-cel
oliversun9 Oct 2, 2023
3d9d2e5
comment
oliversun9 Oct 2, 2023
242a97d
doc
oliversun9 Oct 2, 2023
662bbbc
use protovalidate-go v0.3.3
oliversun9 Oct 3, 2023
5e33b02
use DefaultResolver from protovalidate-go, which in turn requires mov…
oliversun9 Oct 3, 2023
7188999
small refactor
oliversun9 Oct 3, 2023
59e3caf
remove dynamic extension types as they are no longer needed
oliversun9 Oct 3, 2023
3437aaa
rename
oliversun9 Oct 3, 2023
1215364
commit
bufdev Oct 3, 2023
c1016d2
commit
bufdev Oct 3, 2023
54d2489
commit
bufdev Oct 3, 2023
c15c65a
Merge branch 'main' into osun/buf-lint-cel
bufdev Oct 3, 2023
e842c9a
commit
bufdev Oct 3, 2023
3b32cde
add test for parsing error texts from cel-go; comments
oliversun9 Oct 4, 2023
cc10191
lint
oliversun9 Oct 4, 2023
13c2ebd
Merge branch 'main' into osun/buf-lint-cel
oliversun9 Oct 4, 2023
30a07fc
rename sub-directory for testcase
oliversun9 Oct 5, 2023
05e46db
use new dir in test
oliversun9 Oct 5, 2023
0ce125c
dont allow cel expressions on fields of type google.protobuf.Any
oliversun9 Oct 18, 2023
c9bef9d
fix make upgrade
bufdev Oct 18, 2023
a66aaa7
merge
bufdev Oct 18, 2023
a33ac0d
Add support for linting protovalidate rules (#2505)
oliversun9 Oct 31, 2023
8b474dc
fix comment
oliversun9 Oct 31, 2023
da8f4b0
merge main
oliversun9 Oct 31, 2023
742558f
bump protovalidate to v0.5.1
oliversun9 Oct 31, 2023
8aa53b8
update links
oliversun9 Oct 31, 2023
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
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module github.com/bufbuild/buf
go 1.19

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.31.0-20230914171853-63dfe56cc2c4.1
connectrpc.com/connect v1.11.1
connectrpc.com/otelconnect v0.5.0
github.com/bufbuild/protocompile v0.6.0
github.com/bufbuild/protovalidate-go v0.3.4-0.20231003193126-05525acfab0c
github.com/docker/docker v24.0.6+incompatible
github.com/go-chi/chi/v5 v5.0.10
github.com/gofrs/flock v0.8.1
github.com/gofrs/uuid/v5 v5.0.0
github.com/google/cel-go v0.18.1
github.com/google/go-cmp v0.5.9
github.com/google/go-containerregistry v0.16.1
github.com/jdx/go-netrc v1.0.0
Expand Down Expand Up @@ -43,6 +46,7 @@ require (
require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
Expand All @@ -68,8 +72,15 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
go.opentelemetry.io/otel/metric v1.17.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230913181813-007df8e322eb // indirect
)

// The Buf CLI is the one library that should not depend on BSR generated SDKs
// as this could cause bootstrapping errors.
replace buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate => ./private/gen/proto/go/buf/validate
Copy link
Member

Choose a reason for hiding this comment

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

Is this true? What bootstrapping errors do we expect? If we're using the buf CLI to work on buf, we're working with a binary where the remote packages are already resolved and compiled, so I don't see how this is a problem here.

I was under the impression that it's the core repo that must not depend on generated SDKs. Note that this directive here will do nothing to prevent a cyclic dependency in core since replace directives in dependencies are ignored by the go tool.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we're working with a binary where the remote packages are already resolved and compiled

The buf CLI's binary itself will be fine, since like you said, it's just a binary.

Note that this directive here will do nothing to prevent a cyclic dependency in core since replace directives in dependencies are ignored by the go tool.

Yes I think this is the concern and we will also need to add a replace directive there, before or around the time when this PR is merged.

Copy link
Member

Choose a reason for hiding this comment

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

Yes I think this is the concern

So we can remove the replace directive here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great question, and with the reasoning above I think it's probably fine? @bufdev what do you think?

17 changes: 16 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.31.0-20230914171853-63dfe56cc2c4.1 h1:2gmp+PRca1fqQHf/WMKOgu9inVb0R0N07TucgY3QZCQ=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.31.0-20230914171853-63dfe56cc2c4.1/go.mod h1:xafc+XIsTxTy76GJQ1TKgvJWsSugFBqMaN27WhUblew=
connectrpc.com/connect v1.11.1 h1:dqRwblixqkVh+OFBOOL1yIf1jS/yP0MSJLijRj29bFg=
connectrpc.com/connect v1.11.1/go.mod h1:3AGaO6RRGMx5IKFfqbe3hvK1NqLosFNP2BxDYTPmNPo=
connectrpc.com/otelconnect v0.5.0 h1:K7xQKFbgeaHx563B+IIbd1EJe856AanueIYtGEtdnH8=
Expand All @@ -6,10 +8,14 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 h1:goHVqTbFX3AIo0tzGr14pgfAW2ZfPChKO21Z9MGf/gk=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bufbuild/protocompile v0.6.0 h1:Uu7WiSQ6Yj9DbkdnOe7U4mNKp58y9WDMKDn28/ZlunY=
github.com/bufbuild/protocompile v0.6.0/go.mod h1:YNP35qEYoYGme7QMtz5SBCoN4kL4g12jTtjuzRNdjpE=
github.com/bufbuild/protovalidate-go v0.3.4-0.20231003193126-05525acfab0c h1:yLwpjZBhwOMErTl88t+CurnuuJR+cVaubEoDZYCwA4U=
github.com/bufbuild/protovalidate-go v0.3.4-0.20231003193126-05525acfab0c/go.mod h1:36yOYnOgeU1gtdIC/J+SKt+jww0gXMW5j/KZzgYuOJU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -51,6 +57,8 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/cel-go v0.18.1 h1:V/lAXKq4C3BYLDy/ARzMtpkEEYfHQpZzVyzy69nEUjs=
github.com/google/cel-go v0.18.1/go.mod h1:PVAybmSnWkNMUZR/tEWFUiJ1Np4Hz0MHsZJcgC4zln4=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down Expand Up @@ -102,11 +110,15 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tetratelabs/wazero v1.5.0 h1:Yz3fZHivfDiZFUXnWMPUoiW7s8tC1sjdBtlJn08qYa0=
Expand Down Expand Up @@ -179,7 +191,10 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM=
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb h1:lK0oleSc7IQsUxO3U5TjL9DWlsxpEBemh+zpB7IqhWI=
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230913181813-007df8e322eb h1:Isk1sSH7bovx8Rti2wZK0UZF6oraBDK74uoyLEEVFN0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230913181813-007df8e322eb/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
Expand Down
18 changes: 16 additions & 2 deletions make/buf/all.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ LICENSE_HEADER_LICENSE_TYPE := apache
LICENSE_HEADER_COPYRIGHT_HOLDER := Buf Technologies, Inc.
LICENSE_HEADER_YEAR_RANGE := 2020-2023
LICENSE_HEADER_IGNORES := \/testdata enterprise
PROTOVALIDATE_VERSION := v0.4.3
# Comment out to use released buf
BUF_GO_INSTALL_PATH := ./cmd/buf

Expand Down Expand Up @@ -99,20 +100,33 @@ bufgeneratedeps:: \
bufgeneratecleango:
rm -rf private/gen/proto

bufgenerateclean:: bufgeneratecleango
.PHONY: bufgeneratecleanbuflinttestdata
bufgeneratecleanbuflinttestdata:
rm -rf private/bufpkg/bufcheck/buflint/testdata/deps

bufgenerateclean:: \
bufgeneratecleango \
bufgeneratecleanbuflinttestdata

.PHONY: bufgenerateprotogo
bufgenerateprotogo:
$(BUF_BIN) generate proto --template data/template/buf.go.gen.yaml
$(BUF_BIN) generate buf.build/grpc/grpc --type grpc.reflection.v1.ServerReflection --template data/template/buf.go.gen.yaml
$(BUF_BIN) generate buf.build/bufbuild/protovalidate:$(PROTOVALIDATE_VERSION) --template data/template/buf.go.gen.yaml
echo module protovalidate > private/gen/proto/go/buf/validate/go.mod

.PHONY: bufgenerateprotogoclient
bufgenerateprotogoclient:
$(BUF_BIN) generate proto --template data/template/buf.go-client.gen.yaml

.PHONY: bufgeneratebuflinttestdata
bufgeneratebuflinttestdata:
$(BUF_BIN) export buf.build/bufbuild/protovalidate:$(PROTOVALIDATE_VERSION) --output private/bufpkg/bufcheck/buflint/testdata/deps/protovalidate

bufgeneratesteps:: \
bufgenerateprotogo \
bufgenerateprotogoclient
bufgenerateprotogoclient \
bufgeneratebuflinttestdata

.PHONY: bufrelease
bufrelease: $(MINISIGN)
Expand Down
1 change: 1 addition & 0 deletions private/buf/cmd/buf/buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ ENUM_VALUE_PREFIX DEFAULT Checks that enum valu
ENUM_ZERO_VALUE_SUFFIX DEFAULT Checks that enum zero values are suffixed with _UNSPECIFIED (suffix is configurable).
FILE_LOWER_SNAKE_CASE DEFAULT Checks that filenames are lower_snake_case.
PACKAGE_VERSION_SUFFIX DEFAULT Checks that the last component of all packages is a version of the form v\d+, v\d+test.*, v\d+(alpha|beta)\d+, or v\d+p\d+(alpha|beta)\d+, where numbers are >=1.
PROTOVALIDATE_CEL DEFAULT Checks that protovalidate CEL expressions compile.
RPC_REQUEST_RESPONSE_UNIQUE DEFAULT Checks that RPC request and response types are only used in one RPC (configurable).
RPC_REQUEST_STANDARD_NAME DEFAULT Checks that RPC request type names are RPCNameRequest or ServiceNameRPCNameRequest (configurable).
RPC_RESPONSE_STANDARD_NAME DEFAULT Checks that RPC response type names are RPCNameResponse or ServiceNameRPCNameResponse (configurable).
Expand Down
64 changes: 58 additions & 6 deletions private/bufpkg/bufcheck/buflint/buflint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ func TestRunPackageNoImportCycle(t *testing.T) {
require.NoError(t, err)
return newImage
},
"",
bufanalysistesting.NewFileAnnotation(t, "c1.proto", 5, 1, 5, 19, "PACKAGE_NO_IMPORT_CYCLE"),
bufanalysistesting.NewFileAnnotation(t, "d1.proto", 5, 1, 5, 19, "PACKAGE_NO_IMPORT_CYCLE"),
)
Expand Down Expand Up @@ -555,6 +556,21 @@ func TestRunPackageVersionSuffix(t *testing.T) {
)
}

func TestRunProtovalidateCel(t *testing.T) {
t.Parallel()
testLintWithValidate(
t,
"validate_cel_expressions_compile",
bufanalysistesting.NewFileAnnotation(t, "a.proto", 9, 37, 13, 4, "PROTOVALIDATE_CEL"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 16, 5, 20, 6, "PROTOVALIDATE_CEL"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 28, 5, 32, 6, "PROTOVALIDATE_CEL"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 34, 39, 38, 4, "PROTOVALIDATE_CEL"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 59, 3, 63, 5, "PROTOVALIDATE_CEL"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 75, 5, 79, 6, "PROTOVALIDATE_CEL"),
bufanalysistesting.NewFileAnnotation(t, "a.proto", 87, 5, 91, 6, "PROTOVALIDATE_CEL"),
)
}

func TestRunRPCNoStreaming(t *testing.T) {
t.Parallel()
testLint(
Expand Down Expand Up @@ -896,6 +912,7 @@ func TestCommentIgnoresOn(t *testing.T) {
config.Lint.AllowCommentIgnores = true
},
nil,
"",
)
}

Expand Down Expand Up @@ -949,6 +966,7 @@ func TestCommentIgnoresCascadeOn(t *testing.T) {
config.Lint.AllowCommentIgnores = true
},
nil,
"",
)
}

Expand All @@ -962,6 +980,24 @@ func testLint(
relDirPath,
nil,
nil,
"",
expectedFileAnnotations...,
)
}

func testLintWithValidate(
t *testing.T,
relDirPath string,
expectedFileAnnotations ...bufanalysis.FileAnnotation,
) {
testLintWithModifiers(
t,
relDirPath,
func(config *bufconfig.Config) {
config.Lint.IgnoreRootPaths = []string{"buf/validate"}
},
nil,
"deps/protovalidate",
expectedFileAnnotations...,
)
}
Expand All @@ -971,6 +1007,7 @@ func testLintWithModifiers(
relDirPath string,
configModifier func(*bufconfig.Config),
imageModifier func(bufimage.Image) bufimage.Image,
dependencyPathPrefix string,
expectedFileAnnotations ...bufanalysis.FileAnnotation,
) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand All @@ -991,12 +1028,27 @@ func testLintWithModifiers(
configModifier(config)
}

module, err := bufmodulebuild.NewModuleBucketBuilder().BuildForBucket(
context.Background(),
readWriteBucket,
config.Build,
)
require.NoError(t, err)
var module *bufmodulebuild.BuiltModule
if dependencyPathPrefix != "" {
dependencyReadWriteBucket, err := storageosProvider.NewReadWriteBucket(
filepath.Join("testdata", dependencyPathPrefix),
storageos.ReadWriteBucketWithSymlinksIfSupported(),
)
require.NoError(t, err)
module, err = bufmodulebuild.NewModuleBucketBuilder().BuildForBucket(
context.Background(),
storage.MultiReadBucket(dependencyReadWriteBucket, readWriteBucket),
config.Build,
)
require.NoError(t, err)
} else {
module, err = bufmodulebuild.NewModuleBucketBuilder().BuildForBucket(
context.Background(),
readWriteBucket,
config.Build,
)
require.NoError(t, err)
}
image, fileAnnotations, err := bufimagebuild.NewBuilder(
zap.NewNop(),
bufmodule.NewNopModuleReader(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ var (
`the last component of all packages is a version of the form v\d+, v\d+test.*, v\d+(alpha|beta)\d+, or v\d+p\d+(alpha|beta)\d+, where numbers are >=1`,
newAdapter(buflintcheck.CheckPackageVersionSuffix),
)
// ProtovalidateCelRuleBuilder is a rule builder.
ProtovalidateCelRuleBuilder = internal.NewNopRuleBuilder(
"PROTOVALIDATE_CEL",
"protovalidate CEL expressions compile",
newAdapter(buflintcheck.CheckProtovalidateCel),
)
// RPCNoClientStreamingRuleBuilder is a rule builder.
RPCNoClientStreamingRuleBuilder = internal.NewNopRuleBuilder(
"RPC_NO_CLIENT_STREAMING",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import (
"strings"

"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
"github.com/bufbuild/buf/private/bufpkg/bufcheck/buflint/internal/buflintvalidate"
"github.com/bufbuild/buf/private/bufpkg/bufcheck/internal"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/protodescriptor"
"github.com/bufbuild/buf/private/pkg/protosource"
"github.com/bufbuild/buf/private/pkg/protoversion"
"github.com/bufbuild/buf/private/pkg/stringutil"
"google.golang.org/protobuf/reflect/protodesc"
)

const (
Expand Down Expand Up @@ -627,6 +630,28 @@ func checkPackageVersionSuffix(add addFunc, file protosource.File) error {
return nil
}

// CheckProtovalidateCel is a check function.
func CheckProtovalidateCel(id string, ignoreFunc internal.IgnoreFunc, files []protosource.File) ([]bufanalysis.FileAnnotation, error) {
fileDescriptors := make([]protodescriptor.FileDescriptor, 0, len(files))
for _, file := range files {
fileDescriptors = append(fileDescriptors, file.FileDescriptor())
}
resolver, err := protodesc.NewFiles(protodescriptor.FileDescriptorSetForFileDescriptors(fileDescriptors...))
if err != nil {
return nil, err
}
helper := internal.NewHelper(id, ignoreFunc)
for _, file := range files {
if file.IsImport() {
continue
}
if err := buflintvalidate.ValidateCELCompiles(resolver, helper.AddFileAnnotationWithExtraIgnoreLocationsf, file); err != nil {
return nil, err
}
}
return helper.FileAnnotations(), nil
}

// CheckRPCNoClientStreaming is a check function.
var CheckRPCNoClientStreaming = newMethodCheckFunc(checkRPCNoClientStreaming)

Expand Down
4 changes: 4 additions & 0 deletions private/bufpkg/bufcheck/buflint/internal/buflintv1/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
buflintbuild.PackageSameRubyPackageRuleBuilder,
buflintbuild.PackageSameSwiftPrefixRuleBuilder,
buflintbuild.PackageVersionSuffixRuleBuilder,
buflintbuild.ProtovalidateCelRuleBuilder,
buflintbuild.RPCNoClientStreamingRuleBuilder,
buflintbuild.RPCNoServerStreamingRuleBuilder,
buflintbuild.RPCPascalCaseRuleBuilder,
Expand Down Expand Up @@ -199,6 +200,9 @@ var (
"PACKAGE_VERSION_SUFFIX": {
"DEFAULT",
},
"PROTOVALIDATE_CEL": {
"DEFAULT",
},
"RPC_NO_CLIENT_STREAMING": {
"UNARY_RPC",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buflintvalidate

import (
"github.com/bufbuild/buf/private/pkg/protosource"
"google.golang.org/protobuf/reflect/protodesc"
)

// ValidateCELCompiles validates that all CEL expressions defined for protovalidate
// in the given file compile.
func ValidateCELCompiles(
resolver protodesc.Resolver,
add func(protosource.Descriptor, protosource.Location, []protosource.Location, string, ...interface{}),
file protosource.File,
) error {
for _, message := range file.Messages() {
if err := validateCELCompilesMessage(resolver, add, message); err != nil {
return err
}
}
for _, extensionField := range file.Extensions() {
if err := validateCELCompilesField(resolver, add, extensionField); err != nil {
return err
}
}
return nil
}
Loading