Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nilium opened this issue Feb 6, 2023 · 9 comments
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.

Comments

@nilium
Copy link

nilium commented Feb 6, 2023

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.
@deltamualpha
Copy link

@thanm will probably be interested in this?

@thanm thanm self-assigned this Feb 7, 2023
@thanm
Copy link
Contributor

thanm commented Feb 7, 2023

Thanks for the report. I'll take a look. NB as in many other cases, you can help reduce the frequency of this sort of surprise by running your code through "gofmt".

@thanm thanm added the NeedsFix The path to resolution is known, but the work has not been done. label Feb 7, 2023
@gopherbot
Copy link

Change https://go.dev/cl/466115 mentions this issue: cmd/cover: fix buglet in -covermode=atomic fixup

@dr2chase
Copy link
Contributor

dr2chase commented Feb 8, 2023

Not sure if this is backport-worthy.

@nilium
Copy link
Author

nilium commented Feb 8, 2023

Not sure if this is backport-worthy.

Since it only seems to affect Go 1.20 out of the box where the coverage redesign is in use by default, probably not.

johanbrandhorst pushed a commit to Pryz/go that referenced this issue Feb 12, 2023
Fix a minor buglet in atomic mode fixup that would generate
non-compilable code for a package containing only the "package X"
clause with no trailing newline following the "X".

Fixes golang#58370.

Change-Id: I0d9bc4f2b687c6bd913595418f6db7dbe50cc5df
Reviewed-on: https://go-review.googlesource.com/c/go/+/466115
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
swi-jared added a commit to solarwinds/appoptics-apm-go that referenced this issue Apr 21, 2023
larsve added a commit to modfin/henry that referenced this issue May 8, 2023
Fixes an "syntax error: unexpected var after top level declaration" error when
running 'go test -cover -covermode=atomic ./...' with GO 1.20.

Related to GO issue: golang/go#58370
@pWoLiAn
Copy link

pWoLiAn commented Jun 8, 2023

this issue still exists even on 1.20.5, when running atomic coverage on test files with no new line at EOF🤔

@thanm
Copy link
Contributor

thanm commented Jun 9, 2023

this issue still exists even on 1.20.5, when running atomic coverage on test files with no new line at EOF

That is expected, given that the bugfix hasn't been ported to the 1.20 release branch.

@pWoLiAn
Copy link

pWoLiAn commented Jun 9, 2023

That is expected, given that the bugfix hasn't been ported to the 1.20 release branch.

Oh, thanks for letting me know. I was under the impression the fix was released since the issue was closed/merged PR.
Any particular reason as to why the fix hasn't been released yet?

@thanm
Copy link
Contributor

thanm commented Jun 9, 2023

See https://github.com/golang/go/wiki/MinorReleases .

"Our default decision should always be to not backport, but fixes for security issues, serious problems with no workaround, and documentation fixes are backported to the most recent two release branches, if applicable to that branch. "

A “serious” problem is one that prevents a program from working at all.

100gle added a commit to 100gle/wordcounter that referenced this issue Jun 28, 2023
- see [cmd/cover: cover mode "atomic" generates invalid code for files without a newline at EOF](golang/go#58370)
100gle added a commit to 100gle/wordcounter that referenced this issue Jun 28, 2023
* ci: fix wrong coverage result on badge

* chore: adjust go test command

* chore: disable `atomic` covermode to avoid go bug

- see [cmd/cover: cover mode "atomic" generates invalid code for files without a newline at EOF](golang/go#58370)
dagbay-rh pushed a commit to dagbay-rh/entitlements-api-go that referenced this issue Jan 17, 2024
dagbay-rh added a commit to RedHatInsights/entitlements-api-go that referenced this issue Jan 17, 2024
* update entitlements to go1.20
* add whitespace to avoid bug: golang/go#58370

---------

Co-authored-by: Daniel Agbay <Daniel Agbay>
darccio added a commit to DataDog/dd-trace-go that referenced this issue Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants