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

x/tools/cmd/goimports: bad sorting cannot be fixed completely #36140

Closed
hidu opened this issue Dec 14, 2019 · 5 comments
Closed

x/tools/cmd/goimports: bad sorting cannot be fixed completely #36140

hidu opened this issue Dec 14, 2019 · 5 comments

Comments

@hidu
Copy link

@hidu hidu commented Dec 14, 2019

What version of Go are you using (go version)?

$ go version
go1.13.4

Does this issue reproduce with the latest release?

yes

What did you do?

I have some code with wrong import group,
this is the demo code:

package main

import (
	"fmt"

	"golang.org/x/tools/go/ast/astutil"
	
	"abc.com/a/d/dao"
	"abc.com/a/d/model"
	"golang.org/x/tools/go/ast/inspector"
	"strings"
)


type aliasA  = astutil.ApplyFunc
var aliasB   = dao.Table
var aliasC   = model.Redis
var aliasD   = inspector.AST

func main(){
	fmt.Println("hello")

	fmt.Println(strings.Join([]string{"hello","golang"}," "))
}

What did you expect to see?

$ goimports -local abc.com/a/d demo.go
package main

import (
	"fmt"
	"strings" 

	"golang.org/x/tools/go/ast/astutil"
        "golang.org/x/tools/go/ast/inspector"

	"abc.com/a/d/dao"
	"abc.com/a/d/model"
)

type aliasA = astutil.ApplyFunc
var aliasB = dao.Table
var aliasC = model.Redis
var aliasD   = inspector.AST

func main() {
	fmt.Println("hello")
	fmt.Println(strings.Join([]string{"hello", "golang"}, " "))
}

What did you see instead?

the strings and golang.org/x/tools/go/ast/inspector are at the wrong group:

package main

import (
	"fmt"

	"golang.org/x/tools/go/ast/astutil"

	"strings"

	"golang.org/x/tools/go/ast/inspector"

	"abc.com/a/d/dao"
	"abc.com/a/d/model"
)

type aliasA = astutil.ApplyFunc
var aliasB = dao.Table
var aliasC = model.Redis
var aliasD   = inspector.AST

func main() {
	fmt.Println("hello")
	fmt.Println(strings.Join([]string{"hello", "golang"}, " "))
}

if the raw file's import section is like this, goimports can got the right code:

import (
	"fmt"
	"golang.org/x/tools/go/ast/astutil"
	"abc.com/a/d/dao"
	"abc.com/a/d/model"
	"golang.org/x/tools/go/ast/inspector"
	"strings"
)
@gopherbot gopherbot added this to the Unreleased milestone Dec 14, 2019
@gopherbot gopherbot added the Tools label Dec 14, 2019
@agnivade
Copy link
Contributor

@agnivade agnivade commented Dec 14, 2019

I think this is intended. If your import crosses a newline boundary, it is considered in a separate group. @bradfitz @heschik can confirm.

@dmitshur dmitshur changed the title x/tools/goimports:unexpected group x/tools/cmd/goimports: unexpected group Dec 14, 2019
@dmitshur
Copy link
Member

@dmitshur dmitshur commented Dec 14, 2019

From what I know, the GoLand IDE does not use goimports directly; it implements its own internal version. If that's the case, in order to change the behavior of GoLand, this issue should be reported to their issue tracker or equivalent.

@dmitshur dmitshur changed the title x/tools/cmd/goimports: unexpected group x/tools/cmd/goimports: GoLand adds stdlib imports into a new group rather than the existing top-most group Dec 14, 2019
@hidu
Copy link
Author

@hidu hidu commented Dec 15, 2019

For some reason,I have disabled the sorting imports function on the GoLand IDE。
I don't think we need to worry about whether we use ide or not。
We use this tool to fix the wrong group, don't we?

@hidu
Copy link
Author

@hidu hidu commented Dec 15, 2019

I have updated the issue,deleted description of IDE。
I just have some go files with wrong import group need fix。

@hidu hidu changed the title x/tools/cmd/goimports: GoLand adds stdlib imports into a new group rather than the existing top-most group x/tools/cmd/goimports: bad sorting cannot be fixed completely Dec 15, 2019
@heschik
Copy link
Contributor

@heschik heschik commented Dec 16, 2019

Duplicate of #20818.

@heschik heschik closed this Dec 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.