Skip to content

Commit 0799174

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 f714c5d commit 0799174

File tree

8 files changed

+29
-6
lines changed

8 files changed

+29
-6
lines changed

pkg/config/linters_settings.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,10 @@ type GofumptSettings struct {
478478
}
479479

480480
type GoHeaderSettings struct {
481-
Values map[string]map[string]string `mapstructure:"values"`
482-
Template string `mapstructure:"template"`
483-
TemplatePath string `mapstructure:"template-path"`
481+
Values map[string]map[string]string `mapstructure:"values"`
482+
Template string `mapstructure:"template"`
483+
TemplatePath string `mapstructure:"template-path"`
484+
LintConfigDir string `mapstructure:"lint-config-dir"`
484485
}
485486

486487
type GoImportsSettings struct {

pkg/golinters/goheader.go

+6-1
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"
@@ -21,10 +22,14 @@ func NewGoHeader(settings *config.GoHeaderSettings) *goanalysis.Linter {
2122

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

pkg/lint/lintersdb/manager.go

+4
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
247247
if stylecheckCfg != nil && stylecheckCfg.GoVersion != "" {
248248
stylecheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
249249
}
250+
251+
if goheaderCfg != nil {
252+
goheaderCfg.LintConfigDir = m.cfg.GetConfigDir()
253+
}
250254
}
251255

252256
const megacheckName = "megacheck"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MY {{title}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters-settings:
2+
goheader:
3+
template-path: go-header-template
4+
values:
5+
const:
6+
title: TITLE.

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: "MY {{title}}"
44
values:
55
const:
66
title: TITLE.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*MY TITLE.*/
2+
3+
//golangcitest:args -Egoheader
4+
//golangcitest:config_path testdata/configs/go-header-template.yml
5+
//golangcitest:expected_exitcode 0
6+
package testdata

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)