go-plus icon indicating copy to clipboard operation
go-plus copied to clipboard

Goimports removing needed import

Open deybhayden opened this issue 12 years ago • 23 comments

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.

goimports

deybhayden avatar Aug 11 '14 15:08 deybhayden

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.

jhleath avatar Aug 11 '14 16:08 jhleath

That did work! Thanks for helping me out. :smile:

deybhayden avatar Aug 11 '14 16:08 deybhayden

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.

goimports

deybhayden avatar Aug 14 '14 20:08 deybhayden

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), enter process.env in the Console tab, and paste the current PATH value (as an aside, I really need to just put PATH in Display Go Information also)

Thanks so much for the help!

joefitzgerald avatar Aug 14 '14 20:08 joefitzgerald

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!

deybhayden avatar Aug 14 '14 20:08 deybhayden

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?

joefitzgerald avatar Aug 14 '14 22:08 joefitzgerald

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!

deybhayden avatar Aug 15 '14 02:08 deybhayden

Yet another thing that will likely be fixed by #54 and #55.

joefitzgerald avatar Aug 15 '14 03:08 joefitzgerald

Yeah, looks like it - feel free to close this one out in favor of those tickets. :+1:

deybhayden avatar Aug 15 '14 15:08 deybhayden

This error still happens to me tho. I literally just wrote an import statement with a dash and it got removed.

mainrs avatar Sep 26 '16 00:09 mainrs

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.

joefitzgerald avatar Sep 26 '16 00:09 joefitzgerald

Just checked it out. It is a goimports related issue. Sorry for the inconvenience.

mainrs avatar Sep 26 '16 06:09 mainrs

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

avrono avatar Nov 10 '16 11:11 avrono

i'm having this issue also. i basically have to work without go-plus. anybody have a fix?

gad2103 avatar Nov 23 '16 18:11 gad2103

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.

joefitzgerald avatar Nov 23 '16 18:11 joefitzgerald

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 .

gad2103 avatar Nov 23 '16 19:11 gad2103

+1 still having this issue. Had to change to gofmt to get the issue to stop.

cyphercider avatar Dec 08 '16 21:12 cyphercider

This apparent goimports error happens when you have a global variable, constant or function that defines the same name you're trying to import.

fiatjaf avatar Apr 12 '17 22:04 fiatjaf

use gofmt instead of goimports, it works for me

zannet avatar Apr 30 '17 04:04 zannet

Default behaviour of goimports will remove unused imports --> make sure to use it at least once before you hit save ;)

LouAdrien avatar Nov 27 '17 06:11 LouAdrien