Skip to content

feat: new default values #5470

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

Merged
merged 7 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 7 additions & 7 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4123,15 +4123,15 @@ output:
- file # filepath, line, and column.

# Show statistics per linter.
# Default: false
show-stats: true
# Default: true
show-stats: false


# Options for analysis running.
run:
# Timeout for analysis, e.g. 30s, 5m.
# If the value is lower or equal to 0, the timeout is disabled.
# Default: 1m
# Default: 0 (disabled)
timeout: 5m

# The mode used to evaluate relative paths.
Expand Down Expand Up @@ -4182,12 +4182,12 @@ run:

# Define the Go version limit.
# Mainly related to generics support since go1.18.
# Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.17
go: '1.19'
# Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.22.
go: '1.22'

# Number of operating system threads (`GOMAXPROCS`) that can execute golangci-lint simultaneously.
# If it is explicitly set to 0 (i.e. not the default) then golangci-lint will automatically set the value to match Linux container CPU quota.
# Default: the number of logical CPUs in the machine
# Default: automatically set to match Linux container CPU quota and
# fall back to the number of logical CPUs in the machine.
concurrency: 4


Expand Down
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,3 @@ linters-settings:
- name: unused-parameter
- name: unused-receiver

run:
timeout: 5m
6 changes: 3 additions & 3 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3744,10 +3744,10 @@
"examples": [4]
},
"timeout": {
"description": "Timeout for the analysis.",
"description": "Timeout for total work. Disabled by default",
"type": "string",
"pattern": "^\\d*[sm]$",
"default": "1m",
"default": "0",
"examples": ["30s", "5m"]
},
"issues-exit-code": {
Expand Down Expand Up @@ -3888,7 +3888,7 @@
"show-stats": {
"description": "Show statistics per linter.",
"type": "boolean",
"default": false
"default": true
},
"sort-order": {
"type": "array",
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/flagsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ func setupFormattersFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
}

func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddFlagAndBindP(v, fs, fs.IntP, "concurrency", "j", "run.concurrency", getDefaultConcurrency(),
color.GreenString("Number of CPUs to use (Default: number of logical CPUs)"))
internal.AddFlagAndBindP(v, fs, fs.IntP, "concurrency", "j", "run.concurrency", 0,
color.GreenString("Number of CPUs to use (Default: Automatically set to match Linux container CPU quota,"+
" fall backs on number of logical CPUs)"))

internal.AddFlagAndBind(v, fs, fs.String, "modules-download-mode", "run.modules-download-mode", "",
color.GreenString("Modules download mode. If not empty, passed as -mod=<mode> to go tools"))
internal.AddFlagAndBind(v, fs, fs.Int, "issues-exit-code", "run.issues-exit-code", exitcodes.IssuesFound,
color.GreenString("Exit code when issues were found"))
internal.AddFlagAndBind(v, fs, fs.String, "go", "run.go", "", color.GreenString("Targeted Go version"))
internal.AddHackedStringSlice(fs, "build-tags", color.GreenString("Build tags"))

internal.AddFlagAndBind(v, fs, fs.Duration, "timeout", "run.timeout", defaultTimeout,
color.GreenString("Timeout for total work. If <= 0, the timeout is disabled"))
color.GreenString("Timeout for total work. Disabled by default"))

internal.AddFlagAndBind(v, fs, fs.Bool, "tests", "run.tests", true, color.GreenString("Analyze tests (*_test.go)"))

Expand All @@ -72,7 +72,7 @@ func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddFlagAndBind(v, fs, fs.String, "path-prefix", "output.path-prefix", "",
color.GreenString("Path prefix to add to output"))
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", false, color.GreenString("Show statistics per linter"))
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", true, color.GreenString("Show statistics per linter"))

setupOutputFormatsFlagSet(v, fs)
}
Expand Down
18 changes: 4 additions & 14 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"github.com/golangci/golangci-lint/pkg/timeutils"
)

const defaultTimeout = time.Minute
const defaultTimeout = 0 * time.Minute

const (
// envFailOnWarnings value: "1"
Expand Down Expand Up @@ -161,6 +161,7 @@ func (c *runCommand) persistentPreRunE(cmd *cobra.Command, args []string) error
}

if c.cfg.Run.Concurrency == 0 {
// `runtime.GOMAXPROCS` defaults to the value of `runtime.NumCPU`.
backup := runtime.GOMAXPROCS(0)

// Automatically set GOMAXPROCS to match Linux container CPU quota.
Expand Down Expand Up @@ -245,12 +246,11 @@ func (c *runCommand) execute(_ *cobra.Command, _ []string) {
}
}()

ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
if c.cfg.Run.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.cfg.Run.Timeout)
defer cancel()
}
defer cancel()

