Skip to content

Commit

Permalink
Add protoglobal.DisableGlobalFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Sep 25, 2024
1 parent ee73df5 commit 6f725ad
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions private/buf/cmd/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
2 changes: 2 additions & 0 deletions private/buf/cmd/protoc-gen-buf-breaking/breaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions private/buf/cmd/protoc-gen-buf-lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions private/pkg/protoglobal/protoglobal.go
Original file line number Diff line number Diff line change
@@ -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()
}
19 changes: 19 additions & 0 deletions private/pkg/protoglobal/usage.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f725ad

Please sign in to comment.