Skip to content

Commit bf97e8d

Browse files
committed
goheader: fix relative template path
go-header template-path setting is usually a relative filepath. As a result, the linter only worked when golangci-lint was invoked from that folder. We detect if the template path is relative and prefix it with config path. go-header_bad.go test case was modified because old files are not checked as seen here https://github.com/denis-tingaikin/go-header/blob/v0.4.3/analyzer.go#L59-L63
1 parent ca05239 commit bf97e8d

File tree

5 files changed

+11
-5
lines changed

5 files changed

+11
-5
lines changed

Diff for: pkg/golinters/goheader.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package golinters
22

33
import (
44
"go/token"
5+
"path/filepath"
56
"sync"
67

78
goheader "github.com/denis-tingaikin/go-header"
@@ -15,16 +16,20 @@ import (
1516

1617
const goHeaderName = "goheader"
1718

18-
func NewGoHeader(settings *config.GoHeaderSettings) *goanalysis.Linter {
19+
func NewGoHeader(settings *config.GoHeaderSettings, cfg *config.Config) *goanalysis.Linter {
1920
var mu sync.Mutex
2021
var resIssues []goanalysis.Issue
2122

2223
conf := &goheader.Configuration{}
2324
if settings != nil {
25+
path := settings.TemplatePath
26+
if path != "" && !filepath.IsAbs(path) {
27+
path = filepath.Join(cfg.GetConfigDir(), path)
28+
}
2429
conf = &goheader.Configuration{
2530
Values: settings.Values,
2631
Template: settings.Template,
27-
TemplatePath: settings.TemplatePath,
32+
TemplatePath: path,
2833
}
2934
}
3035

Diff for: pkg/lint/lintersdb/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
511511
WithAutoFix().
512512
WithURL("https://github.com/mvdan/gofumpt"),
513513

514-
linter.NewConfig(golinters.NewGoHeader(goheaderCfg)).
514+
linter.NewConfig(golinters.NewGoHeader(goheaderCfg, m.cfg)).
515515
WithSince("v1.28.0").
516516
WithPresets(linter.PresetStyle).
517517
WithURL("https://github.com/denis-tingaikin/go-header"),

Diff for: test/testdata/configs/go-header-template

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MY {{title}}

Diff for: test/testdata/configs/go-header.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
linters-settings:
22
goheader:
3-
template: MY {{title}}
3+
template-path: go-header-template
44
values:
55
const:
66
title: TITLE.

Diff for: test/testdata/go-header_bad.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!`
1+
/*MY TITLE?*/ // want `Expected:TITLE\., Actual: TITLE?`
22

33
//golangcitest:args -Egoheader
44
//golangcitest:config_path testdata/configs/go-header.yml

0 commit comments

Comments
 (0)