Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 1.32.1
--------------
* Pass absolute source paths to `go tool cover` so that coverage builds
report correct file positions under Go 1.27 (#372)

Version 1.32.0
--------------
* Allow passing command-line arguments to go_test targets (#368)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.32.0
1.32.1
15 changes: 14 additions & 1 deletion tools/please_go/cover/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ subinclude("//build_defs:go")

filegroup(
name = "srcs",
srcs = ["cover.go"],
srcs = glob(
include = ["*.go"],
exclude = ["*_test.go"],
),
visibility = ["//tools/please_go:bootstrap"],
)

Expand All @@ -12,3 +15,13 @@ go_library(
visibility = ["//tools/please_go/..."],
deps = ["//tools/please_go/install/toolchain"],
)

go_test(
name = "cover_test",
srcs = ["cover_test.go"],
deps = [
":cover",
"///third_party/go/github.com_stretchr_testify//assert",
"///third_party/go/github.com_stretchr_testify//require",
],
)
11 changes: 10 additions & 1 deletion tools/please_go/cover/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ func WriteCoverage(goTool, coverTool, covercfg, output, pkg string, srcs []strin
if err != nil {
return err
}
// `go tool cover` copies these into `//line` directives, and since Go 1.27
// (go.dev/issue/70478) a relative one resolves against the file's own directory.
for i, src := range srcs {
abs, err := filepath.Abs(src)
if err != nil {
return err
}
srcs[i] = abs
}
const pkgConfigFile = "pkgcfg"
b, _ := json.Marshal(coverConfig{
OutConfig: covercfg,
Expand All @@ -34,7 +43,7 @@ func WriteCoverage(goTool, coverTool, covercfg, output, pkg string, srcs []strin
var buf bytes.Buffer
// 1.21 requires a cover vars file to be written into the output file list
if coverTool != "" || needs121CoverVars(goTool) {
buf.WriteString(filepath.Join(filepath.Dir(srcs[0]), "_covervars.cover.go\n"))
buf.WriteString(filepath.Join(filepath.Dir(srcs[0]), "_covervars.cover.go") + "\n")
}
for _, src := range srcs {
buf.WriteString(strings.TrimSuffix(src, ".go") + ".cover.go\n")
Expand Down
42 changes: 42 additions & 0 deletions tools/please_go/cover/cover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cover

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWriteCoveragePassesAbsoluteSources(t *testing.T) {
tmpDir := t.TempDir()

pkgDir := filepath.Join(tmpDir, "src", "mypkg")
require.NoError(t, os.MkdirAll(pkgDir, 0755))
require.NoError(t, os.WriteFile(filepath.Join(pkgDir, "mypkg.go"), []byte("package mypkg\n"), 0644))

// A fake cover tool that records the arguments it was given.
argsFile := filepath.Join(tmpDir, "args")
coverTool := filepath.Join(tmpDir, "cover.sh")
script := "#!/bin/sh\nprintf '%s\\n' \"$@\" > \"" + argsFile + "\"\n"
require.NoError(t, os.WriteFile(coverTool, []byte(script), 0755))

t.Chdir(tmpDir)
src := filepath.Join("src", "mypkg", "mypkg.go")
outfilelist := filepath.Join(tmpDir, "outfilelist")
require.NoError(t, WriteCoverage("go", coverTool, filepath.Join(tmpDir, "covcfg"), outfilelist, "example.com/mypkg", []string{src}))

args, err := os.ReadFile(argsFile)
require.NoError(t, err)
assert.Contains(t, strings.Fields(string(args)), filepath.Join(pkgDir, "mypkg.go"))
assert.NotContains(t, strings.Fields(string(args)), src)

list, err := os.ReadFile(outfilelist)
require.NoError(t, err)
assert.Equal(t, []string{
filepath.Join(pkgDir, "_covervars.cover.go"),
filepath.Join(pkgDir, "mypkg.cover.go"),
}, strings.Split(strings.TrimSpace(string(list)), "\n"))
}
Loading