Skip to content

Commit 70ad27d

Browse files
authored
fix: instrumentForCoverage for empty outfiles (#4414)
**What type of PR is this?** Bug fix **What does this PR do? Why is it needed?** **Which issues(s) does this PR fix?** Fixes #4412 **Other notes for review**
1 parent 2af3f27 commit 70ad27d

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

.bazelci/presubmit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ tasks:
156156
- "-//tests/core/cgo:generated_versioned_dylib_test"
157157
- "-//tests/core/coverage:coverage_test"
158158
- "-//tests/core/coverage:issue3017_test"
159+
- "-//tests/core/coverage:issue4414_test"
159160
- "-//tests/core/coverage:reassign_flag_commandline_test"
160161
- "-//tests/core/go_binary:go_default_test"
161162
- "-//tests/core/go_path:go_path_test"

go/tools/builders/compilepkg.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ func compileArchive(
258258
}
259259

260260
var (
261-
coverIn []string
262-
coverOut []string
261+
coverIn []string
262+
coverOut []string
263263
srcPathMapping = make(map[string]string)
264264
)
265265
for i, origSrc := range combined {
@@ -312,12 +312,14 @@ func compileArchive(
312312
// https://github.com/golang/go/blob/go1.24.5/src/cmd/go/internal/work/exec.go#L1932
313313
sum := sha256.Sum256([]byte(importPath))
314314
coverVar := fmt.Sprintf("goCover_%x_", sum[:6])
315-
coverageCfg = workDir + "pkgcfg.txt"
316-
coverOut, err := instrumentForCoverage(goenv, importPath, packageName, coverIn, coverVar, coverMode, coverOut, workDir, relCoverPath, srcPathMapping)
317-
if err != nil {
318-
return err
315+
if len(coverOut) > 0 {
316+
coverageCfg = workDir + "pkgcfg.txt"
317+
coverOut, err := instrumentForCoverage(goenv, importPath, packageName, coverIn, coverVar, coverMode, coverOut, workDir, relCoverPath, srcPathMapping)
318+
if err != nil {
319+
return err
320+
}
321+
goSrcs = append(goSrcs, coverOut[0])
319322
}
320-
goSrcs = append(goSrcs, coverOut[0])
321323
}
322324

323325
// If we have cgo, generate separate C and go files, and compile the

tests/core/coverage/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ go_bazel_test(
3333
srcs = ["issue3017_test.go"],
3434
)
3535

36+
go_bazel_test(
37+
name = "issue4414_test",
38+
srcs = ["issue4414_test.go"],
39+
)
40+
3641
go_bazel_test(
3742
name = "reassign_flag_commandline_test",
3843
srcs = ["reassign_flag_commandline_test.go"],
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2019 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package coverage_test
16+
17+
import (
18+
"testing"
19+
20+
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
21+
)
22+
23+
func TestMain(m *testing.M) {
24+
bazel_testing.TestMain(m, bazel_testing.Args{
25+
Main: `
26+
-- issue.go --
27+
package internal
28+
29+
func OK() {
30+
// This is a no-op function
31+
}
32+
33+
-- issue_test.go --
34+
package internal
35+
36+
import (
37+
"testing"
38+
)
39+
40+
func TestOK(t *testing.T) {
41+
OK()
42+
}
43+
44+
-- BUILD.bazel --
45+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
46+
go_library(
47+
name = "issue",
48+
srcs = ["issue.go"],
49+
importpath = "github.com/bakjos/test/internal",
50+
)
51+
52+
go_test(
53+
name = "issue_test",
54+
srcs = ["issue_test.go"],
55+
embed = [":issue"],
56+
)
57+
`,
58+
})
59+
}
60+
61+
func TestIssue4414(t *testing.T) {
62+
if err := bazel_testing.RunBazel("coverage", "--instrument_test_targets", "//:issue_test"); err != nil {
63+
t.Fatal(err)
64+
}
65+
}

0 commit comments

Comments
 (0)