Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crosslink tidylist command #642

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/crosslink-tidy-v1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. crosslink)
component: crosslink

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds a 'tidy' subcommand to generate 'go mod tidy' schedules

# One or more tracking issues related to the change
issues: [642]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
10 changes: 10 additions & 0 deletions crosslink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,13 @@ for out-dated intra-repository Go modules.
Go version applied when new `go.work` file is created (default "1.22").

crosslink work --go=1.23

### tidylist

The 'tidylist' command generates a sequence of intra-repository modules, such that
running 'go mod tidy' on each module in this sequence guarantees that changes
in the 'go.mod' of one module will be propagated to all of its dependent
modules. This ensures that no modules are left in a broken 'updates to go.mod
needed' state at the end. For an acyclic dependency graph, this corresponds to
topological order. If modules are found to have circular dependencies, they will
be checked against a provided allowlist.
32 changes: 26 additions & 6 deletions crosslink/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
)

type commandConfig struct {
runConfig cl.RunConfig
excludeFlags []string
skipFlags []string
rootCommand cobra.Command
pruneCommand cobra.Command
workCommand cobra.Command
runConfig cl.RunConfig
excludeFlags []string
skipFlags []string
rootCommand cobra.Command
pruneCommand cobra.Command
workCommand cobra.Command
tidyListCommand cobra.Command
}

func newCommandConfig() *commandConfig {
Expand Down Expand Up @@ -118,6 +119,23 @@
}
c.rootCommand.AddCommand(&c.workCommand)

c.tidyListCommand = cobra.Command{
Use: "tidylist output_path",
Short: "Generate a schedule of modules to tidy that guarantees full propagation of changes",
Long: "The 'tidylist' command generates a sequence of intra-repository modules, such that\n" +
"running 'go mod tidy' on each module in this sequence guarantees that changes\n" +
"in the 'go.mod' of one module will be propagated to all of its dependent\n" +
"modules. This ensures that no modules are left in a broken 'updates to go.mod\n" +
"needed' state at the end. For an acyclic dependency graph, this corresponds to\n" +
"topological order. If modules are found to have circular dependencies, they will\n" +
"be checked against a provided allowlist.",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
return cl.TidyList(c.runConfig, args[0])
},

Check warning on line 135 in crosslink/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

crosslink/cmd/root.go#L134-L135

Added lines #L134 - L135 were not covered by tests
}
c.rootCommand.AddCommand(&c.tidyListCommand)

return c
}

Expand Down Expand Up @@ -146,6 +164,8 @@
comCfg.pruneCommand.Flags().StringSliceVar(&comCfg.excludeFlags, "exclude", []string{}, "list of comma separated go modules that crosslink will ignore in operations."+
"multiple calls of --exclude can be made")
comCfg.workCommand.Flags().StringVar(&comCfg.runConfig.GoVersion, "go", "1.22", "Go version applied when new go.work file is created")
comCfg.tidyListCommand.Flags().StringVar(&comCfg.runConfig.AllowCircular, "allow-circular", "", "path to list of go modules that are allowed to have circular dependencies")
comCfg.tidyListCommand.Flags().BoolVar(&comCfg.runConfig.Validate, "validate", false, "enables brute force validation of the tidy schedule")
}

// transform array slice into map
Expand Down
23 changes: 23 additions & 0 deletions crosslink/internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,26 @@
return fn(path)
})
}

func forGoModFiles(rc RunConfig, fn func(modPath string, modName string, modFile *modfile.File) error) error {
return forGoModules(rc.Logger, rc.RootPath, func(path string) error {
if _, ok := rc.SkippedPaths[path]; ok {
rc.Logger.Debug("skipping", zap.String("path", path))
return nil
}
fullPath := filepath.Join(rc.RootPath, path)
modFile, err := os.ReadFile(filepath.Clean(fullPath))
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}

Check warning on line 90 in crosslink/internal/common.go

View check run for this annotation

Codecov / codecov/patch

crosslink/internal/common.go#L89-L90

Added lines #L89 - L90 were not covered by tests

modContents, err := modfile.Parse(fullPath, modFile, nil)
if err != nil {
rc.Logger.Error("Modfile could not be parsed",
zap.Error(err),
zap.String("file_path", path))
}

Check warning on line 97 in crosslink/internal/common.go

View check run for this annotation

Codecov / codecov/patch

crosslink/internal/common.go#L94-L97

Added lines #L94 - L97 were not covered by tests

return fn(path, modfile.ModulePath(modFile), modContents)
})
}
2 changes: 2 additions & 0 deletions crosslink/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type RunConfig struct {
Overwrite bool
Prune bool
GoVersion string
AllowCircular string
Validate bool
Logger *zap.Logger
}

Expand Down
24 changes: 2 additions & 22 deletions crosslink/internal/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ package crosslink

import (
"fmt"
"os"
"path/filepath"
"strings"

"go.uber.org/zap"
"golang.org/x/mod/modfile"
)

Expand All @@ -30,25 +27,8 @@ import (
func buildDepedencyGraph(rc RunConfig, rootModulePath string) (map[string]*moduleInfo, error) {
moduleMap := make(map[string]*moduleInfo)

err := forGoModules(rc.Logger, rc.RootPath, func(path string) error {
if _, ok := rc.SkippedPaths[path]; ok {
rc.Logger.Debug("skipping", zap.String("path", path))
return nil
}
fullPath := filepath.Join(rc.RootPath, path)
modFile, err := os.ReadFile(filepath.Clean(fullPath))
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}

modContents, err := modfile.Parse(fullPath, modFile, nil)
if err != nil {
rc.Logger.Error("Modfile could not be parsed",
zap.Error(err),
zap.String("file_path", path))
}

moduleMap[modfile.ModulePath(modFile)] = newModuleInfo(*modContents)
err := forGoModFiles(rc, func(_ string, modPath string, modContents *modfile.File) error {
moduleMap[modPath] = newModuleInfo(*modContents)
return nil
})
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions crosslink/internal/mock_test_data/testTidyListAcyclic/gomod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go.opentelemetry.io/build-tools/crosslink/testroot

go 1.20
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module go.opentelemetry.io/build-tools/crosslink/testroot/testA

go 1.20

require go.opentelemetry.io/build-tools/crosslink/testroot/testB v1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module go.opentelemetry.io/build-tools/crosslink/testroot/testB

go 1.20

require go.opentelemetry.io/build-tools/crosslink/testroot/testC v1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go.opentelemetry.io/build-tools/crosslink/testroot/testC

go 1.20
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file lists the modules that are expected to have circular dependencies.
# If it does not match the actual list, `crosslink tidylist` will return an error.
testA
testB
testC
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file lists the modules that are expected to have circular dependencies.
# If it does not match the actual list, `crosslink tidylist` will return an error.
testA
testB
3 changes: 3 additions & 0 deletions crosslink/internal/mock_test_data/testTidyListCyclic/gomod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go.opentelemetry.io/build-tools/crosslink/testroot

go 1.20
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module go.opentelemetry.io/build-tools/crosslink/testroot/testA

go 1.20

require go.opentelemetry.io/build-tools/crosslink/testroot/testB v1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module go.opentelemetry.io/build-tools/crosslink/testroot/testB

go 1.20

require go.opentelemetry.io/build-tools/crosslink/testroot/testA v1.0.0
require go.opentelemetry.io/build-tools/crosslink/testroot/testC v1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go.opentelemetry.io/build-tools/crosslink/testroot/testC

go 1.20
Loading
Loading