Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Produced code does not compile due to incorrect import resolution #2

Open
Bo0mer opened this issue Aug 10, 2018 · 0 comments
Open

Produced code does not compile due to incorrect import resolution #2

Bo0mer opened this issue Aug 10, 2018 · 0 comments

Comments

@Bo0mer
Copy link

Bo0mer commented Aug 10, 2018

What I'm doing:

$ cd $GOPATH/src/path/to/my/project
$ gostub Service
$ echo $?
0
$ go build ./service_stubs/service_stub.go
# command-line-arguments
service_stubs/service_stub.go:128:5: cannot use new(ServiceStub) (type *ServiceStub) as type service.Service in assignment:
	*ServiceStub does not implement service.Service (wrong type for GetSession method)
		have GetSession(context.Context, string) (*mgo.Session, error)
		want GetSession(context.Context, string) (*model.Session, error)

The problem seems that the current LocatorContext implementation (see resolution/context.go) does not take into account the package name when computing possible import paths for non-local non-aliased locations:

func (c *LocatorContext) NonLocalNonAliasedLocations() []string {
	result := []string{}
	for _, imp := range c.imports {
		if imp.Alias == "" {
			result = append(result, imp.Location)
		}
	}
	return result
}

The fix I've used is to make sure that the import location ends with the package that is being looked for:

diff --git a/resolution/context.go b/resolution/context.go
index ffd5af3..d75b6f5 100644
--- a/resolution/context.go
+++ b/resolution/context.go
@@ -58,7 +58,7 @@ func (c *LocatorContext) CandidateLocations(alias string) []string {
        if location, found := c.AliasedLocation(alias); found {
                return []string{location}
        }
-       return c.NonLocalNonAliasedLocations()
+       return c.NonLocalNonAliasedLocations(alias)
 }

 func (c *LocatorContext) LocalLocations() []string {
@@ -71,10 +71,10 @@ func (c *LocatorContext) LocalLocations() []string {
        return result
 }

-func (c *LocatorContext) NonLocalNonAliasedLocations() []string {
+func (c *LocatorContext) NonLocalNonAliasedLocations(alias string) []string {
        result := []string{}
        for _, imp := range c.imports {
-               if imp.Alias == "" {
+               if imp.Alias == "" && strings.HasSuffix(imp.Location, alias) {
                        result = append(result, imp.Location)
                }
        }

I'm not sure whether this won't brake any other scenarios, but unfortunately I don't have much time to spend digging it right now, so I'll go with that fix. Here's what happens after applying it:

$ gostub Service
Stub 'ServiceStub' successfully created in 'service_stubs'.
$ go build ./service_stubs/service_stub.go
$ echo $?
0

I'll not be able to share the code that reproduces the issues as-is, but if you'd like I can create a minimal scenario that reproduces the problem, so its added to the acceptance test suite (which are still passing with the fix applied).

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

No branches or pull requests

1 participant