Skip to content

Commit 9dc71dd

Browse files
committed
dev: avoid panic in processors
1 parent e1a8055 commit 9dc71dd

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

pkg/lint/runner.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
6060
return nil, fmt.Errorf("failed to get enabled linters: %w", err)
6161
}
6262

63+
pathShortenerProcessor, err := processors.NewPathShortener()
64+
if err != nil {
65+
return nil, err
66+
}
67+
6368
return &Runner{
6469
Processors: []processors.Processor{
6570
processors.NewCgo(goenv),
@@ -90,7 +95,7 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
9095
processors.NewMaxSameIssues(cfg.Issues.MaxSameIssues, log.Child(logutils.DebugKeyMaxSameIssues), cfg),
9196
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),
9297
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
93-
processors.NewPathShortener(),
98+
pathShortenerProcessor,
9499
processors.NewSeverity(log.Child(logutils.DebugKeySeverityRules), files, &cfg.Severity),
95100

96101
// The fixer still needs to see paths for the issues that are relative to the current directory.

pkg/result/processors/path_prettifier.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package processors
22

33
import (
4-
"fmt"
54
"path/filepath"
65

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

1312
type PathPrettifier struct {
14-
root string
1513
}
1614

1715
func NewPathPrettifier() *PathPrettifier {
18-
root, err := fsutils.Getwd()
19-
if err != nil {
20-
panic(fmt.Sprintf("Can't get working dir: %s", err))
21-
}
22-
23-
return &PathPrettifier{root: root}
16+
return &PathPrettifier{}
2417
}
2518

2619
func (PathPrettifier) Name() string {

pkg/result/processors/path_shortener.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ type PathShortener struct {
1414
wd string
1515
}
1616

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

23-
return &PathShortener{wd: wd}
23+
return &PathShortener{wd: wd}, nil
2424
}
2525

2626
func (PathShortener) Name() string {

0 commit comments

Comments
 (0)