-
Notifications
You must be signed in to change notification settings - Fork 17.3k
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/build and go/types: import path bug in go1.5 #12050
Comments
As far as I can see, the issue is related to the following code.
Where the variable "pkga" is given a value of "/home/netbrain/dev/go/pkg/linux_amd64/github.com/netbrain/importbug/bar.a", which is a file that doesn't exist. @mwhudson, @rsc any idea whats going on with this? any help is greatly appreciated! |
I believe this is not a bug importbug/imporbug_test.go does not import " If I go install those packages then run the test it passes as expected. On Thu, Aug 6, 2015 at 8:59 PM, Kim Eik notifications@github.com wrote:
|
I guess that makes a lot of sense. however github.com/netbrain/importbug/foo is correctly parsed with |
Can you try after running rm -rf $GOPATH/pkg ? On Thu, Aug 6, 2015 at 9:10 PM, Kim Eik notifications@github.com wrote:
|
Still the same result
|
i can confirm running go install in github.com/netbrain/importbug/bar will fix the test. However, this would mean that all packages i would want to inspect with go/types would have to be installed? |
This looks to be the case. The only implementation of types.Importer I could find was go/importer which operated on the compiled object file. I don't know if this is a hard restriction, or just the most expedient. |
I find this quite weird, one would think that if I guess I will look some more at this issue. Anyways thank you for your assistance thus far 👍 |
I think this is working as expected, but Robert can decide. In any case it may require better documentation. |
@netbrain This is working as intended. go/types type-checks a single package, the one for which the source (== AST) is provided. Any imports are delegated to the importer which looks at compiled packages (which must be installed). It is possible to provide an importer that looks at the source: such an importer would have to first parse and type-check the imported package from source, and possibly do the same with its own imports, recursively so. There are also questions about how much to cache (we don't want to re-parse and re-type-check the same package over and over) and when to invalidate the cache, which files to consider, etc. It's not entirely trivial to get this right on all platforms and taking into account build flags etc. which is why importing is separated out from go/types as an independent mechanism. The x/tools/go/loader package provides much of the functionality but it's not in the std lib yet, exactly because it's complicated and the API is not yet fixed. If you need this working for you now, you can implement your own importer and provide it to go/types. You could look at (and copy from) src/cmd/api/goapi.go:421 for an outline of an importer that does about what you might want, and then customize it as needed. Eventually go/importer may provide a source importer. |
@griesemer thank you for the detailed explanation. For the performance reasons you describe it is probably better to require installation of all packages instead of creating my own source importer. But i guess the documentation or atleast the error message should indicate that imported packages through go/types need to be compiled in order for the library to discover them. Anyways, ill leave that up to you guys. Thanks again! |
go/types doesn't care or even know how the package data structure of imported packages came into being, it just cares about the data which is returned by an importer. go/types' access to imported packages is via the importer function which is implemented externally. The go/importer package provides (at the moment) two importers, one for packages compiled by gc and one for packages compiled by gccgo. The documentation in that package explicitly mentions the compilers. But it is correct that it doesn't say that the importers read from the installed binary files. @alandonovan has been working on a longer and detailed explanation of how to use go/types and how it all interacts with the source code, installed code, etc. It will be available with the 1.5 release (or shortly thereafter). |
What version of Go are you using (go version)?
go version go1.5rc1 linux/amd64
What operating system and processor architecture are you using?
debian jessie / amd64
What did you do?
Im trying to read package information with the go/build package, then parsing the package using the go/types package. The package i'm trying to parse has a dependency on another package, and this is where the issue lies.
What did you expect to see?
Successfull import of package "bar"
What did you see instead?
Failed to import package "bar"
I have created a code sample which reproduces this issue: https://github.com/netbrain/importbug
I tried to debug the issue with gdb, and a discovered a couple of things, when build.Import is invoked on the package "github.com/netbrain/importbug/bar" then on line 603 in build.go this gets resolved to "/home/netbrain/dev/go/pkg/linux_amd64/github.com/netbrain/importbug/bar.a", which doesn't exist.
furthermore, the for loop @ gcimporter.go:66 loops through the following filenames
"/home/netbrain/dev/go/pkg/linux_amd64/github.com/netbrain/importbug/bar.a"
"/home/netbrain/dev/go/pkg/linux_amd64/github.com/netbrain/importbug/bar.o"
as these files doesn't exist, it simply returns a blank string. which means the package was not found. Resulting in the error displayed in the test.
The following is a backtrace of the test, showing the involved packages and function calls.
in gdb I had the following breakpoints set
break resolver.go:178
break gcimporter.go:116
break gcimporter.go:37
break gcimporter.go:47
break build.go:460
break build.go:1002
The text was updated successfully, but these errors were encountered: