golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.11k stars 17.37k forks source link

x/playground: func Test doesn't work in module mode: function main is undeclared #66451

Open dolmen opened 3 months ago

dolmen commented 3 months ago

func Test(*testing.T) is not executed on https://go.dev/play when a go.mod file is provided.

What did you do?

This doesn't run: https://go.dev/play/p/ZDFf4aGucoB

package main

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func Test(t *testing.T) {
    assert.True(t, true)
}
-- go.mod --
module play.ground

require github.com/stretchr/testify v1.9.0

What did you see happen?

Output:

go: downloading github.com/stretchr/testify v1.9.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.1
# play.ground
runtime.main_main·f: function main is undeclared in the main package

What did you expect to see?

If I remove the go.mod, it runs correctly.

Example: https://go.dev/play/p/AsRNcxCRdWx

=== RUN   Test
--- PASS: Test (0.00s)
PASS
dr2chase commented 3 months ago

I am not sure this is a bug, there is in fact no main function in the main package. Not-module-mode is now a special case, at least as I understand it.

@rsc (for go/build) and @golang/tools-team (for the playground).

dolmen commented 3 months ago

Reproducible builds on the Go playground (enforced by locking dependencies with go.mod) are an important feature (even if still underused) to help bug reporters to demonstrate issues. A func(*testing.T) is a good way to convey that as the code provided can easily be integrated in a test suite.

This may not qualify as "bug" but "feature request" or "proposal", but I argue it is an important feature for the Go community.

matloob commented 3 weeks ago

On the bottom of go.dev/play it says

If the program contains tests or examples and no main function, the service runs the tests.

It sounds like we should either support this or update that text to make it clear it's not supported in module mode?

On the issue of reproducibility, it's harder to guarantee that what's on the playground is reproducible. The playground runs the build with -mod=mod which will pull in dependencies that are missing from an untidy go.mod and will set the module's go version (which controls language semantics) to the version of the go command that's running.