Skip to content

Commit 2f1cdcc

Browse files
author
golangci
authored
Merge pull request #35 from golangci/support/fix-cross-compilation-goroot
Support/fix cross compilation goroot
2 parents eb7998a + 96a1920 commit 2f1cdcc

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ Sponsored by [GolangCI.com](https://golangci.com): SaaS service for running lint
3232
* [Contact Information](#contact-information)
3333

3434
# Install
35+
Recommended way to install is:
3536
```bash
3637
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
3738
```
3839

40+
You can also install it by brew:
41+
```bash
42+
brew install golangci/tap/golangci-lint
43+
```
44+
45+
Check the [releases page](https://github.com/golangci/golangci-lint/releases) to fix the version.
46+
3947
# Demo
4048
Example of output:
4149
![Screenshot of sample output](docs/run_screenshot.png)

pkg/commands/run.go

+25
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)