Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: reference all the debug keys and env vars #3196

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions internal/cache/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
package cache

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"sync"
)

const envGolangciLintCache = "GOLANGCI_LINT_CACHE"

// Default returns the default cache to use.
func Default() (*Cache, error) {
defaultOnce.Do(initDefaultCache)
Expand Down Expand Up @@ -65,19 +66,19 @@ func DefaultDir() string {
// otherwise distinguish between an explicit "off" and a UserCacheDir error.

defaultDirOnce.Do(func() {
defaultDir = os.Getenv("GOLANGCI_LINT_CACHE")
defaultDir = os.Getenv(envGolangciLintCache)
if filepath.IsAbs(defaultDir) {
return
}
if defaultDir != "" {
defaultDirErr = errors.New("GOLANGCI_LINT_CACHE is not an absolute path")
defaultDirErr = fmt.Errorf("%s is not an absolute path", envGolangciLintCache)
return
}

// Compute default location.
dir, err := os.UserCacheDir()
if err != nil {
defaultDirErr = fmt.Errorf("GOLANGCI_LINT_CACHE is not defined and %v", err)
defaultDirErr = fmt.Errorf("%s is not defined and %w", envGolangciLintCache, err)
return
}
defaultDir = filepath.Join(dir, "golangci-lint")
Expand Down
18 changes: 9 additions & 9 deletions pkg/commands/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func NewExecutor(version, commit, date string) *Executor {
commit: commit,
date: date,
DBManager: lintersdb.NewManager(nil, nil),
debugf: logutils.Debug("exec"),
debugf: logutils.Debug(logutils.DebugKeyExec),
}

e.debugf("Starting execution...")
e.log = report.NewLogWrapper(logutils.NewStderrLog(""), &e.reportData)
e.log = report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &e.reportData)

// to setup log level early we need to parse config from command line extra time to
// find `-v` option
Expand Down Expand Up @@ -105,7 +105,7 @@ func NewExecutor(version, commit, date string) *Executor {
// like the default ones. It will overwrite them only if the same option
// is found in command-line: it's ok, command-line has higher priority.

r := config.NewFileReader(e.cfg, commandLineCfg, e.log.Child("config_reader"))
r := config.NewFileReader(e.cfg, commandLineCfg, e.log.Child(logutils.DebugKeyConfigReader))
if err = r.Read(); err != nil {
e.log.Fatalf("Can't read config: %s", err)
}
Expand All @@ -122,18 +122,18 @@ func NewExecutor(version, commit, date string) *Executor {
fixSlicesFlags(e.lintersCmd.Flags())

e.EnabledLintersSet = lintersdb.NewEnabledSet(e.DBManager,
lintersdb.NewValidator(e.DBManager), e.log.Child("lintersdb"), e.cfg)
e.goenv = goutil.NewEnv(e.log.Child("goenv"))
lintersdb.NewValidator(e.DBManager), e.log.Child(logutils.DebugKeyLintersDB), e.cfg)
e.goenv = goutil.NewEnv(e.log.Child(logutils.DebugKeyGoEnv))
e.fileCache = fsutils.NewFileCache()
e.lineCache = fsutils.NewLineCache(e.fileCache)

e.sw = timeutils.NewStopwatch("pkgcache", e.log.Child("stopwatch"))
e.pkgCache, err = pkgcache.NewCache(e.sw, e.log.Child("pkgcache"))
e.sw = timeutils.NewStopwatch("pkgcache", e.log.Child(logutils.DebugKeyStopwatch))
e.pkgCache, err = pkgcache.NewCache(e.sw, e.log.Child(logutils.DebugKeyPkgCache))
if err != nil {
e.log.Fatalf("Failed to build packages cache: %s", err)
}
e.loadGuard = load.NewGuard()
e.contextLoader = lint.NewContextLoader(e.cfg, e.log.Child("loader"), e.goenv,
e.contextLoader = lint.NewContextLoader(e.cfg, e.log.Child(logutils.DebugKeyLoader), e.goenv,
e.lineCache, e.fileCache, e.pkgCache, e.loadGuard)
if err = e.initHashSalt(version); err != nil {
e.log.Fatalf("Failed to init hash salt: %s", err)
Expand Down Expand Up @@ -169,7 +169,7 @@ func computeBinarySalt(version string) ([]byte, error) {
return []byte(version), nil
}

if logutils.HaveDebugTag("bin_salt") {
if logutils.HaveDebugTag(logutils.DebugKeyBinSalt) {
return []byte("debug"), nil
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
"github.com/golangci/golangci-lint/pkg/logutils"
)

const (
// envHelpRun value: "1".
envHelpRun = "HELP_RUN"
envMemProfileRate = "GL_MEM_PROFILE_RATE"
)

func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) error {
if e.cfg.Run.PrintVersion {
_, _ = fmt.Fprintf(logutils.StdOut, "golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
Expand All @@ -35,7 +41,7 @@ func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) error {
}

if e.cfg.Run.MemProfilePath != "" {
if rate := os.Getenv("GL_MEMPROFILE_RATE"); rate != "" {
if rate := os.Getenv(envMemProfileRate); rate != "" {
runtime.MemProfileRate, _ = strconv.Atoi(rate)
}
}
Expand Down Expand Up @@ -112,7 +118,7 @@ func formatMemory(memBytes uint64) string {
}

func getDefaultConcurrency() int {
if os.Getenv("HELP_RUN") == "1" {
if os.Getenv(envHelpRun) == "1" {
// Make stable concurrency for README help generating builds.
const prettyConcurrency = 8
return prettyConcurrency
Expand Down
21 changes: 14 additions & 7 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import (

const defaultFileMode = 0644

const (
// envFailOnWarnings value: "1"
envFailOnWarnings = "FAIL_ON_WARNINGS"
// envMemLogEvery value: "1"
envMemLogEvery = "GL_MEM_LOG_EVERY"
)

func getDefaultIssueExcludeHelp() string {
parts := []string{"Use or not use default excludes:"}
for _, ep := range config.DefaultExcludePatterns {
Expand Down Expand Up @@ -350,9 +357,9 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss
if err != nil {
return nil, errors.Wrap(err, "context loading failed")
}
lintCtx.Log = e.log.Child("linters context")
lintCtx.Log = e.log.Child(logutils.DebugKeyLintersContext)

runner, err := lint.NewRunner(e.cfg, e.log.Child("runner"),
runner, err := lint.NewRunner(e.cfg, e.log.Child(logutils.DebugKeyRunner),
e.goenv, e.EnabledLintersSet, e.lineCache, e.DBManager, lintCtx.Packages)
if err != nil {
return nil, err
Expand Down Expand Up @@ -390,7 +397,7 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
e.log.Warnf("Failed to discover go env: %s", err)
}

if !logutils.HaveDebugTag("linters_output") {
if !logutils.HaveDebugTag(logutils.DebugKeyLintersOutput) {
// Don't allow linters and loader to print anything
log.SetOutput(io.Discard)
savedStdout, savedStderr := e.setOutputToDevNull()
Expand Down Expand Up @@ -474,9 +481,9 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer,
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
p = printers.NewText(e.cfg.Output.PrintIssuedLine,
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName,
e.log.Child("text_printer"), w)
e.log.Child(logutils.DebugKeyTextPrinter), w)
case config.OutFormatTab:
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child("tab_printer"), w)
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child(logutils.DebugKeyTabPrinter), w)
case config.OutFormatCheckstyle:
p = printers.NewCheckstyle(w)
case config.OutFormatCodeClimate:
Expand Down Expand Up @@ -545,7 +552,7 @@ func (e *Executor) setupExitCode(ctx context.Context) {
return
}

needFailOnWarnings := os.Getenv("GL_TEST_RUN") == "1" || os.Getenv("FAIL_ON_WARNINGS") == "1"
needFailOnWarnings := os.Getenv(lintersdb.EnvTestRun) == "1" || os.Getenv(envFailOnWarnings) == "1"
if needFailOnWarnings && len(e.reportData.Warnings) != 0 {
e.exitCode = exitcodes.WarningInTest
return
Expand All @@ -569,7 +576,7 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
ticker := time.NewTicker(intervalMS * time.Millisecond)
defer ticker.Stop()

logEveryRecord := os.Getenv("GL_MEM_LOG_EVERY") == "1"
logEveryRecord := os.Getenv(envMemLogEvery) == "1"
const MB = 1024 * 1024

track := func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package golinters
import "github.com/golangci/golangci-lint/pkg/logutils"

// linterLogger must be use only when the context logger is not available.
var linterLogger = logutils.NewStderrLog("linter")
var linterLogger = logutils.NewStderrLog(logutils.DebugKeyLinter)
18 changes: 9 additions & 9 deletions pkg/golinters/goanalysis/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ import (
)

var (
debugf = logutils.Debug("goanalysis")
debugf = logutils.Debug(logutils.DebugKeyGoAnalysis)

analyzeDebugf = logutils.Debug("goanalysis/analyze")
isMemoryDebug = logutils.HaveDebugTag("goanalysis/memory")
issuesCacheDebugf = logutils.Debug("goanalysis/issues/cache")
analyzeDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisAnalyze)
isMemoryDebug = logutils.HaveDebugTag(logutils.DebugKeyGoAnalysisMemory)
issuesCacheDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisIssuesCache)

factsDebugf = logutils.Debug("goanalysis/facts")
factsCacheDebugf = logutils.Debug("goanalysis/facts/cache")
factsInheritDebugf = logutils.Debug("goanalysis/facts/inherit")
factsExportDebugf = logutils.Debug("goanalysis/facts")
isFactsExportDebug = logutils.HaveDebugTag("goanalysis/facts/export")
factsDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFacts)
factsCacheDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFactsCache)
factsInheritDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFactsInherit)
factsExportDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFacts)
isFactsExportDebug = logutils.HaveDebugTag(logutils.DebugKeyGoAnalysisFactsExport)
)

type Diagnostic struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/golinters/goanalysis/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/golangci/golangci-lint/internal/pkgcache"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
"github.com/golangci/golangci-lint/pkg/timeutils"
)
Expand All @@ -28,7 +29,7 @@ type runAnalyzersConfig interface {
}

func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Issue, error) {
log := lintCtx.Log.Child("goanalysis")
log := lintCtx.Log.Child(logutils.DebugKeyGoAnalysis)
sw := timeutils.NewStopwatch("analyzers", log)

const stagesToPrint = 10
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
const goCriticName = "gocritic"

var (
goCriticDebugf = logutils.Debug(goCriticName)
isGoCriticDebug = logutils.HaveDebugTag(goCriticName)
goCriticDebugf = logutils.Debug(logutils.DebugKeyGoCritic)
isGoCriticDebug = logutils.HaveDebugTag(logutils.DebugKeyGoCritic)
)

func NewGoCritic(settings *config.GoCriticSettings, cfg *config.Config) *goanalysis.Linter {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

const reviveName = "revive"

var reviveDebugf = logutils.Debug("revive")
var reviveDebugf = logutils.Debug(logutils.DebugKeyRevive)

// jsonObject defines a JSON object of a failure
type jsonObject struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/staticcheck_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/golangci/golangci-lint/pkg/logutils"
)

var debugf = logutils.Debug("megacheck")
var debugf = logutils.Debug(logutils.DebugKeyMegacheck)

func getGoVersion(settings *config.StaticCheckSettings) string {
var goVersion string
Expand Down
2 changes: 1 addition & 1 deletion pkg/goutil/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewEnv(log logutils.Log) *Env {
return &Env{
vars: map[string]string{},
log: log,
debugf: logutils.Debug("env"),
debugf: logutils.Debug(logutils.DebugKeyEnv),
}
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/lint/lintersdb/enabled_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/golangci/golangci-lint/pkg/logutils"
)

// EnvTestRun value: "1"
const EnvTestRun = "GL_TEST_RUN"

type EnabledSet struct {
m *Manager
v *Validator
Expand All @@ -24,7 +27,7 @@ func NewEnabledSet(m *Manager, v *Validator, log logutils.Log, cfg *config.Confi
v: v,
log: log,
cfg: cfg,
debugf: logutils.Debug("enabled_linters"),
debugf: logutils.Debug(logutils.DebugKeyEnabledLinters),
}
}

Expand Down Expand Up @@ -84,7 +87,7 @@ func (es EnabledSet) GetEnabledLintersMap() (map[string]*linter.Config, error) {
}

enabledLinters := es.build(&es.cfg.Linters, es.m.GetAllEnabledByDefaultLinters())
if os.Getenv("GL_TEST_RUN") == "1" {
if os.Getenv(EnvTestRun) == "1" {
es.verbosePrintLintersStatus(enabledLinters)
}
return enabledLinters, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewManager(cfg *config.Config, log logutils.Log) *Manager {
// WithCustomLinters loads private linters that are specified in the golangci config file.
func (m *Manager) WithCustomLinters() *Manager {
if m.log == nil {
m.log = report.NewLogWrapper(logutils.NewStderrLog(""), &report.Data{})
m.log = report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
}
if m.cfg != nil {
for name, settings := range m.cfg.LintersSettings.Custom {
Expand Down
4 changes: 2 additions & 2 deletions pkg/lint/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewContextLoader(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
return &ContextLoader{
cfg: cfg,
log: log,
debugf: logutils.Debug("loader"),
debugf: logutils.Debug(logutils.DebugKeyLoader),
goenv: goenv,
pkgTestIDRe: regexp.MustCompile(`^(.*) \[(.*)\.test\]`),
lineCache: lineCache,
Expand All @@ -59,7 +59,7 @@ func (cl *ContextLoader) prepareBuildContext() {
return
}

os.Setenv("GOROOT", goroot)
os.Setenv(string(goutil.EnvGoRoot), goroot)
build.Default.GOROOT = goroot
build.Default.BuildTags = cl.cfg.Run.BuildTags
}
Expand Down
Loading