Skip to content

Commit 2a39d1e

Browse files
committed
cmd/cover: add //line comment pointing to original file
Now that cover does not modify the formatting of the original file or add any newline characters, we can make it print a //line comment pointing back at the original, and compiler errors and panics will report accurate line numbers. Fixes #6329. Fixes #15757. Change-Id: I7b0e386112c69beafe69e0d47c5f9e9abc87c0f5 Reviewed-on: https://go-review.googlesource.com/77151 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a8474c7 commit 2a39d1e

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/cmd/cover/cover.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ func annotate(name string) {
340340
}
341341
}
342342

343+
fmt.Fprintf(fd, "//line %s:1\n", name)
343344
fd.Write(newContent)
344345

345346
// After printing the source tree, add some declarations for the counters etc.

src/cmd/go/go_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,33 @@ func TestCoveragePattern(t *testing.T) {
24302430
tg.run("test", "-coverprofile="+filepath.Join(tg.tempdir, "cover.out"), "-coverpkg=sleepy...", "-run=^$", "sleepy1")
24312431
}
24322432

2433+
func TestCoverageErrorLine(t *testing.T) {
2434+
tg := testgo(t)
2435+
defer tg.cleanup()
2436+
tg.parallel()
2437+
tg.makeTempdir()
2438+
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2439+
tg.setenv("GOTMPDIR", tg.tempdir)
2440+
2441+
tg.runFail("test", "coverbad")
2442+
tg.grepStderr(`coverbad[\\/]p.go:4`, "did not find correct line number for error")
2443+
tg.grepStderrNot(regexp.QuoteMeta(tg.tempdir), "found temporary directory in error")
2444+
stderr := tg.getStderr()
2445+
2446+
tg.runFail("test", "-cover", "coverbad")
2447+
tg.grepStderr(`coverbad[\\/]p.go:4`, "did not find correct line number for error")
2448+
stderr2 := tg.getStderr()
2449+
2450+
// It's OK that stderr2 drops the character position in the error,
2451+
// because of the //line directive.
2452+
stderr = strings.Replace(stderr, "p.go:4:2:", "p.go:4:", -1)
2453+
if stderr != stderr2 {
2454+
t.Logf("test -cover changed error messages:\nbefore:\n%s\n\nafter:\n%s", stderr, stderr2)
2455+
t.Skip("golang.org/issue/22660")
2456+
t.FailNow()
2457+
}
2458+
}
2459+
24332460
func TestPluginNonMain(t *testing.T) {
24342461
wd, err := os.Getwd()
24352462
if err != nil {

src/cmd/go/testdata/src/coverbad/p.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package p
2+
3+
func f() {
4+
g()
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package p
2+
3+
import "testing"
4+
5+
func Test(t *testing.T) {}

0 commit comments

Comments
 (0)