Description
I just updated to 0.6.49 and format on save is now broken for goimports. After looking at recent changes, I believe that b143a0d triggered the breakage.
The difference is that the text for the file is passed via stdin instead of providing the on-disk filename. The consequence of passing from stdin is that the goimports process is not able to determine what global variables exist within the package. For example, if the global variable "log" is declared foo.go, while saving bar.go which calls "log.Info" or some other function which the "log" package declares, the command will operate on local data and assume you want to import the "log" package rather than using the package global variable which results in the tool incorrectly adding the "log" import.
The fix for this is to do one of the following:
- Continue to pass the file contents via stdin and invoke as "goimports -d -srcdir [ filename ]" (see goimports help for the -srcdir flag semantics when a filename is specified)
- Do not pass the file contents via stdin and invoke "goimports -d [ filename ]"
Unfortunately, there doesn't appear to be a way to configure the current filename as a parameter in "go.FormatFlags" otherwise that would be an easy workaround.