Skip to content

Commit

Permalink
Merge pull request #44 from grafana/dontUseLatestk6
Browse files Browse the repository at this point in the history
Don't force latest k6 when building extensions
  • Loading branch information
mstoykov authored May 12, 2022
2 parents 9001905 + 20fdc08 commit b706106
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
}()
log.Printf("[INFO] Temporary folder: %s", tempFolder)

// write the main module file to temporary folder
mainPath := filepath.Join(tempFolder, "main.go")
log.Printf("[INFO] Writing main module: %s", mainPath)
err = ioutil.WriteFile(mainPath, buf.Bytes(), 0600)
if err != nil {
return nil, err
}

// initialize the go module
log.Println("[INFO] Initializing Go module")
cmd := env.newCommand("go", "mod", "init", "k6")
Expand Down Expand Up @@ -116,13 +108,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {

// pin versions by populating go.mod, first for k6 itself and then extensions
log.Println("[INFO] Pinning versions")
if b.K6Repo == "" {
// building with the default main repo
err = env.execGoModRequire(ctx, k6ModulePath, env.k6Version)
if err != nil {
return nil, err
}
} else {
if b.K6Repo != "" {
// building with a forked repo, so get the main one and replace it with
// the fork
err = env.execGoModRequire(ctx, k6ModulePath, "")
Expand Down Expand Up @@ -164,6 +150,35 @@ nextExt:
default:
}
}
// This is here as we could've not run go mod tidy due to a replace being the only extension
err = env.execGoModTidy(ctx)
if err != nil {
return nil, err
}

// write the main module file to temporary folder
// we do this last so we get the needed versions from all the replacements and extensions instead of k6 if possible
mainPath := filepath.Join(tempFolder, "main.go")
log.Printf("[INFO] Writing main module: %s", mainPath)
err = ioutil.WriteFile(mainPath, buf.Bytes(), 0o600)
if err != nil {
return nil, err
}

// building with the default main repo
if b.K6Repo == "" && env.k6Version != "" {
// Only require a specific k6 version if provided. Otherwise extensions
// will require a version they depend on, and Go's module resolution
// algorithm will choose the highest one among all extensions.
err = env.execGoModRequire(ctx, k6ModulePath, env.k6Version)
if err != nil {
return nil, err
}
}
err = env.execGoModTidy(ctx)
if err != nil {
return nil, err
}

log.Println("[INFO] Build environment ready")

Expand Down Expand Up @@ -195,7 +210,7 @@ func (env environment) writeExtensionImportFile(packagePath string) error {
import _ %q
`, packagePath)
filePath := filepath.Join(env.tempFolder, strings.ReplaceAll(packagePath, "/", "_")+".go")
return ioutil.WriteFile(filePath, []byte(fileContents), 0600)
return ioutil.WriteFile(filePath, []byte(fileContents), 0o600)
}

func (env environment) newCommand(command string, args ...string) *exec.Cmd {
Expand Down

0 comments on commit b706106

Please sign in to comment.