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

Run go mod tidy after go mod edit -replace #39

Merged
merged 4 commits into from
Feb 17, 2022
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Stay updated, be aware of changes, and please submit feedback! Thanks!

## Requirements

- [Go installed](https://golang.org/doc/install)
- [Go installed](https://golang.org/doc/install). At least version 1.17 is needed.

## Install

Expand Down
4 changes: 1 addition & 3 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {

log.Println("[INFO] Building k6")

// tidy the module to ensure go.mod and go.sum are consistent with the module prereq
tidyCmd := buildEnv.newCommand("go", "mod", "tidy")
if err := buildEnv.runCommand(ctx, tidyCmd, b.TimeoutGet); err != nil {
if err := buildEnv.execGoModTidy(ctx); err != nil {
return err
}

Expand Down
31 changes: 21 additions & 10 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
replaced := make(map[string]string)
for _, r := range b.Replacements {
log.Printf("[INFO] Replace %s => %s", r.Old.String(), r.New.String())
cmd := env.newCommand("go", "mod", "edit",
"-replace", fmt.Sprintf("%s=%s", r.Old.Param(), r.New.Param()))
err := env.runCommand(ctx, cmd, 10*time.Second)
err = env.execGoModReplace(ctx, r.Old.Param(), r.New.Param())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -135,12 +133,11 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
if err != nil {
return nil, err
}
replace := fmt.Sprintf("%s=%s", k6ModulePath, b.K6Repo)
replace := b.K6Repo
if b.K6Version != "" {
replace = fmt.Sprintf("%s@%s", replace, b.K6Version)
replace = fmt.Sprintf("%s@%s", b.K6Repo, b.K6Version)
}
cmd = env.newCommand("go", "mod", "edit", "-replace", replace)
err = env.runCommand(ctx, cmd, 10*time.Second)
err = env.execGoModReplace(ctx, k6ModulePath, replace)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -247,6 +244,12 @@ func (env environment) runCommand(ctx context.Context, cmd *exec.Cmd, timeout ti
}
}

// tidy the module to ensure go.mod will not have versions such as `latest`
func (env environment) execGoModTidy(ctx context.Context) error {
tidyCmd := env.newCommand("go", "mod", "tidy", "-compat=1.17")
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
}

func (env environment) execGoModRequire(ctx context.Context, modulePath, moduleVersion string) error {
mod := modulePath
if moduleVersion != "" {
Expand All @@ -259,9 +262,17 @@ func (env environment) execGoModRequire(ctx context.Context, modulePath, moduleV
if err != nil {
return err
}
// tidy the module to ensure go.mod will not have versions such as `latest`
tidyCmd := env.newCommand("go", "mod", "tidy")
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
return env.execGoModTidy(ctx)
}

func (env environment) execGoModReplace(ctx context.Context, modulePath, replaceRepo string) error {
replace := fmt.Sprintf("%s=%s", modulePath, replaceRepo)
cmd := env.newCommand("go", "mod", "edit", "-replace", replace)
err := env.runCommand(ctx, cmd, env.timeoutGoGet)
if err != nil {
return err
}
return env.execGoModTidy(ctx)
}

type goModTemplateContext struct {
Expand Down