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/types: Type checker does not handle some imported types #9702

Closed
obeattie opened this issue Jan 27, 2015 · 6 comments
Closed

x/tools/go/types: Type checker does not handle some imported types #9702

obeattie opened this issue Jan 27, 2015 · 6 comments

Comments

@obeattie
Copy link

Using Go 1.4.1, it seems that the type checker cannot handle some imported type declarations. This was found using stringer. By way of example, I can create a project with two packages, domain and impl:

domain/domain.go:

package domain

type Value map[string]string

impl/impl.go:

package impl

import (
    "github.com/obeattie/test/domain"
)

type Foo interface {
    Bar(domain.Value)
}

Upon invoking go generate (and therefore stringer, and therefore the type checker), I get the following error:

stringer: checking package: impl.go:8:6: Value not declared by package domain

I would not expect this to cause an issue, given this will compile just fine.

@minux
Copy link
Member

minux commented Jan 27, 2015

What if you first do
go install domain
And then invoke go generate on impl directory?

@ianlancetaylor ianlancetaylor changed the title Type checker does not handle some imported types x/tools/go/types: Type checker does not handle some imported types Jan 28, 2015
@ianlancetaylor
Copy link
Contributor

You wrote domain/domain.go but your impl/impl.go imports test/domain. Can you confirm that your impl.go is importing the domain package that contains domain/domain.go?

@obeattie
Copy link
Author

@ianlancetaylor Yes. I wrote the code above as an example, but did a bad job of making it clear. I originally encountered the issue in this code (you need to checkout that specific branch if you want to see for yourself).

To @minux's point, yes, running go install github.com/obeattie/sase/domain first does fix this. Is this, in fact, expected behaviour?

@minux
Copy link
Member

minux commented Jan 28, 2015

It seems a lot of tools expect you to have installed all dependent
packages, so in that sense, it's expected behavior, as of today.

However, that is certainly not ideal, given that source code is
available, it probably should fallback to source code if the installed
package is not available or stale.

I will leave the judgement to the x/tools developers.

/cc @alandonovan and @griesemer

@adonovan
Copy link
Member

Used directly, the type checker satisfies direct import dependencies by loading export data for installed packages. This assumes that the packages have been built and installed more recently than their source code was edited.

Because this is assumption is often false, the golang.org/x/tools/go/loader package provides a means of loading all dependencies from source code as needed. I recommend that you use that.

The stringer tool doesn't use go/loader; perhaps it should.

@griesemer griesemer assigned robpike and unassigned griesemer Jan 29, 2015
@rsc rsc modified the milestones: Unplanned, Unreleased Apr 10, 2015
@rsc rsc removed the repo-tools label Apr 14, 2015
@robpike
Copy link
Contributor

robpike commented Jun 2, 2015

Expected behavior as explained above.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants