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

x/tools/go/packages: _test packages cannot be loaded directly #66444

Closed
thesilentg opened this issue Mar 21, 2024 · 3 comments
Closed

x/tools/go/packages: _test packages cannot be loaded directly #66444

thesilentg opened this issue Mar 21, 2024 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@thesilentg
Copy link

Go version

go version go1.22.0 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN='/Users/aggnolek/go/bin'
GOCACHE='/Users/aggnolek/Library/Caches/go-build'
GOENV='/Users/aggnolek/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/aggnolek/go/pkg/mod'
GONOPROXY='*'
GONOSUMDB='*'
GOOS='darwin'
GOPATH='/Users/aggnolek/go'
GOPRIVATE='*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/opt/go/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/opt/go/libexec/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/aggnolek/gorepos/aggnolek/refactor-playground/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 -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/x7/2f8ynt3954s4y78yt_54v9fr0000gs/T/go-build471022763=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

go.mod

github.com/fake/scratchpad

go 1.21

example2/main.go

package main

import (
	"fmt"
	"go/token"

	"golang.org/x/tools/go/packages"
)

func main() {
	fileSet := token.NewFileSet()

	cfg := &packages.Config{
		Fset:  fileSet,
		Mode:  packages.NeedModule | packages.NeedName | packages.NeedFiles,
		Tests: true,
	}

	pkgs, _ := packages.Load(cfg, "github.com/fake/scratchpad/example2/nested")
	for _, pkg := range pkgs {
		fmt.Println(pkg)
	}

	pkgs, _ = packages.Load(cfg, "github.com/fake/scratchpad/example2/nested_test")
	packages.PrintErrors(pkgs)
}

example2/nested/example.go

package nested

func DoNothing() {}

example2/nested/example_test.go

package nested

import "testing"

func TestDoNothing(t *testing.T) {
	DoNothing()
}

example2/nested/example_external_test.go

package nested_test

import (
	"scratchpad/example2/nested"
	"testing"
)

func TestExternalDoNothing(t *testing.T) {
	nested.DoNothing()
}

In the example2 directory, ran go run main.go

What did you see happen?

Output:

github.com/fake/scratchpad/example2/nested
github.com/fake/scratchpad/example2/nested.test
github.com/fake/scratchpad/example2/nested [github.com/fake/scratchpad/example2/nested.test]
github.com/fake/scratchpad/example2/nested_test [github.com/fake/scratchpad/example2/nested.test]
-: no required module provides package github.com/fake/scratchpad/example2/nested_test; to add it:
        go get github.com/fake/scratchpad/example2/nested_test

What did you expect to see?

From 1

Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary.

Is it expected that these _test packages cannot be directly loaded via packages.Load?

Note that the original call to Load of the nested package does return the associated nested_test package in the results, but the second call of Load which attempts to directly load the nested_test package fails.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Mar 21, 2024
@gopherbot gopherbot added this to the Unreleased milestone Mar 21, 2024
@dr2chase
Copy link
Contributor

@adonovan not sure if this is for you or not.

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 21, 2024
@adonovan
Copy link
Member

adonovan commented Mar 21, 2024

Is it expected that these _test packages cannot be directly loaded via packages.Load?

Yes; go/packages is at the mercy of the underlying build system's query tool. Queries to build systems such as blaze, bazel, and similar ones, return names for build targets that are themselves valid queries. That's not the case with 'go list': its inputs are essentially the names of directories, each defining up to four packages (p, p [p.test], p_test [p.test], p.test), and these package identifiers are not valid query inputs.

So unfortunately there's not really much go/packages can do here.

@thesilentg
Copy link
Author

Thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants