Skip to content

Commit

Permalink
Fix any kind of go module replacements getting latest k6
Browse files Browse the repository at this point in the history
This is a bit strange but it turns out that if you have replacements go
change how it resolves modules a bit and will try to get the latest
versions of some dependencies.

Also if you have one extension and you are building it from local
sources we would not run `go mod tidy` reliably. This doesn't really
matter in practice as we do run it almost every other command, but was
getting in the way of smaller fix.

Both of those are fixed with this commit and now we do definitely run
`go mod tidy` once we have requested all extensions and put down the
files needing them. And also only write a file requesting k6 directly at
the very end so it only try to get it's version at this point - and at
that point we should already have requested through an extension.
  • Loading branch information
mstoykov committed May 11, 2022
1 parent fa72d58 commit 1c4e786
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 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 @@ -168,6 +160,25 @@ 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
}

err = env.execGoModTidy(ctx)
if err != nil {
return nil, err
}

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

Expand Down Expand Up @@ -199,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 1c4e786

Please sign in to comment.