From fa72d583ef96e8fd0230f0e091e954cfbca7a21a Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Wed, 11 May 2022 10:06:17 +0300 Subject: [PATCH 1/4] Don't force latest k6 when building extensions an extension will always require k6 and a version of it. It makes little sense to force newer version on users especially as we keep breaking them and we need time to go fix them, but that can only happen after release. This still lets users specify the version (as latest for example) and require the latest k6, it just isn't the default. --- environment.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/environment.go b/environment.go index c2c9cdd..6701642 100644 --- a/environment.go +++ b/environment.go @@ -118,9 +118,13 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) { 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 + if env.k6Version != "" { + // don't actually specify k6 version if it wasn't specified + // the extension(s) will require a version that they work with either way + err = env.execGoModRequire(ctx, k6ModulePath, env.k6Version) + if err != nil { + return nil, err + } } } else { // building with a forked repo, so get the main one and replace it with From 1c4e7864d5830993bfd67d996c41f89a599fb0c6 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Wed, 11 May 2022 11:37:10 +0300 Subject: [PATCH 2/4] Fix any kind of go module replacements getting latest k6 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. --- environment.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/environment.go b/environment.go index 6701642..ce3c634 100644 --- a/environment.go +++ b/environment.go @@ -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") @@ -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") @@ -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 { From 94b9ca844c640f2bf2f5cf54a5a8424ddb2f394a Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Wed, 11 May 2022 12:20:22 +0300 Subject: [PATCH 3/4] Fix k6 version not being taken into account anymore This was as there was *possibly* nothing requesting k6 at the time it was setup. Replaces on the other hand stick so XK6_K6_REPO always worked. --- environment.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/environment.go b/environment.go index ce3c634..176d582 100644 --- a/environment.go +++ b/environment.go @@ -108,17 +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 - if env.k6Version != "" { - // don't actually specify k6 version if it wasn't specified - // the extension(s) will require a version that they work with either way - 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, "") @@ -175,6 +165,18 @@ nextExt: return nil, err } + // building with the default main repo + if b.K6Repo == "" && env.k6Version != "" { + // don't actually specify k6 version if it wasn't specified + // the extension(s) will require a version that they work with either way + // but if it was let specify it now + // all the previous steps should've worked so far as the extensions themselves would've required + // specific versions to begin with and they should work with those versions + err = env.execGoModRequire(ctx, k6ModulePath, env.k6Version) + if err != nil { + return nil, err + } + } err = env.execGoModTidy(ctx) if err != nil { return nil, err From 20fdc082bc60970611cd687afdeb49d24f368c13 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov <312246+mstoykov@users.noreply.github.com> Date: Wed, 11 May 2022 18:27:07 +0300 Subject: [PATCH 4/4] Update environment.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ivan Mirić --- environment.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/environment.go b/environment.go index 176d582..24b1a35 100644 --- a/environment.go +++ b/environment.go @@ -167,11 +167,9 @@ nextExt: // building with the default main repo if b.K6Repo == "" && env.k6Version != "" { - // don't actually specify k6 version if it wasn't specified - // the extension(s) will require a version that they work with either way - // but if it was let specify it now - // all the previous steps should've worked so far as the extensions themselves would've required - // specific versions to begin with and they should work with those versions + // 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