Goimports removing needed import
Hey there, love this package but having a weird issue with goimports running on save and removing a needed import. When I run goimports directly from the command line I don't have this issue. See the GIF below.

I think this is an issue with goimports, not necessarily go-plus. I have the same experience, and it always happens on import paths with a dash included go-ini, go-cache, etc.
A workaround that I use is to name the import, and use the name in the source. This prevents goimports from removing in.
package main
import (
gini "github.com/crowdmob/vaughn0/go-ini"
)
func something(cfg gini.File) {}
This code should not remove the import.
That did work! Thanks for helping me out. :smile:
Hey there - so I'm fairly sure this has something to do with the Go Plus implementation of goimports on save. If I run goimports -w foo.go with a file that has a '-' import that needs to be added it is correctly done. However, just running save in Atom it isn't added.

Interesting. I'd love to find a way to reproduce this - it sounds very frustrating! Could you please:
- Paste the output of
Packages(Menu) >Go Plus>Display Go Information - Create a gist with the file(s) that can be used to reproduce this
- Open Atom Developer Tools (
CMD-Option-I<< that's a capital i), enterprocess.envin theConsoletab, and paste the current PATH value (as an aside, I really need to just put PATH inDisplay Go Informationalso)
Thanks so much for the help!
Go Information
Using Go: go1.3 darwin/amd64 (@/usr/local/bin/go)
GOPATH: /Users/benhayden/Documents/go-zone
Cover Tool: /usr/local/Cellar/go/1.3/libexec/pkg/tool/darwin_amd64/cover
Vet Tool: /usr/local/Cellar/go/1.3/libexec/pkg/tool/darwin_amd64/vet
Format Tool: /Users/benhayden/Documents/go-zone/bin/goimports
Lint Tool: /Users/benhayden/Documents/go-zone/bin/golint
Oracle Tool: /Users/benhayden/Documents/go-zone/bin/oracle
Gist
It seems like what @huntaub said is right, so in my example, I'm using go-ini.
$> go get github.com/vaughan0/go-ini
Then you can use this gist to test. On save in Atom the import line will be rewritten to
import "fmt"
But if you use goimports -w dashes.go nothing changes.
Dev Tools Console Tab
PATH: "/usr/bin:/bin:/usr/sbin:/sbin"
Let me know if you need anything else!
PATH: "/usr/bin:/bin:/usr/sbin:/sbin"
This indicates that you have launched Atom from one of:
- Finder
- Dock
- Spotlight
- Anything launched by them, or by
launchctl
Can you try installing Atom Shell Commands (Atom (Menu) > Install Shell Commands), open your terminal of choice, verify PATH contains /usr/local/bin in it (run env), then launch atom from the command line in your directory of choice?
Does goimports work as expected then?
I was launching from the Dock for sure - and you are correct, launching from iTerm2 indeed set the path correctly and goimports works correctly. I didn't figure that'd be a path issue since the plugin had me set GOPATH explicitly and said it found goimports. Thanks for the debugging help!
Yet another thing that will likely be fixed by #54 and #55.
Yeah, looks like it - feel free to close this one out in favor of those tickets. :+1:
This error still happens to me tho. I literally just wrote an import statement with a dash and it got removed.
I'm happy to fix this if you can show me that it's a go-plus issue and not a goimports issue. I need a test case where you run goimports on the command line and the outcome is different than if you use goimports via go-plus. In lieu of that, your issue is likely with goimports itself. If that is your issue, you can always switch to use gofmt instead, via preferences.
Just checked it out. It is a goimports related issue. Sorry for the inconvenience.
I am having the same issue, with an import "myproj/mylib". Works fine on the command line but removes the import on save even is named e.g `person "myproj/mylib" ... using go-plus 4.3.2 on ATOM 1.11.2
i'm having this issue also. i basically have to work without go-plus. anybody have a fix?
You never need to stop using go-plus. Your issue is with goimports. You can change your package preferences to use gofmt instead of goimports.
That worked. Thank you! On Wed, Nov 23, 2016 at 1:53 PM Joe Fitzgerald [email protected] wrote:
You never need to stop using go-plus. Your issue is with goimports. You can change your package preferences to use gofmt instead of goimports.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/joefitzgerald/go-plus/issues/52#issuecomment-262601327, or mute the thread https://github.com/notifications/unsubscribe-auth/AA_zEUlxJ0vUTwaqcgSTw_xR1gizg76yks5rBIvFgaJpZM4CWIX8 .
+1 still having this issue. Had to change to gofmt to get the issue to stop.
This apparent goimports error happens when you have a global variable, constant or function that defines the same name you're trying to import.
use gofmt instead of goimports, it works for me
Default behaviour of goimports will remove unused imports --> make sure to use it at least once before you hit save ;)