Either that it works or that the documentation would tell me not to expect it to work
What did you see instead?
Can't find import
I spent hours trying to get go/types to analyze some code, and none of the documentation said it didn't work with modules. Modules have been out for like 2 years at this point. Please put clear, hard to miss statements in the comments for go/types that it doesn't support code that uses modules.
Also (and probably something that can be fixed faster), please update the documentation on the examples the godoc links to here: https://github.com/golang/example/tree/master/gotypes so that it, too, says that it won't work with modules. No where did I see any indication that it wouldn't work, except when I posted on Twitter.
The text was updated successfully, but these errors were encountered:
Can you clarify how go/types doesn't work with modules? It's perhaps not trivial to set up correctly, but it certainly works with modules. Over half of the Go tools out there which support analyzing Go code with modules do use go/types.
I think the reply you got on Twitter is mostly right; most tools which just want to load Go packages should use x/tools/go/packages, which uses go/types under the hood and supports modules. But it's certainly possible to use go/types directly and support modules at the same time. Another way to think about it - if go/types simply did not support modules, how would go/packages support modules and expose go/types.Info etc?
I think I have an idea of the problem I was having. I am running tests that parse go files that exist only for testing purposes (under /testdata). Those testdata files sometimes include 3rd party imports that the main program doesn't have, so they're not in the go.mod or go.sum. I presume this screws up go/types, since it doesn't do the auto-download of imports the way the build tools do.
$ go run main.go
2020/12/14 10:59:15 exec: echo foo
failed to check types on files in ./foo/: foo/foo.go:4:2: could not import github.com/magefile/mage/sh (can't find import: "github.com/magefile/mage/sh")