Skip to content

Commit

Permalink
goheader: fix relative template path
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thejan2009 committed Mar 31, 2023
1 parent ca05239 commit bf97e8d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
9 changes: 7 additions & 2 deletions pkg/golinters/goheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package golinters

import (
"go/token"
"path/filepath"
"sync"

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

const goHeaderName = "goheader"

func NewGoHeader(settings *config.GoHeaderSettings) *goanalysis.Linter {
func NewGoHeader(settings *config.GoHeaderSettings, cfg *config.Config) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

conf := &goheader.Configuration{}
if settings != nil {
path := settings.TemplatePath
if path != "" && !filepath.IsAbs(path) {
path = filepath.Join(cfg.GetConfigDir(), path)
}
conf = &goheader.Configuration{
Values: settings.Values,
Template: settings.Template,
TemplatePath: settings.TemplatePath,
TemplatePath: path,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithAutoFix().
WithURL("https://github.com/mvdan/gofumpt"),

linter.NewConfig(golinters.NewGoHeader(goheaderCfg)).
linter.NewConfig(golinters.NewGoHeader(goheaderCfg, m.cfg)).
WithSince("v1.28.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/denis-tingaikin/go-header"),
Expand Down
1 change: 1 addition & 0 deletions test/testdata/configs/go-header-template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MY {{title}}
2 changes: 1 addition & 1 deletion test/testdata/configs/go-header.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
linters-settings:
goheader:
template: MY {{title}}
template-path: go-header-template
values:
const:
title: TITLE.
2 changes: 1 addition & 1 deletion test/testdata/go-header_bad.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!`
/*MY TITLE?*/ // want `Expected:TITLE\., Actual: TITLE?`

//golangcitest:args -Egoheader
//golangcitest:config_path testdata/configs/go-header.yml
Expand Down

0 comments on commit bf97e8d

Please sign in to comment.