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
		
			
				
	
	
		
			84 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package golinters
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"go/token"
 | 
						|
	"sync"
 | 
						|
 | 
						|
	duplAPI "github.com/golangci/dupl"
 | 
						|
	"github.com/pkg/errors"
 | 
						|
	"golang.org/x/tools/go/analysis"
 | 
						|
 | 
						|
	"github.com/golangci/golangci-lint/pkg/fsutils"
 | 
						|
	"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
 | 
						|
	"github.com/golangci/golangci-lint/pkg/lint/linter"
 | 
						|
	"github.com/golangci/golangci-lint/pkg/result"
 | 
						|
)
 | 
						|
 | 
						|
const duplLinterName = "dupl"
 | 
						|
 | 
						|
func NewDupl() *goanalysis.Linter {
 | 
						|
	var mu sync.Mutex
 | 
						|
	var resIssues []goanalysis.Issue
 | 
						|
 | 
						|
	analyzer := &analysis.Analyzer{
 | 
						|
		Name: duplLinterName,
 | 
						|
		Doc:  goanalysis.TheOnlyanalyzerDoc,
 | 
						|
	}
 | 
						|
	return goanalysis.NewLinter(
 | 
						|
		duplLinterName,
 | 
						|
		"Tool for code clone detection",
 | 
						|
		[]*analysis.Analyzer{analyzer},
 | 
						|
		nil,
 | 
						|
	).WithContextSetter(func(lintCtx *linter.Context) {
 | 
						|
		analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
 | 
						|
			var fileNames []string
 | 
						|
			for _, f := range pass.Files {
 | 
						|
				pos := pass.Fset.PositionFor(f.Pos(), false)
 | 
						|
				fileNames = append(fileNames, pos.Filename)
 | 
						|
			}
 | 
						|
 | 
						|
			issues, err := duplAPI.Run(fileNames, lintCtx.Settings().Dupl.Threshold)
 | 
						|
			if err != nil {
 | 
						|
				return nil, err
 | 
						|
			}
 | 
						|
 | 
						|
			if len(issues) == 0 {
 | 
						|
				return nil, nil
 | 
						|
			}
 | 
						|
 | 
						|
			res := make([]goanalysis.Issue, 0, len(issues))
 | 
						|
			for _, i := range issues {
 | 
						|
				toFilename, err := fsutils.ShortestRelPath(i.To.Filename(), "")
 | 
						|
				if err != nil {
 | 
						|
					return nil, errors.Wrapf(err, "failed to get shortest rel path for %q", i.To.Filename())
 | 
						|
				}
 | 
						|
				dupl := fmt.Sprintf("%s:%d-%d", toFilename, i.To.LineStart(), i.To.LineEnd())
 | 
						|
				text := fmt.Sprintf("%d-%d lines are duplicate of %s",
 | 
						|
					i.From.LineStart(), i.From.LineEnd(),
 | 
						|
					formatCode(dupl, lintCtx.Cfg))
 | 
						|
				res = append(res, goanalysis.NewIssue(&result.Issue{
 | 
						|
					Pos: token.Position{
 | 
						|
						Filename: i.From.Filename(),
 | 
						|
						Line:     i.From.LineStart(),
 | 
						|
					},
 | 
						|
					LineRange: &result.Range{
 | 
						|
						From: i.From.LineStart(),
 | 
						|
						To:   i.From.LineEnd(),
 | 
						|
					},
 | 
						|
					Text:       text,
 | 
						|
					FromLinter: duplLinterName,
 | 
						|
				}, pass))
 | 
						|
			}
 | 
						|
 | 
						|
			mu.Lock()
 | 
						|
			resIssues = append(resIssues, res...)
 | 
						|
			mu.Unlock()
 | 
						|
 | 
						|
			return nil, nil
 | 
						|
		}
 | 
						|
	}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
 | 
						|
		return resIssues
 | 
						|
	}).WithLoadMode(goanalysis.LoadModeSyntax)
 | 
						|
}
 |