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
gotype: type-checking external test files that rely on internal test files fails #22030
Comments
This is not a The documentation inside
so this is at least working as internally documented. But it should be documented externally, and furthermore, this behavior seems incorrect. It appears that go test does something different. |
This is not a trivial fix. An external test package imports the local package consisting of the local package and local test files. The go/command builds that package in a temporary directory and then imports it from that temporary directory to make it all work. gotype relies on go/types to type-check the external test package. go/types simply uses an importer to get the information about the package to be tested, and that importer doesn't know that it needs that package built with its local test files. Furthermore, the existing top-level API (go/importer) doesn't have a mechanism to communicate that information. Also, if the importer is not a "source" importer, but say a "gc" importer, the importer relies on installed packages, and they are never built with the local test files. One option might be to implement a new custom importer, only known to gotype.go, which is based on go/internal/srcimporter. The srcimporter's mode argument (currently unused) could be used to signal that local test files need to be included. But that would not work for Still thinking. |
Punting to Go 1.11 since gotype is an unreleased program. |
I tried to use
go/types
to type check a package with internal test files (withpackage foo
) and external test files (withpackage foo_test
). The internal test files expose some symbols that the external test files consume. This works fine ingo test
. However, thego/types
code does not recognize the symbols exported by the internal test files.I want to use the
go/types
package to write a refactoring tool that consumes external test files that rely on symbols exported by internal tests files, but I can't because of this error.I think the issue is that the
go/types
import logic ignores the internal_test.go
files when type-checking the external_test.go
files. Thego/types
config https://golang.org/pkg/go/types/#Config does not seem to have a knob to import_test.go
files.What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
I created a new package
testgotypes
with 3 files:testgotypes.go
, without this file gotype doesn't run:internal_test.go
, which exports a variable for the external test:external_test.go
, which tries to access the variable exported byinternal_test.go
:I ran tests, which worked just fine:
Then I tried to run
gotype
, compiled from/usr/local/Cellar/go/1.9/libexec/src/go/types/gotype.go
:It fails with the same error my refactoring code sees.
To sanity check
gotype
, I then renamedinternal_test.go
tointernal.go
to make it part of the non-test build. Thengotype
is happy:What did you expect to see?
gotype
successfully type checkingexternal_test.go
.What did you see instead?
gotype
fails to type check theexternal_test.go
file.The text was updated successfully, but these errors were encountered: