-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
go/types: clearly mark the package as not supporting modules #43170
Comments
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. |
An example of a tool that supports modules and uses go/types directly: https://github.com/burrowers/garble/blob/2e2bd09b5e420455f51f7e1e1cbe46a11a6a7cf3/main.go#L409-L413 Granted that it's a complex file and not a very clear example, but just to prove a point. I agree that better docs would be nice, but they should definitely not say "does not support modules". |
hmm... lemme recheck what I was doing. I have an incredibly basic test that fails, but I may be making a simple mistake. I heard from twitter that go/types didn't work with modules and ... probably should have double checked that assertion :) Lemme get back to you in a bit. |
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. |
Previously I was just using go/ast to parse them, and it doesn't care about imports, so these tests worked fine. |
Yeah, ok, no, that's not it either. here's my test: $ go run main.go main.go package main
import (
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"play.ground/foo"
)
func main() {
foo.Bar()
fset := token.NewFileSet()
path := "./foo/"
f, err := parser.ParseFile(fset, "foo/foo.go", nil, 0)
if err != nil {
panic(err)
}
info := &types.Info{
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Types: make(map[ast.Expr]types.TypeAndValue),
}
conf := types.Config{Importer: importer.Default()}
if _, err = conf.Check(path, fset, []*ast.File{f}, info); err != nil {
fmt.Printf("failed to check types on files in %s: %s\n", path, err)
}
} foo/foo.go package foo
import (
"github.com/magefile/mage/sh"
)
func Bar() {
sh.RunV("echo", "foo")
} go.mod
|
Seeing your example, I still think that your answer here is that you should be using Closing as a duplicate of #28328 and #31821. But also note that trying to use the importer interface yourself directly is not a great idea; see #44630. This is another reason why you should just rely on |
What did you do?
Tried to use go/types with code that uses modules
What did you expect to see?
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
The Problem
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: