Skip to content

Commit

Permalink
Add bufparse package
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane committed Oct 30, 2024
1 parent 0d630b6 commit bce5b5b
Show file tree
Hide file tree
Showing 104 changed files with 783 additions and 949 deletions.
6 changes: 3 additions & 3 deletions private/buf/bufcli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"

"connectrpc.com/connect"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
)

var (
Expand Down Expand Up @@ -73,12 +73,12 @@ func NewModuleNotFoundError(name string) error {
}

// NewModuleRefNotFoundError informs the user that a ModuleRef does not exist.
func NewModuleRefNotFoundError(moduleRef bufmodule.ModuleRef) error {
func NewModuleRefNotFoundError(moduleRef bufparse.Ref) error {
return fmt.Errorf("%q does not exist", moduleRef)
}

// NewLabelNotFoundError informs the user that a ModuleRef does not exist as a label.
func NewLabelNotFoundError(moduleRef bufmodule.ModuleRef) error {
func NewLabelNotFoundError(moduleRef bufparse.Ref) error {
return fmt.Errorf("label %q does not exist", moduleRef)
}

Expand Down
31 changes: 16 additions & 15 deletions private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/bufbuild/buf/private/bufpkg/bufimage"
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimageutil"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/bufpkg/bufreflect"
"github.com/bufbuild/buf/private/gen/data/datawkt"
imagev1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/image/v1"
Expand Down Expand Up @@ -1090,39 +1091,39 @@ func (c *controller) warnUnconfiguredTransitiveImports(
if slicesext.Count(workspace.Modules(), bufmodule.Module.IsLocal) == 0 {
return nil
}
// Construct a struct map of all the ModuleFullName strings of the configured buf.yaml
// Construct a struct map of all the FullName strings of the configured buf.yaml
// Module dependencies, and the local Modules. These are considered OK to depend on
// for non-imports in the Image.
configuredModuleFullNameStrings, err := slicesext.MapError(
configuredFullNameStrings, err := slicesext.MapError(
workspace.ConfiguredDepModuleRefs(),
func(moduleRef bufmodule.ModuleRef) (string, error) {
moduleFullName := moduleRef.ModuleFullName()
func(moduleRef bufparse.Ref) (string, error) {
moduleFullName := moduleRef.FullName()
if moduleFullName == nil {
return "", syserror.New("ModuleFullName nil on ModuleRef")
return "", syserror.New("FullName nil on ModuleRef")
}
return moduleFullName.String(), nil
},
)
if err != nil {
return err
}
configuredModuleFullNameStringMap := slicesext.ToStructMap(configuredModuleFullNameStrings)
configuredFullNameStringMap := slicesext.ToStructMap(configuredFullNameStrings)
for _, localModule := range bufmodule.ModuleSetLocalModules(workspace) {
if moduleFullName := localModule.ModuleFullName(); moduleFullName != nil {
configuredModuleFullNameStringMap[moduleFullName.String()] = struct{}{}
if moduleFullName := localModule.FullName(); moduleFullName != nil {
configuredFullNameStringMap[moduleFullName.String()] = struct{}{}
}
}

// Construct a map from Image file path -> ModuleFullName string.
// Construct a map from Image file path -> FullName string.
//
// If a given file in the Image did not have a ModuleFullName, it came from a local unnamed Module
// If a given file in the Image did not have a FullName, it came from a local unnamed Module
// in the Workspace, and we're safe to ignore it with respect to calculating the undeclared
// transitive imports.
pathToModuleFullNameString := make(map[string]string)
pathToFullNameString := make(map[string]string)
for _, imageFile := range image.Files() {
// If nil, this came from a local unnamed Module in the Workspace, and we're safe to ignore.
if moduleFullName := imageFile.ModuleFullName(); moduleFullName != nil {
pathToModuleFullNameString[imageFile.Path()] = moduleFullName.String()
if moduleFullName := imageFile.FullName(); moduleFullName != nil {
pathToFullNameString[imageFile.Path()] = moduleFullName.String()
}
}

Expand All @@ -1132,12 +1133,12 @@ func (c *controller) warnUnconfiguredTransitiveImports(
continue
}
for _, importPath := range imageFile.FileDescriptorProto().GetDependency() {
moduleFullNameString, ok := pathToModuleFullNameString[importPath]
moduleFullNameString, ok := pathToFullNameString[importPath]
if !ok {
// The import was from a local unnamed Module in the Workspace.
continue
}
if _, ok := configuredModuleFullNameStringMap[moduleFullNameString]; !ok {
if _, ok := configuredFullNameStringMap[moduleFullNameString]; !ok {
c.logger.Warn(fmt.Sprintf(
`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.
Declare %q in the deps key in your buf.yaml.`,
Expand Down
5 changes: 3 additions & 2 deletions private/buf/buffetch/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/bufbuild/buf/private/buf/buftarget"
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/httpauth"
Expand Down Expand Up @@ -223,7 +224,7 @@ func NewGitRef(
// ModuleRef is a module reference.
type ModuleRef interface {
Ref
ModuleRef() bufmodule.ModuleRef
ModuleRef() bufparse.Ref
moduleRef()
}

Expand Down Expand Up @@ -375,7 +376,7 @@ type ParsedModuleRef interface {
// This should only be used for testing.
func NewDirectParsedModuleRef(
format string,
moduleRef bufmodule.ModuleRef,
moduleRef bufparse.Ref,
) ParsedModuleRef {
return newDirectModuleRef(
format,
Expand Down
10 changes: 5 additions & 5 deletions private/buf/buffetch/internal/module_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package internal
import (
"strings"

"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/pkg/app"
)

Expand All @@ -27,7 +27,7 @@ var (

type moduleRef struct {
format string
iModuleRef bufmodule.ModuleRef
iModuleRef bufparse.Ref
}

func newModuleRef(
Expand All @@ -46,15 +46,15 @@ func newModuleRef(
if strings.Contains(path, "://") {
return nil, NewInvalidPathError(format, path)
}
moduleRef, err := bufmodule.ParseModuleRef(path)
moduleRef, err := bufparse.ParseRef(path)
if err != nil {
// TODO: this is dumb
return nil, NewInvalidPathError(format, path)
}
return newDirectModuleRef(format, moduleRef), nil
}

func newDirectModuleRef(format string, iModuleRef bufmodule.ModuleRef) *moduleRef {
func newDirectModuleRef(format string, iModuleRef bufparse.Ref) *moduleRef {
return &moduleRef{
format: format,
iModuleRef: iModuleRef,
Expand All @@ -65,7 +65,7 @@ func (r *moduleRef) Format() string {
return r.format
}

func (r *moduleRef) ModuleRef() bufmodule.ModuleRef {
func (r *moduleRef) ModuleRef() bufparse.Ref {
return r.iModuleRef
}

Expand Down
3 changes: 2 additions & 1 deletion private/buf/buffetch/internal/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/bufbuild/buf/private/buf/buftarget"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/httpauth"
Expand Down Expand Up @@ -398,7 +399,7 @@ func (r *reader) getModuleKey(
}
moduleKeys, err := r.moduleKeyProvider.GetModuleKeysForModuleRefs(
ctx,
[]bufmodule.ModuleRef{moduleRef.ModuleRef()},
[]bufparse.Ref{moduleRef.ModuleRef()},
bufmodule.DigestTypeB5,
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions private/buf/buffetch/ref_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/bufbuild/buf/private/buf/buffetch/internal"
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/syserror"
)
Expand Down Expand Up @@ -837,7 +837,7 @@ func assumeModuleOrDir(path string) (string, error) {
if path == "" {
return "", errors.New("assumeModuleOrDir: no path given")
}
if _, err := bufmodule.ParseModuleRef(path); err == nil {
if _, err := bufparse.ParseRef(path); err == nil {
// this is possible to be a module, check if it is a directory though
// OK to use os.Stat instead of os.Lstat here
fileInfo, err := os.Stat(path)
Expand Down
6 changes: 3 additions & 3 deletions private/buf/buffetch/ref_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"

"github.com/bufbuild/buf/private/buf/buffetch/internal"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/normalpath"
Expand Down Expand Up @@ -1442,8 +1442,8 @@ func testNewModuleRef(
owner string,
name string,
ref string,
) bufmodule.ModuleRef {
moduleRef, err := bufmodule.NewModuleRef(registry, owner, name, ref)
) bufparse.Ref {
moduleRef, err := bufparse.NewRef(registry, owner, name, ref)
require.NoError(t, err)
return moduleRef
}
2 changes: 1 addition & 1 deletion private/buf/buflsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func (f *file) RunLints(ctx context.Context) bool {
return false
}

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

lintConfig := workspace.GetLintConfigForOpaqueID(module.OpaqueID())
err := f.lsp.checkClient.Lint(
Expand Down
2 changes: 1 addition & 1 deletion private/buf/buflsp/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (s *symbol) FormatDocs(ctx context.Context) string {
if def.file.IsWKT() {
bsrHost = "buf.build/protocolbuffers/wellknowntypes"
} else if fileInfo, ok := def.file.objectInfo.(bufmodule.FileInfo); ok {
bsrHost = fileInfo.Module().ModuleFullName().String()
bsrHost = fileInfo.Module().FullName().String()
}
if hasAnchor {
bsrTooltip = pkg + "." + strings.Join(path, ".")
Expand Down
15 changes: 8 additions & 7 deletions private/buf/bufmigrate/migrate_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/slicesext"
Expand All @@ -45,7 +46,7 @@ type migrateBuilder struct {
addedModuleDirPaths map[string]struct{}

moduleConfigs []bufconfig.ModuleConfig
configuredDepModuleRefs []bufmodule.ModuleRef
configuredDepModuleRefs []bufparse.Ref
hasSeenBufLockFile bool
depModuleKeys []bufmodule.ModuleKey
pathToMigratedBufGenYAMLFile map[string]bufconfig.BufGenYAMLFile
Expand Down Expand Up @@ -244,7 +245,7 @@ func (m *migrateBuilder) addModule(ctx context.Context, moduleDirPath string) (r
return syserror.Newf("expect exactly 1 module config from buf yaml, got %d", len(bufYAMLFile.ModuleConfigs()))
}
moduleConfig := bufYAMLFile.ModuleConfigs()[0]
moduleFullName := moduleConfig.ModuleFullName()
moduleFullName := moduleConfig.FullName()
// If a buf.yaml v1beta1 has a non-empty name and multiple roots, the
// resulting buf.yaml v2 should have these roots as module directories,
// but they should not share the same module name. Instead we just give
Expand Down Expand Up @@ -314,7 +315,7 @@ func (m *migrateBuilder) addModule(ctx context.Context, moduleDirPath string) (r
}
moduleConfig, err = bufconfig.NewModuleConfig(
moduleRootRelativeToDestination,
moduleConfig.ModuleFullName(),
moduleConfig.FullName(),
// We do not need to handle paths in rootToIncludes, rootToExcludes, lint or breaking config specially,
// because the paths are transformed correctly by readBufYAMLFile and writeBufYAMLFile.
moduleConfig.RootToIncludes(),
Expand Down Expand Up @@ -387,12 +388,12 @@ func (m *migrateBuilder) addModule(ctx context.Context, moduleDirPath string) (r

func (m *migrateBuilder) appendModuleConfig(moduleConfig bufconfig.ModuleConfig, parentPath string) error {
m.moduleConfigs = append(m.moduleConfigs, moduleConfig)
if moduleConfig.ModuleFullName() == nil {
if moduleConfig.FullName() == nil {
return nil
}
if file, ok := m.moduleFullNameStringToParentPath[moduleConfig.ModuleFullName().String()]; ok {
return fmt.Errorf("module %s is found in both %s and %s", moduleConfig.ModuleFullName(), file, parentPath)
if file, ok := m.moduleFullNameStringToParentPath[moduleConfig.FullName().String()]; ok {
return fmt.Errorf("module %s is found in both %s and %s", moduleConfig.FullName(), file, parentPath)
}
m.moduleFullNameStringToParentPath[moduleConfig.ModuleFullName().String()] = parentPath
m.moduleFullNameStringToParentPath[moduleConfig.FullName().String()] = parentPath
return nil
}
Loading

0 comments on commit bce5b5b

Please sign in to comment.