Skip to content

Commit 213111d

Browse files
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 golang/go#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
1 parent 7fb34ca commit 213111d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

magefile.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,16 +474,22 @@ func validateBuildDependencies() {
474474
continue
475475
}
476476

477-
env := map[string]string{
478-
// Disable module mode to install development dependencies to prevent to pollute the project module file.
479-
// This is a necessary workaround until the Go toolchain is able to install packages globally without
480-
// updating the module file when the "go get" command is run from within the project root directory.
481-
// See https://github.com/golang/go/issues/30515 for more details or more details and proposed solutions
482-
// that might be added to Go's build tools in future versions.
483-
"GO111MODULE": "off"}
484-
485477
prt.Infof("Installing required build dependency: %s", color.CyanString(bd.PackageName))
486-
if err = sh.RunWith(env, goExec, "get", "-u", bd.PackageName); err != nil {
478+
c := exec.Command(goExec, "get", "-u", bd.PackageName)
479+
// Run installations outside of the project root directory to prevent the pollution of the project's Go module
480+
// file.
481+
// This is a necessary workaround until the Go toolchain is able to install packages globally without
482+
// updating the module file when the "go get" command is run from within the project root directory.
483+
// See https://github.com/golang/go/issues/30515 for more details or more details and proposed solutions
484+
// that might be added to Go's build tools in future versions.
485+
c.Dir = os.TempDir()
486+
c.Env = os.Environ()
487+
// Explicitly enable "module" mode to install development dependencies to allow to use pinned module versions.
488+
env := map[string]string{"GO111MODULE": "on"}
489+
for k, v := range env {
490+
c.Env = append(c.Env, k+"="+v)
491+
}
492+
if err = c.Run(); err != nil {
487493
prt.Errorf("Failed to install required build dependency %s: %v", color.CyanString(bd.PackageName), err)
488494
prt.Warnf("Please install manually: %s", color.CyanString("go get -u %s", bd.PackageName))
489495
os.Exit(1)

0 commit comments

Comments
 (0)