Skip to content

A black box test that uses a name from export_test.go makes gobco panic with "undefined" #42

Open

Description

@jmrplens

Since 1.3.4, gobco type-checks a black box test (package x_test) against the package x as the source importer loads it, which is without the test files of x. An export_test.go is the usual Go idiom for handing an unexported name to a black box test, and as soon as the black box test uses such a name, gobco panics before anything is instrumented, while go test passes on the same package.

This is independent of #40, which I reported about the files that build constraints exclude, and of #41, my open pull request for it: those are about which files gobco reads from the directory, this one is about how the imports of the black box test are resolved. The reproduction below has no build constraint at all, and gobco built from the branch of #41 panics on it in the same way.

Minimal reproduction

Four files in an empty directory:

go.mod

module example.com/repro

go 1.22

repro.go

package repro

func isEven(n int) bool {
	return n%2 == 0
}

export_test.go

package repro

var IsEven = isEven

repro_test.go

package repro_test

import (
	"testing"

	"example.com/repro"
)

func TestIsEven(t *testing.T) {
	if !repro.IsEven(2) {
		t.Error("2 is even")
	}
}

On Linux:

$ go test ./... -count=1
ok  	example.com/repro	0.002s
$ go run github.com/rillig/gobco@v1.3.4
panic: repro_test.go:10:12: undefined: repro.IsEven

goroutine 1 [running]:
main.ok(...)
	/go/pkg/mod/github.com/rillig/gobco@v1.3.4/util.go:100
main.(*instrumenter).resolveTypes(0x2c02475de1c0, 0x2c024759d500)
	/go/pkg/mod/github.com/rillig/gobco@v1.3.4/instrumenter.go:143 +0x445
main.(*instrumenter).instrument(0x2c02475de1c0, {0x6c3000, 0x1}, {0x0, 0x0}, {0x2c02475aea00, 0x33})
	/go/pkg/mod/github.com/rillig/gobco@v1.3.4/instrumenter.go:95 +0x105
main.(*gobco).instrument(0x2c02475e8120)
	/go/pkg/mod/github.com/rillig/gobco@v1.3.4/main.go:275 +0x2b6
main.gobcoMain({0x96fc10?, 0x2c0247582058?}, {0x96fc10?, 0x2c0247582060?}, {0x2c02475ac1f0, 0x1, 0x1})
	/go/pkg/mod/github.com/rillig/gobco@v1.3.4/main.go:27 +0x67
main.main()
	/go/pkg/mod/github.com/rillig/gobco@v1.3.4/main.go:20 +0x45
exit status 2

1.3.3 reports Condition coverage: 1/2 for this package, as it did not resolve types yet, and gobco built from master at 7a09995 panics like 1.3.4. All measured with Go 1.27.1 on linux/amd64.

Where

resolveTypes type-checks each package of the directory with the source importer from importer.ForCompiler(fset, "source", nil). When it checks x_test, that importer loads x from the GoFiles and CgoFiles that go/build lists, so a name that export_test.go declares does not exist there, and ok(err) panics.

go test builds the test binary differently: the package under test includes its own test files, and each package that the black box test imports and that imports x, directly or indirectly, is compiled again against that x. go list -test -deps shows each such package with the test binary in brackets, the way it shows the package under test here, example.com/repro [example.com/repro.test]. So replacing only the import of x is not enough: a package that the black box test imports and that passes values of the types of x to it would still refer to the other x.

Possible fix

Type-check x first, together with its test files, and give x_test an importer that returns that package for the import path of x, type-checks each imported package that depends on x again against it, once per package, and leaves every other import to the source importer.

I have a patch for this with tests on the branch export-test-package of my fork, and I'll open a pull request from it that refers to this issue. Feel free to take only what you like from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions