Skip to content

goheader: fix relative template path #3748

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
wants to merge 1 commit into from
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: 4 additions & 3 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,10 @@ type GofumptSettings struct {
}

type GoHeaderSettings struct {
Values map[string]map[string]string `mapstructure:"values"`
Template string `mapstructure:"template"`
TemplatePath string `mapstructure:"template-path"`
Values map[string]map[string]string `mapstructure:"values"`
Template string `mapstructure:"template"`
TemplatePath string `mapstructure:"template-path"`
LintConfigDir string `mapstructure:"lint-config-dir"`
}

type GoImportsSettings struct {
Expand Down
7 changes: 6 additions & 1 deletion 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 @@ -21,10 +22,14 @@ func NewGoHeader(settings *config.GoHeaderSettings) *goanalysis.Linter {

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

Expand Down
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
if stylecheckCfg != nil && stylecheckCfg.GoVersion != "" {
stylecheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
}

if goheaderCfg != nil {
goheaderCfg.LintConfigDir = m.cfg.GetConfigDir()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant you can override goheaderCfg.TemplatePath at this level.
But I cannot decide which option is better

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try to run golangci-lint without config, it works as expected?

}
}

const megacheckName = "megacheck"
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}}
6 changes: 6 additions & 0 deletions test/testdata/configs/go-header-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters-settings:
goheader:
template-path: go-header-template
values:
const:
title: 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: "MY {{title}}"
values:
const:
title: TITLE.
6 changes: 6 additions & 0 deletions test/testdata/go-header-template_good.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*MY TITLE.*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is go-header-template_bad?


//golangcitest:args -Egoheader
//golangcitest:config_path testdata/configs/go-header-template.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question – how it works without test/ prefix?
Looks like your filepath.Join is not covered 🤔

//golangcitest:expected_exitcode 0
package testdata
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?`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra change?


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