Skip to content

Commit f639807

Browse files
authored
feat: add an option to display absolute paths (#5651)
1 parent e589139 commit f639807

File tree

9 files changed

+92
-16
lines changed

9 files changed

+92
-16
lines changed

.golangci.next.reference.yml

+7
Original file line numberDiff line numberDiff line change
@@ -4152,9 +4152,16 @@ output:
41524152
path: ./path/to/output.json
41534153

41544154
# Add a prefix to the output file references.
4155+
# This option is ignored when using `output.path-mode: abs` mode.
41554156
# Default: ""
41564157
path-prefix: ""
41574158

4159+
# By default, the report are related to the path obtained by `run.relative-path-mode`.
4160+
# The mode `abs` allows to show absolute file paths instead of relative file paths.
4161+
# The option `output.path-prefix` is ignored when using `abs` mode.
4162+
# Default: ""
4163+
path-mode: "abs"
4164+
41584165
# Order to use when sorting results.
41594166
# Possible values: `file`, `linter`, and `severity`.
41604167
#

jsonschema/golangci.next.jsonschema.json

+5
Original file line numberDiff line numberDiff line change
@@ -4222,6 +4222,11 @@
42224222
}
42234223
}
42244224
},
4225+
"path-mode": {
4226+
"type": "string",
4227+
"default": "",
4228+
"examples": ["abs"]
4229+
},
42254230
"path-prefix": {
42264231
"description": "Add a prefix to the output file references.",
42274232
"type": "string",

pkg/commands/flagsets.go

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
6262
func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
6363
internal.AddFlagAndBind(v, fs, fs.String, "path-prefix", "output.path-prefix", "",
6464
color.GreenString("Path prefix to add to output"))
65+
internal.AddFlagAndBind(v, fs, fs.String, "path-mode", "output.path-mode", "",
66+
color.GreenString("Path mode to use (empty, or 'abs')"))
6567
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", true, color.GreenString("Show statistics per linter"))
6668

6769
setupOutputFormatsFlagSet(v, fs)

pkg/config/output.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,34 @@ import (
44
"fmt"
55
"slices"
66
"strings"
7+
8+
"github.com/golangci/golangci-lint/v2/pkg/fsutils"
79
)
810

911
type Output struct {
1012
Formats Formats `mapstructure:"formats"`
1113
SortOrder []string `mapstructure:"sort-order"`
12-
PathPrefix string `mapstructure:"path-prefix"`
1314
ShowStats bool `mapstructure:"show-stats"`
15+
PathPrefix string `mapstructure:"path-prefix"`
16+
PathMode string `mapstructure:"path-mode"`
1417
}
1518

1619
func (o *Output) Validate() error {
20+
validators := []func() error{
21+
o.validateSortOrder,
22+
o.validatePathMode,
23+
}
24+
25+
for _, v := range validators {
26+
if err := v(); err != nil {
27+
return err
28+
}
29+
}
30+
31+
return nil
32+
}
33+
34+
func (o *Output) validateSortOrder() error {
1735
validOrders := []string{"linter", "file", "severity"}
1836

1937
all := strings.Join(o.SortOrder, " ")
@@ -30,3 +48,15 @@ func (o *Output) Validate() error {
3048

3149
return nil
3250
}
51+
52+
func (o *Output) validatePathMode() error {
53+
switch o.PathMode {
54+
case "", fsutils.OutputPathModeAbsolute:
55+
// Valid
56+
57+
default:
58+
return fmt.Errorf("unsupported output path mode %q", o.PathMode)
59+
}
60+
61+
return nil
62+
}

pkg/config/output_test.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7+
8+
"github.com/golangci/golangci-lint/v2/pkg/fsutils"
79
)
810

911
func TestOutput_Validate(t *testing.T) {
@@ -12,29 +14,41 @@ func TestOutput_Validate(t *testing.T) {
1214
settings *Output
1315
}{
1416
{
15-
desc: "file",
17+
desc: "SortOrder: file",
1618
settings: &Output{
1719
SortOrder: []string{"file"},
1820
},
1921
},
2022
{
21-
desc: "linter",
23+
desc: "SortOrder: linter",
2224
settings: &Output{
2325
SortOrder: []string{"linter"},
2426
},
2527
},
2628
{
27-
desc: "severity",
29+
desc: "SortOrder: severity",
2830
settings: &Output{
2931
SortOrder: []string{"severity"},
3032
},
3133
},
3234
{
33-
desc: "multiple",
35+
desc: "SortOrder: multiple",
3436
settings: &Output{
3537
SortOrder: []string{"file", "linter", "severity"},
3638
},
3739
},
40+
{
41+
desc: "PathMode: empty",
42+
settings: &Output{
43+
PathMode: "",
44+
},
45+
},
46+
{
47+
desc: "PathMode: absolute",
48+
settings: &Output{
49+
PathMode: fsutils.OutputPathModeAbsolute,
50+
},
51+
},
3852
}
3953

4054
for _, test := range testCases {
@@ -54,19 +68,26 @@ func TestOutput_Validate_error(t *testing.T) {
5468
expected string
5569
}{
5670
{
57-
desc: "invalid sort-order",
71+
desc: "SortOrder: invalid",
5872
settings: &Output{
5973
SortOrder: []string{"a"},
6074
},
6175
expected: `unsupported sort-order name "a"`,
6276
},
6377
{
64-
desc: "duplicate",
78+
desc: "SortOrder: duplicate",
6579
settings: &Output{
6680
SortOrder: []string{"file", "linter", "severity", "linter"},
6781
},
6882
expected: `the sort-order name "linter" is repeated several times`,
6983
},
84+
{
85+
desc: "PathMode: invalid",
86+
settings: &Output{
87+
PathMode: "example",
88+
},
89+
expected: `unsupported output path mode "example"`,
90+
},
7091
}
7192

7293
for _, test := range testCases {

pkg/fsutils/basepath.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const (
1919
RelativePathModeWd = "wd"
2020
)
2121

22+
// OutputPathModeAbsolute path mode used to show absolute paths in output reports (user-facing).
23+
const OutputPathModeAbsolute = "abs"
24+
2225
func AllRelativePathModes() []string {
2326
return []string{RelativePathModeGoMod, RelativePathModeGitRoot, RelativePathModeCfg, RelativePathModeWd}
2427
}

pkg/lint/runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env,
110110
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
111111
processors.NewPathShortener(),
112112
processors.NewSeverity(log.Child(logutils.DebugKeySeverityRules), lineCache, &cfg.Severity),
113-
processors.NewPathPrettifier(log, cfg.Output.PathPrefix),
113+
processors.NewPathPrettifier(log, &cfg.Output),
114114
processors.NewSortResults(&cfg.Output),
115115
},
116116
lintCtx: lintCtx,

pkg/result/processors/path_prettifier.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package processors
33
import (
44
"path/filepath"
55

6+
"github.com/golangci/golangci-lint/v2/pkg/config"
7+
"github.com/golangci/golangci-lint/v2/pkg/fsutils"
68
"github.com/golangci/golangci-lint/v2/pkg/logutils"
79
"github.com/golangci/golangci-lint/v2/pkg/result"
810
)
@@ -12,14 +14,15 @@ var _ Processor = (*PathPrettifier)(nil)
1214
// PathPrettifier modifies report file path to be relative to the base path.
1315
// Also handles the `output.path-prefix` option.
1416
type PathPrettifier struct {
15-
prefix string
16-
log logutils.Log
17+
cfg *config.Output
18+
19+
log logutils.Log
1720
}
1821

19-
func NewPathPrettifier(log logutils.Log, prefix string) *PathPrettifier {
22+
func NewPathPrettifier(log logutils.Log, cfg *config.Output) *PathPrettifier {
2023
return &PathPrettifier{
21-
prefix: prefix,
22-
log: log.Child(logutils.DebugKeyPathPrettifier),
24+
cfg: cfg,
25+
log: log.Child(logutils.DebugKeyPathPrettifier),
2326
}
2427
}
2528

@@ -28,13 +31,17 @@ func (*PathPrettifier) Name() string {
2831
}
2932

3033
func (p *PathPrettifier) Process(issues []result.Issue) ([]result.Issue, error) {
34+
if p.cfg.PathMode == fsutils.OutputPathModeAbsolute {
35+
return issues, nil
36+
}
37+
3138
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
3239
newIssue := issue
3340

34-
if p.prefix == "" {
41+
if p.cfg.PathPrefix == "" {
3542
newIssue.Pos.Filename = issue.RelativePath
3643
} else {
37-
newIssue.Pos.Filename = filepath.Join(p.prefix, issue.RelativePath)
44+
newIssue.Pos.Filename = filepath.Join(p.cfg.PathPrefix, issue.RelativePath)
3845
}
3946

4047
return newIssue

pkg/result/processors/path_prettifier_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
1010

11+
"github.com/golangci/golangci-lint/v2/pkg/config"
1112
"github.com/golangci/golangci-lint/v2/pkg/logutils"
1213
"github.com/golangci/golangci-lint/v2/pkg/result"
1314
)
@@ -59,7 +60,7 @@ func TestPathPrettifier_Process(t *testing.T) {
5960
},
6061
} {
6162
t.Run(tt.name, func(t *testing.T) {
62-
p := NewPathPrettifier(logutils.NewStderrLog(logutils.DebugKeyEmpty), tt.prefix)
63+
p := NewPathPrettifier(logutils.NewStderrLog(logutils.DebugKeyEmpty), &config.Output{PathPrefix: tt.prefix})
6364

6465
got, err := p.Process(tt.issues)
6566
require.NoError(t, err)

0 commit comments

Comments
 (0)