From 8e4b0c384a15322587d3cd5ad1b9106679bf4af1 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 2 Jan 2025 19:36:52 +0100 Subject: [PATCH] goheader: skip issues with invalid positions (#5286) --- pkg/golinters/goheader/goheader.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/golinters/goheader/goheader.go b/pkg/golinters/goheader/goheader.go index 843afff800b9..5043381143a3 100644 --- a/pkg/golinters/goheader/goheader.go +++ b/pkg/golinters/goheader/goheader.go @@ -81,12 +81,18 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { // Inspired by /~https://github.com/denis-tingaikin/go-header/blob/4c75a6a2332f025705325d6c71fff4616aedf48f/analyzer.go#L85-L92 if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package { if !strings.HasPrefix(file.Comments[0].List[0].Text, "/*") { - // When the comment are "//" there is a one character offset. + // When the comment is "//" there is a one character offset. offset = 1 } commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line } + // Skip issues related to build directives. + // /~https://github.com/denis-tingaikin/go-header/issues/18 + if issue.Location().Position-offset < 0 { + continue + } + diag := analysis.Diagnostic{ Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position-offset), // The position of the first divergence. Message: issue.Message(),