Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import removed when package name doesn't match directory name #153

Open
sman591 opened this issue Jul 13, 2023 · 0 comments
Open

Import removed when package name doesn't match directory name #153

sman591 opened this issue Jul 13, 2023 · 0 comments

Comments

@sman591
Copy link

sman591 commented Jul 13, 2023

Thanks for putting together this utility!

I believe I found a bug with how gosimports removes unused imports. If the package name doesn't match the name of the directory a package is imported from, gosimports will mistakenly think it is unused and remove it.

Example:

File being imported:

// project/foo/test.go
package foobar  // NOTE: package name is "foobar", but directory is "foo"

func Test() { ... }

Source file:

package server

import (
	"fmt"

	"github.com/mycompany/repo/foo/foo"
)

func main() {
	fmt.Printf("Hello world")
	foobar.Test()
}

gosimports will incorrectly remove this import:

package server

import (
	"fmt"
)

func main() {
	fmt.Printf("Hello world")
	foobar.Test()
}

However, if I manually name the import, gosimports will keep the import as desired.

package server

import (
	"fmt"

	foobar "github.com/mycompany/project/foo/foo"
)

func main() {
	fmt.Printf("Hello world")
	foobar.Test()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant