From 02cffcde177bf3fbb36e75d749d0cb338b4f7566 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Fri, 28 Oct 2022 13:46:32 -0400 Subject: [PATCH] cmd/cover: fix buglet causing differences in -m output Use a slightly different line number pragma when emitting instrumented code, so as to ensure that we don't get any changes in the "-gcflags=-m" output for coverage vs non-coverage. Fixes #56475. Change-Id: I3079171fdf83c0434ed6ea0ce3eb2797c2280c55 Reviewed-on: https://go-review.googlesource.com/c/go/+/446259 TryBot-Result: Gopher Robot Run-TryBot: Than McIntosh Reviewed-by: Cherry Mui --- src/cmd/cover/cover.go | 2 +- src/cmd/go/testdata/script/cover_error.txt | 33 ++-------------------- 2 files changed, 3 insertions(+), 32 deletions(-) diff --git a/src/cmd/cover/cover.go b/src/cmd/cover/cover.go index 53a5b6df41d..989c109a79a 100644 --- a/src/cmd/cover/cover.go +++ b/src/cmd/cover/cover.go @@ -629,7 +629,7 @@ func (p *Package) annotateFile(name string, fd io.Writer, last bool) { } newContent := file.edit.Bytes() - fmt.Fprintf(fd, "//line %s:1\n", name) + fmt.Fprintf(fd, "//line %s:1:1\n", name) fd.Write(newContent) // After printing the source tree, add some declarations for the diff --git a/src/cmd/go/testdata/script/cover_error.txt b/src/cmd/go/testdata/script/cover_error.txt index 15838d5609d..fa4b58bff7f 100644 --- a/src/cmd/go/testdata/script/cover_error.txt +++ b/src/cmd/go/testdata/script/cover_error.txt @@ -5,17 +5,12 @@ # Get errors from a go test into stderr.txt ! go test coverbad -stderr 'p\.go:4' # look for error at coverbad/p.go:4 -[cgo] stderr 'p1\.go:6' # look for error at coverbad/p.go:6 +stderr 'p\.go:4:2' # look for error at coverbad/p.go:4 +[cgo] stderr 'p1\.go:6:2' # look for error at coverbad/p.go:6 ! stderr $WORK # make sure temporary directory isn't in error cp stderr $WORK/stderr.txt -# Clean out character positions from stderr.txt -# It's OK that stderr2 drops the character position in the error, -# because of the //line directive (see golang.org/issue/22662). -go run clean_charpos.go $WORK/stderr.txt & - # Get errors from coverage into stderr2.txt ! go test -cover coverbad cp stderr $WORK/stderr2.txt @@ -48,27 +43,3 @@ package p import "testing" func Test(t *testing.T) {} --- clean_charpos.go -- -// +build ignore - -package main - -import ( - "log" - "os" - "strings" -) - -func main() { - log.SetFlags(0) - b, err := os.ReadFile(os.Args[1]) - if err != nil { - log.Fatal(err) - } - s := strings.ReplaceAll(string(b), "p.go:4:2:", "p.go:4:") - s = strings.ReplaceAll(s, "p1.go:6:2:", "p1.go:6:") - os.WriteFile(os.Args[1], []byte(s), 0644) - if err != nil { - log.Fatal(err) - } -}