if needTrackResources {
go watchResources(ctx, trackResourcesEndCh, c.log, c.debugf)
Expand Down Expand Up @@ -590,16 +590,6 @@ func setupRunPersistentFlags(fs *pflag.FlagSet, opts *runOptions) {
fs.StringVar(&opts.TracePath, "trace-path", "", color.GreenString("Path to trace output file"))
}

func getDefaultConcurrency() int {
if os.Getenv(envHelpRun) == "1" {
// Make stable concurrency for generating help documentation.
const prettyConcurrency = 8
return prettyConcurrency
}

return runtime.NumCPU()
}

func printMemStats(ms *runtime.MemStats, logger logutils.Log) {
logger.Infof("Mem stats: alloc=%s total_alloc=%s sys=%s "+
"heap_alloc=%s heap_sys=%s heap_idle=%s heap_released=%s heap_in_use=%s "+
Expand Down
6 changes: 5 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"golang.org/x/mod/modfile"
)

// defaultGoVersion the value should be "oldstable" - 1.
// If the current stable version is 1.24 then 1.23 - 1 = 1.22.
const defaultGoVersion = "1.22"

// Config encapsulates the config data specified in the golangci-lint YAML config file.
type Config struct {
cfgDir string // Path to the directory containing golangci-lint config file.
Expand Down Expand Up @@ -93,7 +97,7 @@ func IsGoGreaterThanOrEqual(current, limit string) bool {
}

func detectGoVersion(ctx context.Context) string {
return cmp.Or(detectGoVersionFromGoMod(ctx), "1.17")
return cmp.Or(detectGoVersionFromGoMod(ctx), defaultGoVersion)
}

// detectGoVersionFromGoMod tries to get Go version from go.mod.
Expand Down
24 changes: 20 additions & 4 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestAutogeneratedNoIssues(t *testing.T) {

testshared.NewRunnerBuilder(t).
WithConfig(cfg).
WithArgs("--show-stats=false").
WithTargetPath(testdataDir, "autogenerated").
WithBinPath(binPath).
Runner().
Expand All @@ -47,6 +48,7 @@ func TestAutogeneratedNoIssues(t *testing.T) {
func TestEmptyDirRun(t *testing.T) {
testshared.NewRunnerBuilder(t).
WithEnviron("GO111MODULE=off").
WithArgs("--show-stats=false").
WithTargetPath(testdataDir, "nogofiles").
Runner().
Install().
Expand All @@ -69,6 +71,7 @@ func TestNotExistingDirRun(t *testing.T) {

func TestSymlinkLoop(t *testing.T) {
testshared.NewRunnerBuilder(t).
WithArgs("--show-stats=false").
WithTargetPath(testdataDir, "symlink_loop", "...").
Runner().
Install().
Expand Down Expand Up @@ -120,7 +123,9 @@ func TestTestsAreLintedByDefault(t *testing.T) {
func TestCgoOk(t *testing.T) {
testshared.NewRunnerBuilder(t).
WithNoConfig().
WithArgs("--timeout=3m",
WithArgs(
"--timeout=3m",
"--show-stats=false",
"--enable-all",
).
WithTargetPath(testdataDir, "cgo").
Expand Down Expand Up @@ -358,7 +363,10 @@ func TestUnsafeOk(t *testing.T) {

testshared.NewRunnerBuilder(t).
WithConfig(cfg).
WithArgs("--enable-all").
WithArgs(
"--show-stats=false",
"--enable-all",
).
WithTargetPath(testdataDir, "unsafe").
WithBinPath(binPath).
Runner().
Expand All @@ -371,7 +379,10 @@ func TestSortedResults(t *testing.T) {

testshared.NewRunnerBuilder(t).
WithNoConfig().
WithArgs("--output.text.print-issued-lines=false").
WithArgs(
"--show-stats=false",
"--output.text.print-issued-lines=false",
).
WithTargetPath(testdataDir, "sort_results").
WithBinPath(binPath).
Runner().
Expand All @@ -385,7 +396,11 @@ func TestSortedResults(t *testing.T) {
func TestIdentifierUsedOnlyInTests(t *testing.T) {
testshared.NewRunnerBuilder(t).
WithNoConfig().
WithArgs("--disable-all", "-Eunused").
WithArgs(
"--show-stats=false",
"--disable-all",
"-Eunused",
).
WithTargetPath(testdataDir, "used_only_in_tests").
Runner().
Install().
Expand All @@ -395,6 +410,7 @@ func TestIdentifierUsedOnlyInTests(t *testing.T) {

func TestUnusedCheckExported(t *testing.T) {
testshared.NewRunnerBuilder(t).
WithArgs("--show-stats=false").
WithConfigFile("testdata_etc/unused_exported/golangci.yml").
WithTargetPath("testdata_etc/unused_exported/...").
Runner().
Expand Down
1 change: 1 addition & 0 deletions test/testshared/integration/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func testOneSource(t *testing.T, log *logutils.StderrLog, binPath, sourcePath st

args := []string{
"--disable-all",
"--show-stats=false",
"--output.json.path=stdout",
"--max-same-issues=100",
"--max-issues-per-linter=100",
Expand Down
Loading