Skip to content

Commit 74e77b8

Browse files
ldezSeigeC
authored andcommitted
dev: reference all the debug keys and env vars (golangci#3196)
1 parent a12684d commit 74e77b8

30 files changed

+169
-76
lines changed

internal/cache/default.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
package cache
66

77
import (
8-
"errors"
98
"fmt"
109
"log"
1110
"os"
1211
"path/filepath"
1312
"sync"
1413
)
1514

15+
const envGolangciLintCache = "GOLANGCI_LINT_CACHE"
16+
1617
// Default returns the default cache to use.
1718
func Default() (*Cache, error) {
1819
defaultOnce.Do(initDefaultCache)
@@ -65,19 +66,19 @@ func DefaultDir() string {
6566
// otherwise distinguish between an explicit "off" and a UserCacheDir error.
6667

6768
defaultDirOnce.Do(func() {
68-
defaultDir = os.Getenv("GOLANGCI_LINT_CACHE")
69+
defaultDir = os.Getenv(envGolangciLintCache)
6970
if filepath.IsAbs(defaultDir) {
7071
return
7172
}
7273
if defaultDir != "" {
73-
defaultDirErr = errors.New("GOLANGCI_LINT_CACHE is not an absolute path")
74+
defaultDirErr = fmt.Errorf("%s is not an absolute path", envGolangciLintCache)
7475
return
7576
}
7677

7778
// Compute default location.
7879
dir, err := os.UserCacheDir()
7980
if err != nil {
80-
defaultDirErr = fmt.Errorf("GOLANGCI_LINT_CACHE is not defined and %v", err)
81+
defaultDirErr = fmt.Errorf("%s is not defined and %w", envGolangciLintCache, err)
8182
return
8283
}
8384
defaultDir = filepath.Join(dir, "golangci-lint")

pkg/commands/executor.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func NewExecutor(version, commit, date string) *Executor {
6464
commit: commit,
6565
date: date,
6666
DBManager: lintersdb.NewManager(nil, nil),
67-
debugf: logutils.Debug("exec"),
67+
debugf: logutils.Debug(logutils.DebugKeyExec),
6868
}
6969

7070
e.debugf("Starting execution...")
71-
e.log = report.NewLogWrapper(logutils.NewStderrLog(""), &e.reportData)
71+
e.log = report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &e.reportData)
7272

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

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

124124
e.EnabledLintersSet = lintersdb.NewEnabledSet(e.DBManager,
125-
lintersdb.NewValidator(e.DBManager), e.log.Child("lintersdb"), e.cfg)
126-
e.goenv = goutil.NewEnv(e.log.Child("goenv"))
125+
lintersdb.NewValidator(e.DBManager), e.log.Child(logutils.DebugKeyLintersDB), e.cfg)
126+
e.goenv = goutil.NewEnv(e.log.Child(logutils.DebugKeyGoEnv))
127127
e.fileCache = fsutils.NewFileCache()
128128
e.lineCache = fsutils.NewLineCache(e.fileCache)
129129

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

172-
if logutils.HaveDebugTag("bin_salt") {
172+
if logutils.HaveDebugTag(logutils.DebugKeyBinSalt) {
173173
return []byte("debug"), nil
174174
}
175175

pkg/commands/root.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import (
1616
"github.com/golangci/golangci-lint/pkg/logutils"
1717
)
1818

19+
const (
20+
// envHelpRun value: "1".
21+
envHelpRun = "HELP_RUN"
22+
envMemProfileRate = "GL_MEM_PROFILE_RATE"
23+
)
24+
1925
func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) error {
2026
if e.cfg.Run.PrintVersion {
2127
_, _ = fmt.Fprintf(logutils.StdOut, "golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
@@ -35,7 +41,7 @@ func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) error {
3541
}
3642

3743
if e.cfg.Run.MemProfilePath != "" {
38-
if rate := os.Getenv("GL_MEMPROFILE_RATE"); rate != "" {
44+
if rate := os.Getenv(envMemProfileRate); rate != "" {
3945
runtime.MemProfileRate, _ = strconv.Atoi(rate)
4046
}
4147
}
@@ -112,7 +118,7 @@ func formatMemory(memBytes uint64) string {
112118
}
113119

114120
func getDefaultConcurrency() int {
115-
if os.Getenv("HELP_RUN") == "1" {
121+
if os.Getenv(envHelpRun) == "1" {
116122
// Make stable concurrency for README help generating builds.
117123
const prettyConcurrency = 8
118124
return prettyConcurrency

pkg/commands/run.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ import (
2828

2929
const defaultFileMode = 0644
3030

31+
const (
32+
// envFailOnWarnings value: "1"
33+
envFailOnWarnings = "FAIL_ON_WARNINGS"
34+
// envMemLogEvery value: "1"
35+
envMemLogEvery = "GL_MEM_LOG_EVERY"
36+
)
37+
3138
func getDefaultIssueExcludeHelp() string {
3239
parts := []string{"Use or not use default excludes:"}
3340
for _, ep := range config.DefaultExcludePatterns {
@@ -350,9 +357,9 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss
350357
if err != nil {
351358
return nil, errors.Wrap(err, "context loading failed")
352359
}
353-
lintCtx.Log = e.log.Child("linters context")
360+
lintCtx.Log = e.log.Child(logutils.DebugKeyLintersContext)
354361

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

393-
if !logutils.HaveDebugTag("linters_output") {
400+
if !logutils.HaveDebugTag(logutils.DebugKeyLintersOutput) {
394401
// Don't allow linters and loader to print anything
395402
log.SetOutput(io.Discard)
396403
savedStdout, savedStderr := e.setOutputToDevNull()
@@ -474,9 +481,9 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer,
474481
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
475482
p = printers.NewText(e.cfg.Output.PrintIssuedLine,
476483
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName,
477-
e.log.Child("text_printer"), w)
484+
e.log.Child(logutils.DebugKeyTextPrinter), w)
478485
case config.OutFormatTab:
479-
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child("tab_printer"), w)
486+
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child(logutils.DebugKeyTabPrinter), w)
480487
case config.OutFormatCheckstyle:
481488
p = printers.NewCheckstyle(w)
482489
case config.OutFormatCodeClimate:
@@ -545,7 +552,7 @@ func (e *Executor) setupExitCode(ctx context.Context) {
545552
return
546553
}
547554

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

572-
logEveryRecord := os.Getenv("GL_MEM_LOG_EVERY") == "1"
579+
logEveryRecord := os.Getenv(envMemLogEvery) == "1"
573580
const MB = 1024 * 1024
574581

575582
track := func() {

pkg/golinters/commons.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package golinters
33
import "github.com/golangci/golangci-lint/pkg/logutils"
44

55
// linterLogger must be use only when the context logger is not available.
6-
var linterLogger = logutils.NewStderrLog("linter")
6+
var linterLogger = logutils.NewStderrLog(logutils.DebugKeyLinter)

pkg/golinters/goanalysis/runner.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ import (
2828
)
2929

3030
var (
31-
debugf = logutils.Debug("goanalysis")
31+
debugf = logutils.Debug(logutils.DebugKeyGoAnalysis)
3232

33-
analyzeDebugf = logutils.Debug("goanalysis/analyze")
34-
isMemoryDebug = logutils.HaveDebugTag("goanalysis/memory")
35-
issuesCacheDebugf = logutils.Debug("goanalysis/issues/cache")
33+
analyzeDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisAnalyze)
34+
isMemoryDebug = logutils.HaveDebugTag(logutils.DebugKeyGoAnalysisMemory)
35+
issuesCacheDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisIssuesCache)
3636

37-
factsDebugf = logutils.Debug("goanalysis/facts")
38-
factsCacheDebugf = logutils.Debug("goanalysis/facts/cache")
39-
factsInheritDebugf = logutils.Debug("goanalysis/facts/inherit")
40-
factsExportDebugf = logutils.Debug("goanalysis/facts")
41-
isFactsExportDebug = logutils.HaveDebugTag("goanalysis/facts/export")
37+
factsDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFacts)
38+
factsCacheDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFactsCache)
39+
factsInheritDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFactsInherit)
40+
factsExportDebugf = logutils.Debug(logutils.DebugKeyGoAnalysisFacts)
41+
isFactsExportDebug = logutils.HaveDebugTag(logutils.DebugKeyGoAnalysisFactsExport)
4242
)
4343

4444
type Diagnostic struct {

pkg/golinters/goanalysis/runners.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/golangci/golangci-lint/internal/pkgcache"
1616
"github.com/golangci/golangci-lint/pkg/lint/linter"
17+
"github.com/golangci/golangci-lint/pkg/logutils"
1718
"github.com/golangci/golangci-lint/pkg/result"
1819
"github.com/golangci/golangci-lint/pkg/timeutils"
1920
)
@@ -28,7 +29,7 @@ type runAnalyzersConfig interface {
2829
}
2930

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

3435
const stagesToPrint = 10

pkg/golinters/gocritic.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
const goCriticName = "gocritic"
2727

2828
var (
29-
goCriticDebugf = logutils.Debug(goCriticName)
30-
isGoCriticDebug = logutils.HaveDebugTag(goCriticName)
29+
goCriticDebugf = logutils.Debug(logutils.DebugKeyGoCritic)
30+
isGoCriticDebug = logutils.HaveDebugTag(logutils.DebugKeyGoCritic)
3131
)
3232

3333
func NewGoCritic(settings *config.GoCriticSettings, cfg *config.Config) *goanalysis.Linter {

pkg/golinters/revive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
const reviveName = "revive"
2727

28-
var reviveDebugf = logutils.Debug("revive")
28+
var reviveDebugf = logutils.Debug(logutils.DebugKeyRevive)
2929

3030
// jsonObject defines a JSON object of a failure
3131
type jsonObject struct {

pkg/golinters/staticcheck_common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/golangci/golangci-lint/pkg/logutils"
1313
)
1414

15-
var debugf = logutils.Debug("megacheck")
15+
var debugf = logutils.Debug(logutils.DebugKeyMegacheck)
1616

1717
func getGoVersion(settings *config.StaticCheckSettings) string {
1818
var goVersion string

pkg/goutil/env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewEnv(log logutils.Log) *Env {
3030
return &Env{
3131
vars: map[string]string{},
3232
log: log,
33-
debugf: logutils.Debug("env"),
33+
debugf: logutils.Debug(logutils.DebugKeyEnv),
3434
}
3535
}
3636

pkg/lint/lintersdb/enabled_set.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"github.com/golangci/golangci-lint/pkg/logutils"
1111
)
1212

13+
// EnvTestRun value: "1"
14+
const EnvTestRun = "GL_TEST_RUN"
15+
1316
type EnabledSet struct {
1417
m *Manager
1518
v *Validator
@@ -24,7 +27,7 @@ func NewEnabledSet(m *Manager, v *Validator, log logutils.Log, cfg *config.Confi
2427
v: v,
2528
log: log,
2629
cfg: cfg,
27-
debugf: logutils.Debug("enabled_linters"),
30+
debugf: logutils.Debug(logutils.DebugKeyEnabledLinters),
2831
}
2932
}
3033

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

8689
enabledLinters := es.build(&es.cfg.Linters, es.m.GetAllEnabledByDefaultLinters())
87-
if os.Getenv("GL_TEST_RUN") == "1" {
90+
if os.Getenv(EnvTestRun) == "1" {
8891
es.verbosePrintLintersStatus(enabledLinters)
8992
}
9093
return enabledLinters, nil

pkg/lint/lintersdb/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewManager(cfg *config.Config, log logutils.Log) *Manager {
3838
// WithCustomLinters loads private linters that are specified in the golangci config file.
3939
func (m *Manager) WithCustomLinters() *Manager {
4040
if m.log == nil {
41-
m.log = report.NewLogWrapper(logutils.NewStderrLog(""), &report.Data{})
41+
m.log = report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
4242
}
4343
if m.cfg != nil {
4444
for name, settings := range m.cfg.LintersSettings.Custom {

pkg/lint/load.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewContextLoader(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
4141
return &ContextLoader{
4242
cfg: cfg,
4343
log: log,
44-
debugf: logutils.Debug("loader"),
44+
debugf: logutils.Debug(logutils.DebugKeyLoader),
4545
goenv: goenv,
4646
pkgTestIDRe: regexp.MustCompile(`^(.*) \[(.*)\.test\]`),
4747
lineCache: lineCache,
@@ -59,7 +59,7 @@ func (cl *ContextLoader) prepareBuildContext() {
5959
return
6060
}
6161

62-
os.Setenv("GOROOT", goroot)
62+
os.Setenv(string(goutil.EnvGoRoot), goroot)
6363
build.Default.GOROOT = goroot
6464
build.Default.BuildTags = cl.cfg.Run.BuildTags
6565
}

0 commit comments

Comments
 (0)