Skip to content

Commit 10ddcf5

Browse files
authored
Add bufplugin package (#3431)
This adds a new package bufplugin which implements types and functions for operating on Buf plugins.
1 parent dae748f commit 10ddcf5

File tree

114 files changed

+1794
-1004
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+1794
-1004
lines changed

private/buf/bufcli/errors.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020

2121
"connectrpc.com/connect"
22-
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
22+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
2323
)
2424

2525
var (
@@ -72,14 +72,14 @@ func NewModuleNotFoundError(name string) error {
7272
return fmt.Errorf(`a module named %q does not exist, use "buf registry module create" to create one`, name)
7373
}
7474

75-
// NewModuleRefNotFoundError informs the user that a ModuleRef does not exist.
76-
func NewModuleRefNotFoundError(moduleRef bufmodule.ModuleRef) error {
77-
return fmt.Errorf("%q does not exist", moduleRef)
75+
// NewRefNotFoundError informs the user that a Ref does not exist.
76+
func NewRefNotFoundError(ref bufparse.Ref) error {
77+
return fmt.Errorf("%q does not exist", ref)
7878
}
7979

80-
// NewLabelNotFoundError informs the user that a ModuleRef does not exist as a label.
81-
func NewLabelNotFoundError(moduleRef bufmodule.ModuleRef) error {
82-
return fmt.Errorf("label %q does not exist", moduleRef)
80+
// NewLabelNotFoundError informs the user that a Ref does not exist as a label.
81+
func NewLabelNotFoundError(ref bufparse.Ref) error {
82+
return fmt.Errorf("label %q does not exist", ref)
8383
}
8484

8585
// NewTokenNotFoundError informs the user that a token with

private/buf/bufctl/controller.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/bufbuild/buf/private/bufpkg/bufimage"
3434
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimageutil"
3535
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
36+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
3637
"github.com/bufbuild/buf/private/bufpkg/bufreflect"
3738
"github.com/bufbuild/buf/private/gen/data/datawkt"
3839
imagev1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/image/v1"
@@ -1085,39 +1086,39 @@ func (c *controller) warnUnconfiguredTransitiveImports(
10851086
if slicesext.Count(workspace.Modules(), bufmodule.Module.IsLocal) == 0 {
10861087
return nil
10871088
}
1088-
// Construct a struct map of all the ModuleFullName strings of the configured buf.yaml
1089+
// Construct a struct map of all the FullName strings of the configured buf.yaml
10891090
// Module dependencies, and the local Modules. These are considered OK to depend on
10901091
// for non-imports in the Image.
1091-
configuredModuleFullNameStrings, err := slicesext.MapError(
1092+
configuredFullNameStrings, err := slicesext.MapError(
10921093
workspace.ConfiguredDepModuleRefs(),
1093-
func(moduleRef bufmodule.ModuleRef) (string, error) {
1094-
moduleFullName := moduleRef.ModuleFullName()
1094+
func(moduleRef bufparse.Ref) (string, error) {
1095+
moduleFullName := moduleRef.FullName()
10951096
if moduleFullName == nil {
1096-
return "", syserror.New("ModuleFullName nil on ModuleRef")
1097+
return "", syserror.New("FullName nil on ModuleRef")
10971098
}
10981099
return moduleFullName.String(), nil
10991100
},
11001101
)
11011102
if err != nil {
11021103
return err
11031104
}
1104-
configuredModuleFullNameStringMap := slicesext.ToStructMap(configuredModuleFullNameStrings)
1105+
configuredFullNameStringMap := slicesext.ToStructMap(configuredFullNameStrings)
11051106
for _, localModule := range bufmodule.ModuleSetLocalModules(workspace) {
1106-
if moduleFullName := localModule.ModuleFullName(); moduleFullName != nil {
1107-
configuredModuleFullNameStringMap[moduleFullName.String()] = struct{}{}
1107+
if moduleFullName := localModule.FullName(); moduleFullName != nil {
1108+
configuredFullNameStringMap[moduleFullName.String()] = struct{}{}
11081109
}
11091110
}
11101111

1111-
// Construct a map from Image file path -> ModuleFullName string.
1112+
// Construct a map from Image file path -> FullName string.
11121113
//
1113-
// If a given file in the Image did not have a ModuleFullName, it came from a local unnamed Module
1114+
// If a given file in the Image did not have a FullName, it came from a local unnamed Module
11141115
// in the Workspace, and we're safe to ignore it with respect to calculating the undeclared
11151116
// transitive imports.
1116-
pathToModuleFullNameString := make(map[string]string)
1117+
pathToFullNameString := make(map[string]string)
11171118
for _, imageFile := range image.Files() {
11181119
// If nil, this came from a local unnamed Module in the Workspace, and we're safe to ignore.
1119-
if moduleFullName := imageFile.ModuleFullName(); moduleFullName != nil {
1120-
pathToModuleFullNameString[imageFile.Path()] = moduleFullName.String()
1120+
if moduleFullName := imageFile.FullName(); moduleFullName != nil {
1121+
pathToFullNameString[imageFile.Path()] = moduleFullName.String()
11211122
}
11221123
}
11231124

@@ -1127,12 +1128,12 @@ func (c *controller) warnUnconfiguredTransitiveImports(
11271128
continue
11281129
}
11291130
for _, importPath := range imageFile.FileDescriptorProto().GetDependency() {
1130-
moduleFullNameString, ok := pathToModuleFullNameString[importPath]
1131+
moduleFullNameString, ok := pathToFullNameString[importPath]
11311132
if !ok {
11321133
// The import was from a local unnamed Module in the Workspace.
11331134
continue
11341135
}
1135-
if _, ok := configuredModuleFullNameStringMap[moduleFullNameString]; !ok {
1136+
if _, ok := configuredFullNameStringMap[moduleFullNameString]; !ok {
11361137
c.logger.Warn(fmt.Sprintf(
11371138
`File %q imports %q, which is not in your workspace or in the dependencies declared in your buf.yaml, but is found in transitive dependency %q.
11381139
Declare %q in the deps key in your buf.yaml.`,

private/buf/buffetch/internal/internal.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/bufbuild/buf/private/buf/buftarget"
2626
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
2727
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
28+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
2829
"github.com/bufbuild/buf/private/pkg/app"
2930
"github.com/bufbuild/buf/private/pkg/git"
3031
"github.com/bufbuild/buf/private/pkg/httpauth"
@@ -223,7 +224,7 @@ func NewGitRef(
223224
// ModuleRef is a module reference.
224225
type ModuleRef interface {
225226
Ref
226-
ModuleRef() bufmodule.ModuleRef
227+
ModuleRef() bufparse.Ref
227228
moduleRef()
228229
}
229230

@@ -375,7 +376,7 @@ type ParsedModuleRef interface {
375376
// This should only be used for testing.
376377
func NewDirectParsedModuleRef(
377378
format string,
378-
moduleRef bufmodule.ModuleRef,
379+
moduleRef bufparse.Ref,
379380
) ParsedModuleRef {
380381
return newDirectModuleRef(
381382
format,

private/buf/buffetch/internal/module_ref.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package internal
1717
import (
1818
"strings"
1919

20-
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
20+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
2121
"github.com/bufbuild/buf/private/pkg/app"
2222
)
2323

@@ -27,7 +27,7 @@ var (
2727

2828
type moduleRef struct {
2929
format string
30-
iModuleRef bufmodule.ModuleRef
30+
iModuleRef bufparse.Ref
3131
}
3232

3333
func newModuleRef(
@@ -46,15 +46,15 @@ func newModuleRef(
4646
if strings.Contains(path, "://") {
4747
return nil, NewInvalidPathError(format, path)
4848
}
49-
moduleRef, err := bufmodule.ParseModuleRef(path)
49+
moduleRef, err := bufparse.ParseRef(path)
5050
if err != nil {
5151
// TODO: this is dumb
5252
return nil, NewInvalidPathError(format, path)
5353
}
5454
return newDirectModuleRef(format, moduleRef), nil
5555
}
5656

57-
func newDirectModuleRef(format string, iModuleRef bufmodule.ModuleRef) *moduleRef {
57+
func newDirectModuleRef(format string, iModuleRef bufparse.Ref) *moduleRef {
5858
return &moduleRef{
5959
format: format,
6060
iModuleRef: iModuleRef,
@@ -65,7 +65,7 @@ func (r *moduleRef) Format() string {
6565
return r.format
6666
}
6767

68-
func (r *moduleRef) ModuleRef() bufmodule.ModuleRef {
68+
func (r *moduleRef) ModuleRef() bufparse.Ref {
6969
return r.iModuleRef
7070
}
7171

private/buf/buffetch/internal/reader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/bufbuild/buf/private/buf/buftarget"
3030
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
31+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
3132
"github.com/bufbuild/buf/private/pkg/app"
3233
"github.com/bufbuild/buf/private/pkg/git"
3334
"github.com/bufbuild/buf/private/pkg/httpauth"
@@ -397,7 +398,7 @@ func (r *reader) getModuleKey(
397398
}
398399
moduleKeys, err := r.moduleKeyProvider.GetModuleKeysForModuleRefs(
399400
ctx,
400-
[]bufmodule.ModuleRef{moduleRef.ModuleRef()},
401+
[]bufparse.Ref{moduleRef.ModuleRef()},
401402
bufmodule.DigestTypeB5,
402403
)
403404
if err != nil {

private/buf/buffetch/ref_parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
"github.com/bufbuild/buf/private/buf/buffetch/internal"
2727
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
28-
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
28+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
2929
"github.com/bufbuild/buf/private/pkg/app"
3030
"github.com/bufbuild/buf/private/pkg/syserror"
3131
)
@@ -837,7 +837,7 @@ func assumeModuleOrDir(path string) (string, error) {
837837
if path == "" {
838838
return "", errors.New("assumeModuleOrDir: no path given")
839839
}
840-
if _, err := bufmodule.ParseModuleRef(path); err == nil {
840+
if _, err := bufparse.ParseRef(path); err == nil {
841841
// this is possible to be a module, check if it is a directory though
842842
// OK to use os.Stat instead of os.Lstat here
843843
fileInfo, err := os.Stat(path)

private/buf/buffetch/ref_parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/bufbuild/buf/private/buf/buffetch/internal"
24-
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
24+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
2525
"github.com/bufbuild/buf/private/pkg/app"
2626
"github.com/bufbuild/buf/private/pkg/git"
2727
"github.com/bufbuild/buf/private/pkg/normalpath"
@@ -1442,8 +1442,8 @@ func testNewModuleRef(
14421442
owner string,
14431443
name string,
14441444
ref string,
1445-
) bufmodule.ModuleRef {
1446-
moduleRef, err := bufmodule.NewModuleRef(registry, owner, name, ref)
1445+
) bufparse.Ref {
1446+
moduleRef, err := bufparse.NewRef(registry, owner, name, ref)
14471447
require.NoError(t, err)
14481448
return moduleRef
14491449
}

private/buf/buflsp/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func (f *file) RunLints(ctx context.Context) bool {
575575
return false
576576
}
577577

578-
f.lsp.logger.Debug(fmt.Sprintf("running lint for %q in %v", f.uri, module.ModuleFullName()))
578+
f.lsp.logger.Debug(fmt.Sprintf("running lint for %q in %v", f.uri, module.FullName()))
579579

580580
lintConfig := workspace.GetLintConfigForOpaqueID(module.OpaqueID())
581581
err := f.lsp.checkClient.Lint(

private/buf/buflsp/symbol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func (s *symbol) FormatDocs(ctx context.Context) string {
471471
if def.file.IsWKT() {
472472
bsrHost = "buf.build/protocolbuffers/wellknowntypes"
473473
} else if fileInfo, ok := def.file.objectInfo.(bufmodule.FileInfo); ok {
474-
bsrHost = fileInfo.Module().ModuleFullName().String()
474+
bsrHost = fileInfo.Module().FullName().String()
475475
}
476476
if hasAnchor {
477477
bsrTooltip = pkg + "." + strings.Join(path, ".")

private/buf/bufmigrate/migrate_builder.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
2525
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
26+
"github.com/bufbuild/buf/private/bufpkg/bufparse"
2627
"github.com/bufbuild/buf/private/pkg/normalpath"
2728
"github.com/bufbuild/buf/private/pkg/slicesext"
2829
"github.com/bufbuild/buf/private/pkg/storage"
@@ -42,7 +43,7 @@ type migrateBuilder struct {
4243
addedModuleDirPaths map[string]struct{}
4344

4445
moduleConfigs []bufconfig.ModuleConfig
45-
configuredDepModuleRefs []bufmodule.ModuleRef
46+
configuredDepModuleRefs []bufparse.Ref
4647
hasSeenBufLockFile bool
4748
depModuleKeys []bufmodule.ModuleKey
4849
pathToMigratedBufGenYAMLFile map[string]bufconfig.BufGenYAMLFile
@@ -239,7 +240,7 @@ func (m *migrateBuilder) addModule(ctx context.Context, moduleDirPath string) (r
239240
return syserror.Newf("expect exactly 1 module config from buf yaml, got %d", len(bufYAMLFile.ModuleConfigs()))
240241
}
241242
moduleConfig := bufYAMLFile.ModuleConfigs()[0]
242-
moduleFullName := moduleConfig.ModuleFullName()
243+
moduleFullName := moduleConfig.FullName()
243244
// If a buf.yaml v1beta1 has a non-empty name and multiple roots, the
244245
// resulting buf.yaml v2 should have these roots as module directories,
245246
// but they should not share the same module name. Instead we just give
@@ -309,7 +310,7 @@ func (m *migrateBuilder) addModule(ctx context.Context, moduleDirPath string) (r
309310
}
310311
moduleConfig, err = bufconfig.NewModuleConfig(
311312
moduleRootRelativeToDestination,
312-
moduleConfig.ModuleFullName(),
313+
moduleConfig.FullName(),
313314
// We do not need to handle paths in rootToIncludes, rootToExcludes, lint or breaking config specially,
314315
// because the paths are transformed correctly by readBufYAMLFile and writeBufYAMLFile.
315316
moduleConfig.RootToIncludes(),
@@ -382,12 +383,12 @@ func (m *migrateBuilder) addModule(ctx context.Context, moduleDirPath string) (r
382383

383384
func (m *migrateBuilder) appendModuleConfig(moduleConfig bufconfig.ModuleConfig, parentPath string) error {
384385
m.moduleConfigs = append(m.moduleConfigs, moduleConfig)
385-
if moduleConfig.ModuleFullName() == nil {
386+
if moduleConfig.FullName() == nil {
386387
return nil
387388
}
388-
if file, ok := m.moduleFullNameStringToParentPath[moduleConfig.ModuleFullName().String()]; ok {
389-
return fmt.Errorf("module %s is found in both %s and %s", moduleConfig.ModuleFullName(), file, parentPath)
389+
if file, ok := m.moduleFullNameStringToParentPath[moduleConfig.FullName().String()]; ok {
390+
return fmt.Errorf("module %s is found in both %s and %s", moduleConfig.FullName(), file, parentPath)
390391
}
391-
m.moduleFullNameStringToParentPath[moduleConfig.ModuleFullName().String()] = parentPath
392+
m.moduleFullNameStringToParentPath[moduleConfig.FullName().String()] = parentPath
392393
return nil
393394
}

0 commit comments

Comments
 (0)