Skip to content
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

feat: add an option to display absolute paths #5651

Merged
merged 4 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4149,9 +4149,16 @@ output:
path: ./path/to/output.json

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

# By default, the report are related to the path obtained by `run.relative-path-mode`.
# The mode `abs` allows to show absolute file paths instead of relative file paths.
# The option `output.path-prefix` is ignored when using `abs` mode.
# Default: ""
path-mode: "abs"

# Order to use when sorting results.
# Possible values: `file`, `linter`, and `severity`.
#
Expand Down
5 changes: 5 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4222,6 +4222,11 @@
}
}
},
"path-mode": {
"type": "string",
"default": "",
"examples": ["abs"]
},
"path-prefix": {
"description": "Add a prefix to the output file references.",
"type": "string",
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/flagsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ 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.String, "path-mode", "output.path-mode", "",
color.GreenString("Path mode to use (empty, or 'abs')"))
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", true, color.GreenString("Show statistics per linter"))

setupOutputFormatsFlagSet(v, fs)
Expand Down
32 changes: 31 additions & 1 deletion pkg/config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@ import (
"fmt"
"slices"
"strings"

"github.com/golangci/golangci-lint/v2/pkg/fsutils"
)

type Output struct {
Formats Formats `mapstructure:"formats"`
SortOrder []string `mapstructure:"sort-order"`
PathPrefix string `mapstructure:"path-prefix"`
ShowStats bool `mapstructure:"show-stats"`
PathPrefix string `mapstructure:"path-prefix"`
PathMode string `mapstructure:"path-mode"`
}

func (o *Output) Validate() error {
validators := []func() error{
o.validateSortOrder,
o.validatePathMode,
}

for _, v := range validators {
if err := v(); err != nil {
return err
}
}

return nil
}

func (o *Output) validateSortOrder() error {
validOrders := []string{"linter", "file", "severity"}

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

return nil
}

func (o *Output) validatePathMode() error {
switch o.PathMode {
case "", fsutils.OutputPathModeAbsolute:
// Valid

default:
return fmt.Errorf("unsupported output path mode %q", o.PathMode)
}

return nil
}
33 changes: 27 additions & 6 deletions pkg/config/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/v2/pkg/fsutils"
)

func TestOutput_Validate(t *testing.T) {
Expand All @@ -12,29 +14,41 @@ func TestOutput_Validate(t *testing.T) {
settings *Output
}{
{
desc: "file",
desc: "SortOrder: file",
settings: &Output{
SortOrder: []string{"file"},
},
},
{
desc: "linter",
desc: "SortOrder: linter",
settings: &Output{
SortOrder: []string{"linter"},
},
},
{
desc: "severity",
desc: "SortOrder: severity",
settings: &Output{
SortOrder: []string{"severity"},
},
},
{
desc: "multiple",
desc: "SortOrder: multiple",
settings: &Output{
SortOrder: []string{"file", "linter", "severity"},
},
},
{
desc: "PathMode: empty",
settings: &Output{
PathMode: "",
},
},
{
desc: "PathMode: absolute",
settings: &Output{
PathMode: fsutils.OutputPathModeAbsolute,
},
},
}

for _, test := range testCases {
Expand All @@ -54,19 +68,26 @@ func TestOutput_Validate_error(t *testing.T) {
expected string
}{
{
desc: "invalid sort-order",
desc: "SortOrder: invalid",
settings: &Output{
SortOrder: []string{"a"},
},
expected: `unsupported sort-order name "a"`,
},
{
desc: "duplicate",
desc: "SortOrder: duplicate",
settings: &Output{
SortOrder: []string{"file", "linter", "severity", "linter"},
},
expected: `the sort-order name "linter" is repeated several times`,
},
{
desc: "PathMode: invalid",
settings: &Output{
PathMode: "example",
},
expected: `unsupported output path mode "example"`,
},
}

for _, test := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions pkg/fsutils/basepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const (
RelativePathModeWd = "wd"
)

// OutputPathModeAbsolute path mode used to show absolute paths in output reports (user-facing).
const OutputPathModeAbsolute = "abs"

func AllRelativePathModes() []string {
return []string{RelativePathModeGoMod, RelativePathModeGitRoot, RelativePathModeCfg, RelativePathModeWd}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/lint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env,
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
processors.NewPathShortener(),
processors.NewSeverity(log.Child(logutils.DebugKeySeverityRules), lineCache, &cfg.Severity),
processors.NewPathPrettifier(log, cfg.Output.PathPrefix),
processors.NewPathPrettifier(log, &cfg.Output),
processors.NewSortResults(&cfg.Output),
},
lintCtx: lintCtx,
Expand Down
21 changes: 14 additions & 7 deletions pkg/result/processors/path_prettifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package processors
import (
"path/filepath"

"github.com/golangci/golangci-lint/v2/pkg/config"
"github.com/golangci/golangci-lint/v2/pkg/fsutils"
"github.com/golangci/golangci-lint/v2/pkg/logutils"
"github.com/golangci/golangci-lint/v2/pkg/result"
)
Expand All @@ -12,14 +14,15 @@ var _ Processor = (*PathPrettifier)(nil)
// PathPrettifier modifies report file path to be relative to the base path.
// Also handles the `output.path-prefix` option.
type PathPrettifier struct {
prefix string
log logutils.Log
cfg *config.Output

log logutils.Log
}

func NewPathPrettifier(log logutils.Log, prefix string) *PathPrettifier {
func NewPathPrettifier(log logutils.Log, cfg *config.Output) *PathPrettifier {
return &PathPrettifier{
prefix: prefix,
log: log.Child(logutils.DebugKeyPathPrettifier),
cfg: cfg,
log: log.Child(logutils.DebugKeyPathPrettifier),
}
}

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

func (p *PathPrettifier) Process(issues []result.Issue) ([]result.Issue, error) {
if p.cfg.PathMode == fsutils.OutputPathModeAbsolute {
return issues, nil
}

return transformIssues(issues, func(issue *result.Issue) *result.Issue {
newIssue := issue

if p.prefix == "" {
if p.cfg.PathPrefix == "" {
newIssue.Pos.Filename = issue.RelativePath
} else {
newIssue.Pos.Filename = filepath.Join(p.prefix, issue.RelativePath)
newIssue.Pos.Filename = filepath.Join(p.cfg.PathPrefix, issue.RelativePath)
}

return newIssue
Expand Down
3 changes: 2 additions & 1 deletion pkg/result/processors/path_prettifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/v2/pkg/config"
"github.com/golangci/golangci-lint/v2/pkg/logutils"
"github.com/golangci/golangci-lint/v2/pkg/result"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestPathPrettifier_Process(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
p := NewPathPrettifier(logutils.NewStderrLog(logutils.DebugKeyEmpty), tt.prefix)
p := NewPathPrettifier(logutils.NewStderrLog(logutils.DebugKeyEmpty), &config.Output{PathPrefix: tt.prefix})

got, err := p.Process(tt.issues)
require.NoError(t, err)
Expand Down
Loading