golang/go

cmd/cover: //line directives frequently handled incorrectly

Open

#41,222 opened on Sep 4, 2020

View on GitHub
 (4 comments) (0 reactions) (0 assignees)Go (19,008 forks)batch import
NeedsInvestigationhelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

(This combines #35781, #33690, #25280, and https://github.com/golang/vscode-go/issues/603)

Source files that contain //line directives confuse the coverage tool, prducing coverage output that is unhelpful or confusing to users.

For instance, a short main.go (import path foo.gak) with first lines

//
package main

produces a coverage file:

mode: set
foo.gak/main.go:6.10,8.26 2 1
foo.gak/main.go:12.2,12.19 1 1
foo.gak/main.go:8.26,11.3 2 1
foo.gak/main.go:12.19,13.21 1 0

If the first line is changed to

//line con.go:8

the coverage file changes to:

mode: set
foo.gak/main.go:12.0,14.0 2 1
foo.gak/main.go:18.0,18.0 1 1
foo.gak/main.go:14.0,17.0 2 1
foo.gak/main.go:18.0,19.0 1 0

This file does not refer to con.go, and the ranges are wrong (or, at best, imprecise) for that file, and completely wrong for main.go.

A minimal improvement would be for the coverage tool to change user's //line directives to bare // comments, so that the coverage data corresponds to the file presented to the tool. [Getting results that point to files mentioned in //line directives would require changing the format of the tables in the generated code, at least. Even then the results might be peculiar, for instance if the generated file were run through go fmt.]

Contributor guide