Skip to content

Commit 1a931cb

Browse files
fmeumtyler-french
andauthored
Fix regression in empty .go file filtering for nogo (#3832)
* Fix regression in empty `.go` file filtering for nogo 6236dd8 regressed the nogo exclusion logic for the empty `.go` file generated for targets that otherwise don't contain any `.go` files. The test had been silently disabled a while ago when the name pattern for the empty file was changed. This is fixed by reintroducing the exclusion logic and making the test more strict. * Update go/tools/builders/compilepkg.go Co-authored-by: Tyler French <[email protected]> --------- Co-authored-by: Tyler French <[email protected]>
1 parent 03bbd51 commit 1a931cb

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

go/tools/builders/compilepkg.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ func compileArchive(
203203
}
204204
defer cleanup()
205205

206+
emptyGoFilePath := ""
206207
if len(srcs.goSrcs) == 0 {
207208
// We need to run the compiler to create a valid archive, even if there's nothing in it.
208209
// Otherwise, GoPack will complain if we try to add assembly or cgo objects.
@@ -227,7 +228,7 @@ func compileArchive(
227228
matched: true,
228229
pkg: "empty",
229230
})
230-
231+
emptyGoFilePath = emptyGoFile.Name()
231232
}
232233
packageName := srcs.goSrcs[0].pkg
233234
var goSrcs, cgoSrcs []string
@@ -270,18 +271,24 @@ func compileArchive(
270271
// constraints.
271272
compilingWithCgo := haveCgo && cgoEnabled
272273

274+
filterForNogo := func(slice []string) []string {
275+
filtered := make([]string, 0, len(slice))
276+
for _, s := range slice {
277+
// Do not subject the generated empty .go file to nogo checks.
278+
if s != emptyGoFilePath {
279+
filtered = append(filtered, s)
280+
}
281+
}
282+
return filtered
283+
}
273284
// When coverage is set, source files will be modified during instrumentation. We should only run static analysis
274285
// over original source files and not the modified ones.
275286
// goSrcsNogo and cgoSrcsNogo are copies of the original source files for nogo to run static analysis.
276-
goSrcsNogo := goSrcs
277-
cgoSrcsNogo := cgoSrcs
287+
goSrcsNogo := filterForNogo(goSrcs)
288+
cgoSrcsNogo := append([]string{}, cgoSrcs...)
278289

279290
// Instrument source files for coverage.
280291
if coverMode != "" {
281-
// deep copy original source files for nogo static analysis, avoid being modified by coverage.
282-
goSrcsNogo = append([]string{}, goSrcs...)
283-
cgoSrcsNogo = append([]string{}, cgoSrcs...)
284-
285292
relCoverPath := make(map[string]string)
286293
for _, s := range coverSrcs {
287294
relCoverPath[abs(s)] = s

tests/core/nogo/generate/empty_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ package noempty
6363
6464
import (
6565
"fmt"
66-
"path/filepath"
67-
"strings"
6866
6967
"golang.org/x/tools/go/analysis"
7068
)
@@ -77,12 +75,11 @@ var Analyzer = &analysis.Analyzer{
7775
7876
func run(pass *analysis.Pass) (interface{}, error) {
7977
for _, f := range pass.Files {
80-
pos := pass.Fset.PositionFor(f.Pos(), false)
81-
82-
if strings.HasSuffix(pos.Filename, filepath.Join(".", "_empty.go")) {
78+
// Fail on any package that isn't the "simple_test"'s package ('simple') or 'main'.
79+
if f.Name.Name != "simple" && f.Name.Name != "main" {
8380
pass.Report(analysis.Diagnostic{
8481
Pos: 0,
85-
Message: fmt.Sprintf("Detected generated source code from rules_go: %s", pos.Filename),
82+
Message: fmt.Sprintf("Detected generated source code from rules_go: package %s", f.Name.Name),
8683
})
8784
}
8885
}

0 commit comments

Comments
 (0)