Skip to content

Commit

Permalink
feat: new default for relative-path-mode to cfg (#5454)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Feb 22, 2025
1 parent 79fa7ab commit e651eee
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 118 deletions.
12 changes: 8 additions & 4 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ linters-settings:
# Default: "original"
list-mode: lax
# List of file globs that will match this list of settings to compare against.
# By default, if a path is relative, it is relative to the directory where the golangci-lint command is executed.
# The placeholder '${base-path}' is substituted with a path relative to the mode defined with `run.relative-path-mode`.
# Default: $all
files:
- "!**/*_a _file.go"
Expand Down Expand Up @@ -1364,11 +1366,11 @@ linters-settings:
# Default: ""
failOn: dsl,import
# Comma-separated list of file paths containing ruleguard rules.
# If a path is relative, it is relative to the directory where the golangci-lint command is executed.
# The special '${configDir}' variable is substituted with the absolute directory containing the golangci-lint config file.
# By default, if a path is relative, it is relative to the directory where the golangci-lint command is executed.
# The placeholder '${base-path}' is substituted with a path relative to the mode defined with `run.relative-path-mode`.
# Glob patterns such as 'rules-*.go' may be specified.
# Default: ""
rules: '${configDir}/ruleguard/rules-*.go,${configDir}/myrule1.go'
rules: '${base-path}/ruleguard/rules-*.go,${base-path}/myrule1.go'
# Comma-separated list of enabled groups or skip empty to enable everything.
# Tags can be defined with # character prefix.
# Default: "<all>"
Expand Down Expand Up @@ -1461,6 +1463,8 @@ linters-settings:
# limitations under the License.
# As alternative of directive 'template', you may put the path to file with the template source.
# Useful if you need to load the template from a specific file.
# By default, if a path is relative, it is relative to the directory where the golangci-lint command is executed.
# The placeholder '${base-path}' is substituted with a path relative to the mode defined with `run.relative-path-mode`.
# Default: ""
template-path: /path/to/my/template.tmpl

Expand Down Expand Up @@ -4168,7 +4172,7 @@ run:
# - `gitroot`: the paths will be relative to the git root (the parent directory of `.git`).
# - `cfg`: the paths will be relative to the configuration file.
# - `wd` (NOT recommended): the paths will be relative to the place where golangci-lint is run.
# Default: wd
# Default: cfg
relative-path-mode: gomod

# Exit code when at least one issue was found.
Expand Down
9 changes: 3 additions & 6 deletions pkg/fsutils/basepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package fsutils

import (
"bytes"
"cmp"
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
Expand All @@ -24,10 +24,7 @@ func AllRelativePathModes() []string {
}

func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {
if mode == "" {
// TODO(ldez): v2 the default should be cfg or gomod.
mode = RelativePathModeWd
}
mode = cmp.Or(mode, RelativePathModeCfg)

switch mode {
case RelativePathModeCfg:
Expand Down Expand Up @@ -62,7 +59,7 @@ func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {
return wd, nil

default:
return "", errors.New("unknown relative path mode")
return "", fmt.Errorf("unknown relative path mode: %s", mode)
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/golinters/gocritic/gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ Dynamic rules are written declaratively with AST patterns, filters, report messa
WithContextSetter(func(context *linter.Context) {
wrapper.replacer = strings.NewReplacer(
internal.PlaceholderBasePath, context.Cfg.GetBasePath(),
internal.PlaceholderConfigDir, context.Cfg.GetConfigDir(), //nolint:staticcheck // It must be removed in v2.
)

wrapper.init(context.Log, settings)
Expand Down
72 changes: 0 additions & 72 deletions pkg/golinters/gocritic/testdata/gocritic_configDir.go

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/golinters/gocritic/testdata/gocritic_configDir.yml

This file was deleted.

10 changes: 2 additions & 8 deletions pkg/golinters/internal/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,5 @@ import "github.com/golangci/golangci-lint/pkg/logutils"
// LinterLogger must be use only when the context logger is not available.
var LinterLogger = logutils.NewStderrLog(logutils.DebugKeyLinter)

// Placeholders used inside linters to evaluate relative paths.
const (
PlaceholderBasePath = "${base-path}"
// Deprecated: it must be removed in v2.
// [PlaceholderBasePath] will be the only one placeholder as it is a dynamic value based on
// [github.com/golangci/golangci-lint/pkg/config.Run.RelativePathMode].
PlaceholderConfigDir = "${configDir}"
)
// PlaceholderBasePath used inside linters to evaluate relative paths.
const PlaceholderBasePath = "${base-path}"
10 changes: 1 addition & 9 deletions pkg/lint/lintersdb/builder_plugin_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,7 @@ func (b *PluginGoBuilder) loadConfig(cfg *config.Config, name string, settings *
// or the linter does not implement the AnalyzerPlugin interface.
func (b *PluginGoBuilder) getAnalyzerPlugin(cfg *config.Config, path string, settings any) ([]*analysis.Analyzer, error) {
if !filepath.IsAbs(path) {
// Hack for compatibility:
// the previous default (v1) was `cfg` but `fsutils.GetBasePath` defaults on `wd`.
// TODO(ldez): should be removed in v2.
relativePathMode := cfg.Run.RelativePathMode
if relativePathMode == "" {
relativePathMode = fsutils.RelativePathModeCfg
}

basePath, err := fsutils.GetBasePath(context.Background(), relativePathMode, cfg.GetConfigDir())
basePath, err := fsutils.GetBasePath(context.Background(), cfg.Run.RelativePathMode, cfg.GetConfigDir())
if err != nil {
return nil, fmt.Errorf("get base path: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,12 @@ func TestPathPrefix(t *testing.T) {
}{
{
desc: "empty",
pattern: "^testdata/withtests/",
pattern: "^test/testdata/withtests/",
},
{
desc: "prefixed",
args: []string{"--path-prefix=cool"},
pattern: "^cool/testdata/withtests",
pattern: "^cool/test/testdata/withtests",
},
}

Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/default_exclude.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ linters:
- std-error-handling
- common-false-positives
- legacy

run:
relative-path-mode: wd
3 changes: 3 additions & 0 deletions test/testdata/configs/multiple-issues-fix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ formatters:
settings:
gofumpt:
extra-rules: true

run:
relative-path-mode: wd
3 changes: 3 additions & 0 deletions test/testdata/configs/output.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ linters-settings:
ignore-words:
- langauge
- Dialogue

run:
relative-path-mode: wd
3 changes: 3 additions & 0 deletions test/testdata/configs/path-except.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ linters:
- path-except: _test\.go
linters:
- forbidigo

run:
relative-path-mode: wd

0 comments on commit e651eee

Please sign in to comment.