From 213111d3b2ae8ff10f3175cf0d062f508a48d400 Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Sat, 12 Oct 2019 17:17:22 +0200 Subject: [PATCH] Development dependency global installation workaround (#89) The workaround implemented in GH-82 (PR GH-85) worked fine, but due to the explicitly disabled "module" mode it was not possible to define pinned dependency versions but only using the normal `go get` behavior to build the repositories default branch. A better workaround is to run the `go get` command for development & build dependencies/packages outside of the project's root directory. Therefore the `go.mod` file is not in scope for the `go get` command and is therefore not updated. In order to use pinned versions the `GO1111MODULE=on` environment variable is explicitly set when running the `go get` command. See https://github.com/golang/go/issues/30515 for more details and proposed solutions that might be added to Go's build tools in future versions. Epic GH-33 Resolves GH-88 --- magefile.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/magefile.go b/magefile.go index e10eff9..a88c431 100644 --- a/magefile.go +++ b/magefile.go @@ -474,16 +474,22 @@ func validateBuildDependencies() { continue } - env := map[string]string{ - // Disable module mode to install development dependencies to prevent to pollute the project module file. - // This is a necessary workaround until the Go toolchain is able to install packages globally without - // updating the module file when the "go get" command is run from within the project root directory. - // See https://github.com/golang/go/issues/30515 for more details or more details and proposed solutions - // that might be added to Go's build tools in future versions. - "GO111MODULE": "off"} - prt.Infof("Installing required build dependency: %s", color.CyanString(bd.PackageName)) - if err = sh.RunWith(env, goExec, "get", "-u", bd.PackageName); err != nil { + c := exec.Command(goExec, "get", "-u", bd.PackageName) + // Run installations outside of the project root directory to prevent the pollution of the project's Go module + // file. + // This is a necessary workaround until the Go toolchain is able to install packages globally without + // updating the module file when the "go get" command is run from within the project root directory. + // See https://github.com/golang/go/issues/30515 for more details or more details and proposed solutions + // that might be added to Go's build tools in future versions. + c.Dir = os.TempDir() + c.Env = os.Environ() + // Explicitly enable "module" mode to install development dependencies to allow to use pinned module versions. + env := map[string]string{"GO111MODULE": "on"} + for k, v := range env { + c.Env = append(c.Env, k+"="+v) + } + if err = c.Run(); err != nil { prt.Errorf("Failed to install required build dependency %s: %v", color.CyanString(bd.PackageName), err) prt.Warnf("Please install manually: %s", color.CyanString("go get -u %s", bd.PackageName)) os.Exit(1)