Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

"Format Code" imports wrong package #109

Closed
ivoviz opened this issue Nov 27, 2015 · 5 comments
Closed

"Format Code" imports wrong package #109

ivoviz opened this issue Nov 27, 2015 · 5 comments

Comments

@ivoviz
Copy link

ivoviz commented Nov 27, 2015

Let's say I have a structure like this (in the src folder, obviously):

github.com/ivoviz/app1
github.com/ivoviz/app2
github.com/ivoviz/app3

Let's also assume that both app1 and app2 has a package called db. Now I haven't really tested out when does the import work properly and when not (just came here to report the issue asap), but in my case I have used the db package in app2, then used "Format Code". After half an hour I realized it imported the package from app1.

In my case both db packages were similar - not exactly the same -, but they both had that exact function.

@newhook
Copy link
Contributor

newhook commented Nov 27, 2015

This doesn't really have anything to do with vscode-go but rather goimports.

I also don't see how goimports can make a decision about which is the correct import.

@ivoviz
Copy link
Author

ivoviz commented Nov 27, 2015

Yeah, I wasn't sure if it's a vscode-go thing or goimports. But still, imo it would be more logical to first try to import the package from the same app.

@newhook
Copy link
Contributor

newhook commented Nov 27, 2015

What do you mean "same app"?

@ivoviz
Copy link
Author

ivoviz commented Nov 27, 2015

I'll try to explain a bit more clearly: I have 2 db packages, one in app1 and one in app2. Simplified code - in reality they're not exactly the same, just similar, but both have the X variable with the same function:

github.com/ivoviz/app1/db/db.go
package db

var X *whatever

func (x *whatever) Test() {
...
}
github.com/ivoviz/app2/db/db.go
package db

var X *whatever

func (x *whatever) Test() {
...
}

Now if I write a code in

github.com/ivoviz/app2/main.go

like this:

package main

func main() {
    db.X.Test()
}

And then using "Format Code" will import the db package from app1 instead of app2. For me it would seem logical to try to find the package within app2 first, but I might be alone on this one. :)

@lukehoban
Copy link
Contributor

Thanks @ivoviz.

This does appear to be arguably a bug in goimports. I think you'll need to report this over at https://github.com/golang/go.

I see a few bugs over there that are still active that are likely related, including golang/go#9197.

I'm going to close the issue out here since we are relying on the existing Go tools for this so we have to accept their behaviour.

@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants