Skip to content

cmd/cover: cover mode "atomic" generates invalid code for files without a newline at EOF #58370

Closed
@nilium

Description

@nilium

What version of Go are you using (go version)?

$ go version
go version go1.20 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ncower/.cache/go-build"
GOENV="/home/ncower/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ncower/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ncower/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/ncower/p/break/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build76322923=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Ran go test -cover -covermode=atomic on a package with a file that had a newline missing at EOF.

All code referred to is also under https://github.com/nilium/go-issue-test/tree/38c0047d4219026ecccbb2f35f96162fef5478a7. Because it involves the trailing newline at EOF on sources, there isn't a good way to reproduce it in the playground as far as I can see.

A more or less empty file can be used to reproduce this provided the empty.go file does not have a newline at EOF:

empty.go

package noeol

some_test.go (doesn't matter if this file has a newline at EOF to reproduce)

package noeol

import "testing"

func TestNothing(t *testing.T) {
}

What did you expect to see?

For test output:

$ go test -cover -covermode=atomic ./noeol
ok      github.com/nilium/go-issue-test/noeol   0.001s  coverage: [no statements]

What did you see instead?

$ go test -cover -covermode=atomic ./noeol
# github.com/nilium/go-issue-test/noeol [github.com/nilium/go-issue-test/noeol.test]
noeol/empty.go:2:51: syntax error: unexpected var after top level declaration
FAIL    github.com/nilium/go-issue-test/noeol [build failed]
FAIL

with the generated code for mode=atomic resembling the following:

//line noeol/empty.go:1:1
// empty.go
package noeol; import _cover_atomic_ "sync/atomic"var _ = _cover_atomic_.LoadUint32

var CoverageVariableNameP uint32
// etc.

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions