Skip to content

Commit ae5152c

Browse files
committed
fix GOROOT after cross-compilation
1 parent eb7998a commit ae5152c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pkg/commands/run.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"log"
1111
"os"
12+
"os/exec"
1213
"runtime"
1314
"strings"
1415
"time"
@@ -175,7 +176,31 @@ func buildSSAProgram(ctx context.Context, lprog *loader.Program) *ssa.Program {
175176
return ssaProg
176177
}
177178

179+
func discoverGoRoot() (string, error) {
180+
goroot := os.Getenv("GOROOT")
181+
if goroot != "" {
182+
return goroot, nil
183+
}
184+
185+
output, err := exec.Command("go", "env", "GOROOT").Output()
186+
if err != nil {
187+
return "", fmt.Errorf("can't execute go env GOROOT: %s", err)
188+
}
189+
190+
return strings.TrimSpace(string(output)), nil
191+
}
192+
178193
func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config) (*golinters.Context, error) {
194+
// Set GOROOT to have working cross-compilation: cross-compiled binaries
195+
// have invalid GOROOT. XXX: can't use runtime.GOROOT().
196+
goroot, err := discoverGoRoot()
197+
if err != nil {
198+
return nil, fmt.Errorf("can't discover GOROOT: %s", err)
199+
}
200+
os.Setenv("GOROOT", goroot)
201+
build.Default.GOROOT = goroot
202+
logrus.Infof("set GOROOT=%q", goroot)
203+
179204
args := cfg.Run.Args
180205
if len(args) == 0 {
181206
args = []string{"./..."}

0 commit comments

Comments
 (0)