Skip to content

Commit

Permalink
fix(go-licences): fix running on macOS when Go is upgraded
Browse files Browse the repository at this point in the history
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 google/go-licenses#149 for reference.
  • Loading branch information
radhus committed Jan 4, 2023
1 parent 8d24b85 commit 35a67ce
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tools/sggolicenses/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"
"os/exec"
"path/filepath"
"runtime"
"strings"

"go.einride.tech/sage/sg"
Expand Down Expand Up @@ -55,6 +56,17 @@ 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 {
Expand Down

0 comments on commit 35a67ce

Please sign in to comment.