Skip to content

Commit e651eee

Browse files
authored
feat: new default for relative-path-mode to cfg (#5454)
1 parent 79fa7ab commit e651eee

File tree

12 files changed

+28
-118
lines changed

12 files changed

+28
-118
lines changed

.golangci.next.reference.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ linters-settings:
535535
# Default: "original"
536536
list-mode: lax
537537
# List of file globs that will match this list of settings to compare against.
538+
# By default, if a path is relative, it is relative to the directory where the golangci-lint command is executed.
539+
# The placeholder '${base-path}' is substituted with a path relative to the mode defined with `run.relative-path-mode`.
538540
# Default: $all
539541
files:
540542
- "!**/*_a _file.go"
@@ -1364,11 +1366,11 @@ linters-settings:
13641366
# Default: ""
13651367
failOn: dsl,import
13661368
# Comma-separated list of file paths containing ruleguard rules.
1367-
# If a path is relative, it is relative to the directory where the golangci-lint command is executed.
1368-
# The special '${configDir}' variable is substituted with the absolute directory containing the golangci-lint config file.
1369+
# By default, if a path is relative, it is relative to the directory where the golangci-lint command is executed.
1370+
# The placeholder '${base-path}' is substituted with a path relative to the mode defined with `run.relative-path-mode`.
13691371
# Glob patterns such as 'rules-*.go' may be specified.
13701372
# Default: ""
1371-
rules: '${configDir}/ruleguard/rules-*.go,${configDir}/myrule1.go'
1373+
rules: '${base-path}/ruleguard/rules-*.go,${base-path}/myrule1.go'
13721374
# Comma-separated list of enabled groups or skip empty to enable everything.
13731375
# Tags can be defined with # character prefix.
13741376
# Default: "<all>"
@@ -1461,6 +1463,8 @@ linters-settings:
14611463
# limitations under the License.
14621464
# As alternative of directive 'template', you may put the path to file with the template source.
14631465
# Useful if you need to load the template from a specific file.
1466+
# By default, if a path is relative, it is relative to the directory where the golangci-lint command is executed.
1467+
# The placeholder '${base-path}' is substituted with a path relative to the mode defined with `run.relative-path-mode`.
14641468
# Default: ""
14651469
template-path: /path/to/my/template.tmpl
14661470

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

41744178
# Exit code when at least one issue was found.

pkg/fsutils/basepath.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package fsutils
22

33
import (
44
"bytes"
5+
"cmp"
56
"context"
6-
"errors"
77
"fmt"
88
"os/exec"
99
"path/filepath"
@@ -24,10 +24,7 @@ func AllRelativePathModes() []string {
2424
}
2525

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

3229
switch mode {
3330
case RelativePathModeCfg:
@@ -62,7 +59,7 @@ func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {
6259
return wd, nil
6360

6461
default:
65-
return "", errors.New("unknown relative path mode")
62+
return "", fmt.Errorf("unknown relative path mode: %s", mode)
6663
}
6764
}
6865

pkg/golinters/gocritic/gocritic.go

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Dynamic rules are written declaratively with AST patterns, filters, report messa
6060
WithContextSetter(func(context *linter.Context) {
6161
wrapper.replacer = strings.NewReplacer(
6262
internal.PlaceholderBasePath, context.Cfg.GetBasePath(),
63-
internal.PlaceholderConfigDir, context.Cfg.GetConfigDir(), //nolint:staticcheck // It must be removed in v2.
6463
)
6564

6665
wrapper.init(context.Log, settings)

pkg/golinters/gocritic/testdata/gocritic_configDir.go

-72
This file was deleted.

pkg/golinters/gocritic/testdata/gocritic_configDir.yml

-16
This file was deleted.

pkg/golinters/internal/commons.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,5 @@ import "github.com/golangci/golangci-lint/pkg/logutils"
55
// LinterLogger must be use only when the context logger is not available.
66
var LinterLogger = logutils.NewStderrLog(logutils.DebugKeyLinter)
77

8-
// Placeholders used inside linters to evaluate relative paths.
9-
const (
10-
PlaceholderBasePath = "${base-path}"
11-
// Deprecated: it must be removed in v2.
12-
// [PlaceholderBasePath] will be the only one placeholder as it is a dynamic value based on
13-
// [github.com/golangci/golangci-lint/pkg/config.Run.RelativePathMode].
14-
PlaceholderConfigDir = "${configDir}"
15-
)
8+
// PlaceholderBasePath used inside linters to evaluate relative paths.
9+
const PlaceholderBasePath = "${base-path}"

pkg/lint/lintersdb/builder_plugin_go.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,7 @@ func (b *PluginGoBuilder) loadConfig(cfg *config.Config, name string, settings *
8383
// or the linter does not implement the AnalyzerPlugin interface.
8484
func (b *PluginGoBuilder) getAnalyzerPlugin(cfg *config.Config, path string, settings any) ([]*analysis.Analyzer, error) {
8585
if !filepath.IsAbs(path) {
86-
// Hack for compatibility:
87-
// the previous default (v1) was `cfg` but `fsutils.GetBasePath` defaults on `wd`.
88-
// TODO(ldez): should be removed in v2.
89-
relativePathMode := cfg.Run.RelativePathMode
90-
if relativePathMode == "" {
91-
relativePathMode = fsutils.RelativePathModeCfg
92-
}
93-
94-
basePath, err := fsutils.GetBasePath(context.Background(), relativePathMode, cfg.GetConfigDir())
86+
basePath, err := fsutils.GetBasePath(context.Background(), cfg.Run.RelativePathMode, cfg.GetConfigDir())
9587
if err != nil {
9688
return nil, fmt.Errorf("get base path: %w", err)
9789
}

test/run_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -548,12 +548,12 @@ func TestPathPrefix(t *testing.T) {
548548
}{
549549
{
550550
desc: "empty",
551-
pattern: "^testdata/withtests/",
551+
pattern: "^test/testdata/withtests/",
552552
},
553553
{
554554
desc: "prefixed",
555555
args: []string{"--path-prefix=cool"},
556-
pattern: "^cool/testdata/withtests",
556+
pattern: "^cool/test/testdata/withtests",
557557
},
558558
}
559559

test/testdata/configs/default_exclude.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ linters:
66
- std-error-handling
77
- common-false-positives
88
- legacy
9+
10+
run:
11+
relative-path-mode: wd

test/testdata/configs/multiple-issues-fix.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ formatters:
66
settings:
77
gofumpt:
88
extra-rules: true
9+
10+
run:
11+
relative-path-mode: wd

test/testdata/configs/output.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ linters-settings:
66
ignore-words:
77
- langauge
88
- Dialogue
9+
10+
run:
11+
relative-path-mode: wd

test/testdata/configs/path-except.yml

+3
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ linters:
1414
- path-except: _test\.go
1515
linters:
1616
- forbidigo
17+
18+
run:
19+
relative-path-mode: wd

0 commit comments

Comments
 (0)