Skip to content

dev: avoid panic in processors #4777

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

Closed
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: 6 additions & 1 deletion pkg/lint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
return nil, fmt.Errorf("failed to get enabled linters: %w", err)
}

pathShortenerProcessor, err := processors.NewPathShortener()
if err != nil {
return nil, err
}

return &Runner{
Processors: []processors.Processor{
processors.NewCgo(goenv),
Expand Down Expand Up @@ -90,7 +95,7 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
processors.NewMaxSameIssues(cfg.Issues.MaxSameIssues, log.Child(logutils.DebugKeyMaxSameIssues), cfg),
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
processors.NewPathShortener(),
pathShortenerProcessor,
processors.NewSeverity(log.Child(logutils.DebugKeySeverityRules), files, &cfg.Severity),

// The fixer still needs to see paths for the issues that are relative to the current directory.
Expand Down
9 changes: 1 addition & 8 deletions pkg/result/processors/path_prettifier.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package processors

import (
"fmt"
"path/filepath"

"github.com/golangci/golangci-lint/pkg/fsutils"
Expand All @@ -11,16 +10,10 @@ import (
var _ Processor = (*PathPrettifier)(nil)

type PathPrettifier struct {
root string
}

func NewPathPrettifier() *PathPrettifier {
root, err := fsutils.Getwd()
if err != nil {
panic(fmt.Sprintf("Can't get working dir: %s", err))
}

return &PathPrettifier{root: root}
return &PathPrettifier{}
}

func (PathPrettifier) Name() string {
Expand Down
6 changes: 3 additions & 3 deletions pkg/result/processors/path_shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ type PathShortener struct {
wd string
}

func NewPathShortener() *PathShortener {
func NewPathShortener() (*PathShortener, error) {
wd, err := fsutils.Getwd()
if err != nil {
panic(fmt.Sprintf("Can't get working dir: %s", err))
return nil, fmt.Errorf("failed to get working dir: %w", err)
}

return &PathShortener{wd: wd}
return &PathShortener{wd: wd}, nil
}

func (PathShortener) Name() string {
Expand Down
Loading