diff --git a/go.mod b/go.mod index 5b7e67765faf..6ae6d433ccd4 100644 --- a/go.mod +++ b/go.mod @@ -66,8 +66,8 @@ require ( github.com/kyoh86/exportloopref v0.1.11 github.com/lasiar/canonicalheader v1.1.2 github.com/ldez/exptostd v0.3.1 - github.com/ldez/gomoddirectives v0.6.0 - github.com/ldez/grignotin v0.8.0 + github.com/ldez/gomoddirectives v0.6.1 + github.com/ldez/grignotin v0.9.0 github.com/ldez/tagliatelle v0.7.1 github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 diff --git a/go.sum b/go.sum index 4b6e02ad4f52..ea1e8d950a6b 100644 --- a/go.sum +++ b/go.sum @@ -353,10 +353,10 @@ github.com/lasiar/canonicalheader v1.1.2 h1:vZ5uqwvDbyJCnMhmFYimgMZnJMjwljN5VGY0 github.com/lasiar/canonicalheader v1.1.2/go.mod h1:qJCeLFS0G/QlLQ506T+Fk/fWMa2VmBUiEI2cuMK4djI= github.com/ldez/exptostd v0.3.1 h1:90yWWoAKMFHeovTK8uzBms9Ppp8Du/xQ20DRO26Ymrw= github.com/ldez/exptostd v0.3.1/go.mod h1:iZBRYaUmcW5jwCR3KROEZ1KivQQp6PHXbDPk9hqJKCQ= -github.com/ldez/gomoddirectives v0.6.0 h1:Jyf1ZdTeiIB4dd+2n4qw+g4aI9IJ6JyfOZ8BityWvnA= -github.com/ldez/gomoddirectives v0.6.0/go.mod h1:TuwOGYoPAoENDWQpe8DMqEm5nIfjrxZXmxX/CExWyZ4= -github.com/ldez/grignotin v0.8.0 h1:M9QeBN2qyPrqwqx+RhG7W2xKjyV7dRWKjNghbz7fkM0= -github.com/ldez/grignotin v0.8.0/go.mod h1:uaVTr0SoZ1KBii33c47O1M8Jp3OP3YDwhZCmzT9GHEk= +github.com/ldez/gomoddirectives v0.6.1 h1:Z+PxGAY+217f/bSGjNZr/b2KTXcyYLgiWI6geMBN2Qc= +github.com/ldez/gomoddirectives v0.6.1/go.mod h1:cVBiu3AHR9V31em9u2kwfMKD43ayN5/XDgr+cdaFaKs= +github.com/ldez/grignotin v0.9.0 h1:MgOEmjZIVNn6p5wPaGp/0OKWyvq42KnzAt/DAb8O4Ow= +github.com/ldez/grignotin v0.9.0/go.mod h1:uaVTr0SoZ1KBii33c47O1M8Jp3OP3YDwhZCmzT9GHEk= github.com/ldez/tagliatelle v0.7.1 h1:bTgKjjc2sQcsgPiT902+aadvMjCeMHrY7ly2XKFORIk= github.com/ldez/tagliatelle v0.7.1/go.mod h1:3zjxUpsNB2aEZScWiZTHrAXOl1x25t3cRmzfK1mlo2I= github.com/ldez/usetesting v0.4.2 h1:J2WwbrFGk3wx4cZwSMiCQQ00kjGR0+tuuyW0Lqm4lwA= diff --git a/pkg/config/config.go b/pkg/config/config.go index c846611838a1..339007b62fd3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,6 +2,7 @@ package config import ( "cmp" + "context" "fmt" "os" "path/filepath" @@ -80,8 +81,8 @@ func IsGoGreaterThanOrEqual(current, limit string) bool { return v1.GreaterThanOrEqual(l) } -func detectGoVersion() string { - return cmp.Or(detectGoVersionFromGoMod(), "1.17") +func detectGoVersion(ctx context.Context) string { + return cmp.Or(detectGoVersionFromGoMod(ctx), "1.17") } // detectGoVersionFromGoMod tries to get Go version from go.mod. @@ -89,11 +90,11 @@ func detectGoVersion() string { // else it returns `go` version if present, // else it returns `GOVERSION` version if present, // else it returns empty. -func detectGoVersionFromGoMod() string { - values, err := goenv.Get(goenv.GOMOD, goenv.GOVERSION) +func detectGoVersionFromGoMod(ctx context.Context) string { + values, err := goenv.Get(ctx, goenv.GOMOD, goenv.GOVERSION) if err != nil { values = map[string]string{ - goenv.GOMOD: detectGoModFallback(), + goenv.GOMOD: detectGoModFallback(ctx), } } @@ -143,8 +144,8 @@ func parseGoMod(goMod string) (*modfile.File, error) { return modfile.Parse("go.mod", raw, nil) } -func detectGoModFallback() string { - info, err := gomod.GetModuleInfo() +func detectGoModFallback(ctx context.Context) string { + info, err := gomod.GetModuleInfo(ctx) if err != nil { return "" } diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 56f57d9d5dd7..3d1f76d12576 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -2,6 +2,7 @@ package config import ( "cmp" + "context" "errors" "fmt" "os" @@ -286,7 +287,7 @@ func (l *Loader) appendStringSlice(name string, current *[]string) { func (l *Loader) handleGoVersion() { if l.cfg.Run.Go == "" { - l.cfg.Run.Go = detectGoVersion() + l.cfg.Run.Go = detectGoVersion(context.Background()) } l.cfg.LintersSettings.Govet.Go = l.cfg.Run.Go diff --git a/pkg/goformatters/gci/gci.go b/pkg/goformatters/gci/gci.go index 590f8da2a0e6..f872c3c667f6 100644 --- a/pkg/goformatters/gci/gci.go +++ b/pkg/goformatters/gci/gci.go @@ -1,6 +1,7 @@ package gci import ( + "context" "fmt" gcicfg "github.com/daixiang0/gci/pkg/config" @@ -22,7 +23,7 @@ func New(settings *config.GciSettings) (*Formatter, error) { log.InitLogger() _ = log.L().Sync() - modPath, err := gomod.GetModulePath() + modPath, err := gomod.GetModulePath(context.Background()) if err != nil { internal.FormatterLogger.Errorf("gci: %v", err) } diff --git a/pkg/goutil/env.go b/pkg/goutil/env.go index 7b748d8e9031..beb71f722e0a 100644 --- a/pkg/goutil/env.go +++ b/pkg/goutil/env.go @@ -2,53 +2,39 @@ package goutil import ( "context" - "encoding/json" "fmt" "os" - "os/exec" - "strings" "time" + "github.com/ldez/grignotin/goenv" + "github.com/golangci/golangci-lint/pkg/logutils" ) type EnvKey string -const ( - EnvGoCache EnvKey = "GOCACHE" - EnvGoRoot EnvKey = "GOROOT" -) - type Env struct { - vars map[string]string - log logutils.Log - debugf logutils.DebugFunc + vars map[string]string + log logutils.Log } func NewEnv(log logutils.Log) *Env { return &Env{ - vars: map[string]string{}, - log: log, - debugf: logutils.Debug(logutils.DebugKeyEnv), + vars: map[string]string{}, + log: log, } } func (e Env) Discover(ctx context.Context) error { startedAt := time.Now() - //nolint:gosec // Everything is static here. - cmd := exec.CommandContext(ctx, "go", "env", "-json", string(EnvGoCache), string(EnvGoRoot)) - - out, err := cmd.Output() + var err error + e.vars, err = goenv.Get(ctx, goenv.GOCACHE, goenv.GOROOT) if err != nil { - return fmt.Errorf("failed to run '%s': %w", strings.Join(cmd.Args, " "), err) - } - - if err = json.Unmarshal(out, &e.vars); err != nil { - return fmt.Errorf("failed to parse '%s' json: %w", strings.Join(cmd.Args, " "), err) + return fmt.Errorf("%w", err) } - e.debugf("Read go env for %s: %#v", time.Since(startedAt), e.vars) + e.log.Infof("Read go env for %s: %#v", time.Since(startedAt), e.vars) return nil } diff --git a/pkg/lint/package.go b/pkg/lint/package.go index c314166cae37..736498b0b4d4 100644 --- a/pkg/lint/package.go +++ b/pkg/lint/package.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/ldez/grignotin/goenv" "golang.org/x/tools/go/packages" "github.com/golangci/golangci-lint/pkg/config" @@ -204,12 +205,13 @@ func (l *PackageLoader) debugPrintLoadedPackages(pkgs []*packages.Package) { func (l *PackageLoader) prepareBuildContext() { // Set GOROOT to have working cross-compilation: cross-compiled binaries // have invalid GOROOT. XXX: can't use runtime.GOROOT(). - goroot := l.goenv.Get(goutil.EnvGoRoot) + goroot := l.goenv.Get(goenv.GOROOT) if goroot == "" { return } - os.Setenv(string(goutil.EnvGoRoot), goroot) + _ = os.Setenv(goenv.GOROOT, goroot) + build.Default.GOROOT = goroot build.Default.BuildTags = l.cfg.Run.BuildTags } diff --git a/pkg/logutils/logutils.go b/pkg/logutils/logutils.go index 9ae402047f55..b5d465de59f2 100644 --- a/pkg/logutils/logutils.go +++ b/pkg/logutils/logutils.go @@ -15,39 +15,47 @@ const EnvTestRun = "GL_TEST_RUN" // - Some analysis details: `GL_DEBUG=goanalysis/analyze,goanalysis/facts golangci-lint run` const envDebug = "GL_DEBUG" +const ( + DebugKeyBinSalt = "bin_salt" + DebugKeyConfigReader = "config_reader" + DebugKeyEmpty = "" + DebugKeyEnabledLinters = "enabled_linters" + DebugKeyExec = "exec" + DebugKeyFormatter = "formatter" + DebugKeyGoEnv = "goenv" + DebugKeyLinter = "linter" + DebugKeyLintersContext = "linters_context" + DebugKeyLintersDB = "lintersdb" + DebugKeyLintersOutput = "linters_output" + DebugKeyLoader = "loader" // Debugs packages loading (including `go/packages` internal debugging). + DebugKeyPkgCache = "pkgcache" + DebugKeyRunner = "runner" + DebugKeyStopwatch = "stopwatch" + DebugKeyTest = "test" +) + +// Printers. +const ( + DebugKeyTabPrinter = "tab_printer" + DebugKeyTextPrinter = "text_printer" +) + +// Processors. const ( DebugKeyAutogenExclude = "autogen_exclude" // Debugs a filter excluding autogenerated source code. - DebugKeyBinSalt = "bin_salt" - DebugKeyConfigReader = "config_reader" - DebugKeyEmpty = "" - DebugKeyEnabledLinters = "enabled_linters" - DebugKeyEnv = "env" // Debugs `go env` command. DebugKeyExcludeRules = "exclude_rules" - DebugKeyExec = "exec" DebugKeyFilenameUnadjuster = "filename_unadjuster" - DebugKeyFormatter = "formatter" - DebugKeyGoEnv = "goenv" DebugKeyInvalidIssue = "invalid_issue" - DebugKeyLinter = "linter" - DebugKeyLintersContext = "linters_context" - DebugKeyLintersDB = "lintersdb" - DebugKeyLintersOutput = "linters_output" - DebugKeyLoader = "loader" // Debugs packages loading (including `go/packages` internal debugging). DebugKeyMaxFromLinter = "max_from_linter" DebugKeyMaxSameIssues = "max_same_issues" DebugKeyPathAbsoluter = "path_absoluter" DebugKeyPathPrettifier = "path_prettifier" - DebugKeyPkgCache = "pkgcache" - DebugKeyRunner = "runner" DebugKeySeverityRules = "severity_rules" DebugKeySkipDirs = "skip_dirs" DebugKeySourceCode = "source_code" - DebugKeyStopwatch = "stopwatch" - DebugKeyTabPrinter = "tab_printer" - DebugKeyTest = "test" - DebugKeyTextPrinter = "text_printer" ) +// Analysis. const ( DebugKeyGoAnalysis = "goanalysis" @@ -61,6 +69,7 @@ const ( DebugKeyGoAnalysisFactsInherit = DebugKeyGoAnalysisFacts + "/inherit" ) +// Linters. const ( DebugKeyForbidigo = "forbidigo" // Debugs `forbidigo` linter. DebugKeyGoCritic = "gocritic" // Debugs `gocritic` linter. diff --git a/pkg/result/processors/cgo.go b/pkg/result/processors/cgo.go index 34dc8a7cfb92..b09b8b72837a 100644 --- a/pkg/result/processors/cgo.go +++ b/pkg/result/processors/cgo.go @@ -4,6 +4,8 @@ import ( "path/filepath" "strings" + "github.com/ldez/grignotin/goenv" + "github.com/golangci/golangci-lint/pkg/goutil" "github.com/golangci/golangci-lint/pkg/result" ) @@ -19,9 +21,9 @@ type Cgo struct { goCacheDir string } -func NewCgo(goenv *goutil.Env) *Cgo { +func NewCgo(env *goutil.Env) *Cgo { return &Cgo{ - goCacheDir: goenv.Get(goutil.EnvGoCache), + goCacheDir: env.Get(goenv.GOCACHE), } }