From 247f6bda38a1b861a3f646aadbfa93da50fc460b Mon Sep 17 00:00:00 2001 From: William Johansson Date: Wed, 4 Jan 2023 12:56:13 +0100 Subject: [PATCH] fix(go-licences): fix running on macOS when Go is upgraded Running go-licenses breaks after upgrading Go through Homebrew, as the GOROOT path on the machine no longer matches the one used to build go-licenses. As a workaround, explicitly set GOROOT. See https://github.com/google/go-licenses/issues/149 for reference. --- tools/sggolicenses/tools.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/sggolicenses/tools.go b/tools/sggolicenses/tools.go index 46e8fa95..4f1f9e8d 100644 --- a/tools/sggolicenses/tools.go +++ b/tools/sggolicenses/tools.go @@ -8,6 +8,7 @@ import ( "io/fs" "os/exec" "path/filepath" + "runtime" "strings" "go.einride.tech/sage/sg" @@ -55,6 +56,16 @@ func Check(ctx context.Context, disallowedTypes ...string) error { } cmd := Command(ctx, args...) cmd.Dir = filepath.Dir(path) + // go-licenses tries to exclude standard library packages by checking if they are prefixed + // with `runtime.GOROOT()`. However, if the go-licenses tool is not run with a GOROOT environment variable, + // that call will return the GOROOT path used during build time of go-licenses. This typically works on Linux, + // but on macOS with Homebrew, the GOROOT is version prefixed, which breaks as soon as Go is upgraded. + // For example: /opt/homebrew/Cellar/go/1.19.4/libexec + // + // As a workaround, add the GOROOT environment variable to the result of `runtime.GOROOT()` called here. + // This should work as the Sage binary is built on the same machine that executes it. + // See: https://github.com/google/go-licenses/issues/149 + cmd.Env = append(cmd.Env, fmt.Sprintf("GOROOT=%s", runtime.GOROOT())) commands = append(commands, cmd) return cmd.Start() }); err != nil {