In parse.go, we have the following call to infer what packages should be imported for the generated file, where filename represents the input file name.
Passing the input filename might cause some packages not being imported in the output. For example, say, we have the following dir structure (the buggy code can be downloaded here):
Since we passed in.template.go as the filename in our case, imports.Process ignores it during the inference procedure and thus can't infer the util package.
The PR fixes this bug by passing the output file name to imports.Process instead. In this case, imports.Process can exploit the information collected from in.template.go to add the util package to the out.go.
In
parse.go
, we have the following call to infer what packages should be imported for the generated file, wherefilename
represents the input file name.Passing the input filename might cause some packages not being imported in the output. For example, say, we have the following dir structure (the buggy code can be downloaded here):
in.template.go
:util.go
:After running
go generate
, thegithub.com/zwcn/genny-test/util
is missing inout.go
. It's missing becauseimports.Process
uses all go files (except thefilename
) in the dir in which thefilename
is to infer the set of imported packages. For details, please see https://github.com/golang/tools/blob/16909d206f00da7d0d5ba28cd9dc7fb223648ecf/imports/fix.go#L472.Since we passed
in.template.go
as thefilename
in our case,imports.Process
ignores it during the inference procedure and thus can't infer theutil
package.The PR fixes this bug by passing the output file name to
imports.Process
instead. In this case,imports.Process
can exploit the information collected fromin.template.go
to add theutil
package to theout.go
.