cheekybits / genny

Elegant generics for Go
MIT License
1.71k stars 127 forks source link

Some imports being lost on generation #14

Closed watchforstock closed 8 years ago

watchforstock commented 8 years ago

It appears than non-built-in import statements are being lost on generation. As an example, take a "simpletest.go" file that looks like this:

package test

import (
        "fmt"
        "github.com/gorilla/mux"
        "github.com/cheekybits/genny/generic"
)

type MyType generic.Type

func MyTypeFunction() {
        _ := mux.NewRouter()
        fmt.Println("Hello")
}

if I then run the command cat simpletest.go | genny gen "MyType=ConcreteType" > simpletest_gen.go I get the following output:

// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny

package test

import "fmt"

func ConcreteTypeFunction() {
    _ := mux.NewRouter()
    fmt.Println("Hello")
}

You can see the import statement has correctly dropped the genny reference, but also incorrectly removed the mux reference which means the code is now invalid.

Am I doing something wrong?

watchforstock commented 8 years ago

Upon further investigation, it looks like this was largely down to a GOPATH issue meaning it couldn't find the libraries when running the imports.Process function.

falun commented 8 years ago

watchforstock - what was your diagnostic process like here? I'm seeing similar behavor where some imports are being dropped. In my case they're all in my repo so inability to find the libraries seems unlikely.

falun commented 8 years ago

For future readers: imports seem to be completely managed by a call to imports.Process -- the genny processing doesn't even read them in deferring entirely to imports.Process to consume the template and produce a sorted/culled list of imports.

edit: actually -- backing off -- genny seems to pass the contents of the template into Process's src parameter so without digging into imports pkg I'm not sure where it's getting the potential imports at all.