Skip to content

Commit

Permalink
MalformedDep now has bufmodule.ModuleRef info
Browse files Browse the repository at this point in the history
  • Loading branch information
doriable committed May 10, 2024
1 parent 92f1092 commit 18dd6b6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
37 changes: 11 additions & 26 deletions private/buf/bufworkspace/malformed_dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,12 @@ const (
type MalformedDepType int

// MalformedDep is a dep that was malformed in some way in the buf.yaml.
//
// TODO: rename this to MalformedDepRef or something and give it a ModuleRef
// It provides the module ref information and the malformed dep type.
type MalformedDep interface {
// ModuleFullName returns the full name of the malformed dep.
// ModuleRef is the module ref information of the malformed dep.
//
// Always present.
ModuleFullName() bufmodule.ModuleFullName
// Ref returns the reference within the Module.
//
// May be a label or dashless commitID.
//
// May be empty, in which case this references the commit of the default label of the Module.
Ref() string
ModuleRef() bufmodule.ModuleRef
// Type is why this dep was malformed.
//
// Always present.
Expand Down Expand Up @@ -113,8 +106,7 @@ func MalformedDepsForWorkspace(workspace Workspace) ([]MalformedDep, error) {
malformedDeps = append(
malformedDeps,
newMalformedDep(
configuredDepModuleRef.ModuleFullName(),
configuredDepModuleRef.Ref(),
configuredDepModuleRef,
MalformedDepTypeUnused,
),
)
Expand All @@ -123,8 +115,8 @@ func MalformedDepsForWorkspace(workspace Workspace) ([]MalformedDep, error) {
sort.Slice(
malformedDeps,
func(i int, j int) bool {
return malformedDeps[i].ModuleFullName().String() <
malformedDeps[j].ModuleFullName().String()
return malformedDeps[i].ModuleRef().ModuleFullName().String() <
malformedDeps[j].ModuleRef().ModuleFullName().String()
},
)
return malformedDeps, nil
Expand All @@ -133,29 +125,22 @@ func MalformedDepsForWorkspace(workspace Workspace) ([]MalformedDep, error) {
// *** PRIVATE ***

type malformedDep struct {
moduleFullName bufmodule.ModuleFullName
ref string
moduleRef bufmodule.ModuleRef
malformedDepType MalformedDepType
}

func newMalformedDep(
moduleFullName bufmodule.ModuleFullName,
ref string,
moduleRef bufmodule.ModuleRef,
malformedDepType MalformedDepType,
) *malformedDep {
return &malformedDep{
moduleFullName: moduleFullName,
ref: ref,
moduleRef: moduleRef,
malformedDepType: malformedDepType,
}
}

func (m *malformedDep) ModuleFullName() bufmodule.ModuleFullName {
return m.moduleFullName
}

func (m *malformedDep) Ref() string {
return m.ref
func (m *malformedDep) ModuleRef() bufmodule.ModuleRef {
return m.moduleRef
}

func (m *malformedDep) Type() MalformedDepType {
Expand Down
4 changes: 2 additions & 2 deletions private/buf/bufworkspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ func TestUnusedDep(t *testing.T) {
malformedDeps, err := MalformedDepsForWorkspace(workspace)
require.NoError(t, err)
require.Equal(t, 2, len(malformedDeps))
require.Equal(t, "buf.testing/acme/date", malformedDeps[0].ModuleFullName().String())
require.Equal(t, "buf.testing/acme/date", malformedDeps[0].ModuleRef().ModuleFullName().String())
require.Equal(t, MalformedDepTypeUnused, malformedDeps[0].Type())
require.Equal(t, "buf.testing/acme/extension", malformedDeps[1].ModuleFullName().String())
require.Equal(t, "buf.testing/acme/extension", malformedDeps[1].ModuleRef().ModuleFullName().String())
require.Equal(t, MalformedDepTypeUnused, malformedDeps[1].Type())
}

Expand Down
11 changes: 10 additions & 1 deletion private/buf/cmd/buf/command/dep/depprune/depprune.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,14 @@ func run(
if err != nil {
return err
}
return internal.Prune(ctx, container.Logger(), controller, configuredDepModuleKeys, workspaceDepManager, dirPath)
return internal.Prune(
ctx,
container.Logger(),
controller,
container,
configuredDepModuleKeys,
workspaceDepManager,
dirPath,
false, // keepUnused
)
}
11 changes: 10 additions & 1 deletion private/buf/cmd/buf/command/dep/depupdate/depupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,14 @@ func run(
// Note that the check to make sure configuredDepModuleKeys is a superset of the remote deps should be a no-op
// in this case, they should be equivalent based on the updates we just did, but we do the check
// anyways to triple verify.
return internal.Prune(ctx, logger, controller, configuredDepModuleKeys, workspaceDepManager, dirPath)
return internal.Prune(
ctx,
logger,
controller,
container,
configuredDepModuleKeys,
workspaceDepManager,
dirPath,
true, // keepUnused
)
}
31 changes: 25 additions & 6 deletions private/buf/cmd/buf/command/dep/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func ModuleKeysAndTransitiveDepModuleKeysForModuleRefs(

// Prune prunes the buf.lock.
//
// Used by both mod prune and mod update.
// Used by both dep/mod prune and dep/mod update.
func Prune(
ctx context.Context,
logger *zap.Logger,
controller bufctl.Controller,
moduleKeyProvider bufmodule.ModuleKeyProvider,
container appext.Container,
// Contains all the Modules and their transitive dependencies based on the buf.yaml.
//
// All dependencies must be within this group from RemoteDepsForModuleSet. If a dependency
Expand All @@ -70,6 +70,8 @@ func Prune(
bufYAMLBasedDepModuleKeys []bufmodule.ModuleKey,
workspaceDepManager bufworkspace.WorkspaceDepManager,
dirPath string,
// For some use cases, such as dep/mod update, we want to keep unused declared dependencies
// in buf.lock (and only prune dependencies that are no longer declared) when set to true.
keepUnused bool,
) error {
workspace, err := controller.GetWorkspace(ctx, dirPath, bufctl.WithIgnoreAndDisallowV1BufWorkYAMLs())
Expand All @@ -90,20 +92,20 @@ func Prune(
if err != nil {
return err
}
var unusedMalformedDeps []bufworkspace.MalformedDep
var unusedModuleRefs []bufmodule.ModuleRef
for _, malformedDep := range malformedDeps {
switch t := malformedDep.Type(); t {
case bufworkspace.MalformedDepTypeUnused:
logger.Sugar().Warnf(
`Module %[1]s is declared in your buf.yaml deps but is unused. This command only modifies buf.lock files, not buf.yaml files. Please remove %[1]s from your buf.yaml deps if it is not needed.`,
malformedDep.ModuleFullName(),
malformedDep.ModuleRef().ModuleFullName(),
)
unusedMalformedDeps = append(unusedMalformedDeps, malformedDep)
unusedModuleRefs = append(unusedModuleRefs, malformedDep.ModuleRef())
default:
return fmt.Errorf("unknown MalformedDepType: %v", t)
}
}
// Sep that actual computed remote dependencies based on imports. These are all
// Step that actual computed remote dependencies based on imports. These are all
// that is needed for buf.lock.
depModules, err := bufmodule.RemoteDepsForModuleSet(workspace)
if err != nil {
Expand All @@ -118,6 +120,23 @@ func Prune(
if err != nil {
return err
}
// If keepUnused is set to true, we add the unused declared dependencies to depModuleKeys.
if keepUnused {
moduleKeyProvider, err := bufcli.NewModuleKeyProvider(container)
if err != nil {
return err
}
unusedModuleKeys, err := moduleKeyProvider.GetModuleKeysForModuleRefs(
ctx,
unusedModuleRefs,
workspaceDepManager.BufLockFileDigestType(),
)
if err != nil {
return err
}
depModuleKeys = append(depModuleKeys, unusedModuleKeys...)
}

if err := validateModuleKeysContains(bufYAMLBasedDepModuleKeys, depModuleKeys); err != nil {
return err
}
Expand Down

0 comments on commit 18dd6b6

Please sign in to comment.