
If the target files contains `//line` directive and it indicates a non-go file, the linter is going to handle it as a go file, which results in failure. The cause of this issue is that the linters (`Analyzer`s) are using `pass.Fset.Position()`. This func returns the adjusted position using `//line` directive. The example project reported in #998 has `//line` directive that indicates other non-go file. According to the description of "Compiler Directives” (https://golang.org/cmd/compile/#hdr-Compiler_Directives), line directives is mainly used for reporting original positions to the generators or something. On linters of golangci-lint, `pass.Fset.Position()` is used just to aggregate file names; we don't have to adjust positions. This changes `Analyzer`s that use `pass.Fset.Position()` to aggregate file names to use `pass.Fset.PositionFor()` with `adjusted == false`. Relates: #998
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package golinters
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
|
|
"github.com/ryancurrah/gomodguard"
|
|
"golang.org/x/tools/go/analysis"
|
|
|
|
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
|
|
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
|
"github.com/golangci/golangci-lint/pkg/result"
|
|
)
|
|
|
|
const (
|
|
gomodguardName = "gomodguard"
|
|
)
|
|
|
|
// NewGomodguard returns a new Gomodguard linter.
|
|
func NewGomodguard() *goanalysis.Linter {
|
|
var (
|
|
issues []goanalysis.Issue
|
|
mu = sync.Mutex{}
|
|
analyzer = &analysis.Analyzer{
|
|
Name: goanalysis.TheOnlyAnalyzerName,
|
|
Doc: goanalysis.TheOnlyanalyzerDoc,
|
|
}
|
|
)
|
|
|
|
return goanalysis.NewLinter(
|
|
gomodguardName,
|
|
"Allow and block list linter for direct Go module dependencies.",
|
|
[]*analysis.Analyzer{analyzer},
|
|
nil,
|
|
).WithContextSetter(func(lintCtx *linter.Context) {
|
|
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
|
|
var (
|
|
files = []string{}
|
|
linterCfg = lintCtx.Cfg.LintersSettings.Gomodguard
|
|
processorCfg = gomodguard.Configuration{}
|
|
)
|
|
processorCfg.Allowed.Modules = linterCfg.Allowed.Modules
|
|
processorCfg.Allowed.Domains = linterCfg.Allowed.Domains
|
|
for n := range linterCfg.Blocked.Modules {
|
|
for k, v := range linterCfg.Blocked.Modules[n] {
|
|
m := gomodguard.BlockedModule{k: gomodguard.Recommendations{
|
|
Recommendations: v.Recommendations,
|
|
Reason: v.Reason,
|
|
}}
|
|
processorCfg.Blocked.Modules = append(processorCfg.Blocked.Modules, m)
|
|
break
|
|
}
|
|
}
|
|
|
|
for _, file := range pass.Files {
|
|
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
|
|
}
|
|
|
|
processor, err := gomodguard.NewProcessor(processorCfg, log.New(os.Stderr, "", 0))
|
|
if err != nil {
|
|
lintCtx.Log.Warnf("running gomodguard failed: %s: if you are not using go modules "+
|
|
"it is suggested to disable this linter", err)
|
|
return nil, nil
|
|
}
|
|
|
|
gomodguardErrors := processor.ProcessFiles(files)
|
|
if len(gomodguardErrors) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
for _, err := range gomodguardErrors {
|
|
issues = append(issues, goanalysis.NewIssue(&result.Issue{
|
|
FromLinter: gomodguardName,
|
|
Pos: err.Position,
|
|
Text: err.Reason,
|
|
}, pass))
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
|
|
return issues
|
|
}).WithLoadMode(goanalysis.LoadModeSyntax)
|
|
}
|