diff --git a/private/buf/cmd/buf/buf.go b/private/buf/cmd/buf/buf.go index c7cfd144db..cd9e3ea68b 100644 --- a/private/buf/cmd/buf/buf.go +++ b/private/buf/cmd/buf/buf.go @@ -89,11 +89,13 @@ import ( "github.com/bufbuild/buf/private/pkg/app" "github.com/bufbuild/buf/private/pkg/app/appcmd" "github.com/bufbuild/buf/private/pkg/app/appext" + "github.com/bufbuild/buf/private/pkg/protoglobal" "github.com/bufbuild/buf/private/pkg/syserror" ) // Main is the entrypoint to the buf CLI. func Main(name string) { + protoglobal.DisableGlobalFiles() appcmd.Main(context.Background(), NewRootCommand(name)) } diff --git a/private/buf/cmd/protoc-gen-buf-breaking/breaking.go b/private/buf/cmd/protoc-gen-buf-breaking/breaking.go index ec6f1d4bdf..8868b09a1b 100644 --- a/private/buf/cmd/protoc-gen-buf-breaking/breaking.go +++ b/private/buf/cmd/protoc-gen-buf-breaking/breaking.go @@ -32,6 +32,7 @@ import ( "github.com/bufbuild/buf/private/pkg/encoding" "github.com/bufbuild/buf/private/pkg/protodescriptor" "github.com/bufbuild/buf/private/pkg/protoencoding" + "github.com/bufbuild/buf/private/pkg/protoglobal" "github.com/bufbuild/buf/private/pkg/tracing" "github.com/bufbuild/protoplugin" ) @@ -43,6 +44,7 @@ const ( // Main is the main. func Main() { + protoglobal.DisableGlobalFiles() protoplugin.Main( protoplugin.HandlerFunc(handle), // An `EmptyResolver` is passed to protoplugin for unmarshalling instead of defaulting to diff --git a/private/buf/cmd/protoc-gen-buf-lint/lint.go b/private/buf/cmd/protoc-gen-buf-lint/lint.go index 8f86b0ba47..e5c0d6ffc4 100644 --- a/private/buf/cmd/protoc-gen-buf-lint/lint.go +++ b/private/buf/cmd/protoc-gen-buf-lint/lint.go @@ -31,6 +31,7 @@ import ( "github.com/bufbuild/buf/private/pkg/encoding" "github.com/bufbuild/buf/private/pkg/protodescriptor" "github.com/bufbuild/buf/private/pkg/protoencoding" + "github.com/bufbuild/buf/private/pkg/protoglobal" "github.com/bufbuild/buf/private/pkg/tracing" "github.com/bufbuild/protoplugin" ) @@ -42,6 +43,7 @@ const ( // Main is the main. func Main() { + protoglobal.DisableGlobalFiles() protoplugin.Main( protoplugin.HandlerFunc(handle), // An `EmptyResolver` is passed to protoplugin for unmarshalling instead of defaulting to diff --git a/private/pkg/protoglobal/protoglobal.go b/private/pkg/protoglobal/protoglobal.go new file mode 100644 index 0000000000..99ef71ab91 --- /dev/null +++ b/private/pkg/protoglobal/protoglobal.go @@ -0,0 +1,34 @@ +// Copyright 2020-2024 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 protoglobal + +import ( + "sync" + + "google.golang.org/protobuf/reflect/protoregistry" +) + +var lock sync.Mutex + +// DisableGlobalFiles disables protoregistry.GlobalFiles by overwriting it with +// an empty Files struct. +// +// This should not be called in init functions, and should only be called at +// the start of applications. +func DisableGlobalFiles() { + lock.Lock() + protoregistry.GlobalFiles = new(protoregistry.Files) + lock.Unlock() +} diff --git a/private/pkg/protoglobal/usage.gen.go b/private/pkg/protoglobal/usage.gen.go new file mode 100644 index 0000000000..667a4d8456 --- /dev/null +++ b/private/pkg/protoglobal/usage.gen.go @@ -0,0 +1,19 @@ +// Copyright 2020-2024 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. + +// Generated. DO NOT EDIT. + +package protoglobal + +import _ "github.com/bufbuild/buf/private/usage"