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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@hidu
Copy link
Contributor

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 This label describes issues relating to any tools in the x/tools repository. label Dec 14, 2019
@agnivade
Copy link
Contributor

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
Contributor

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
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 14, 2019
@hidu
Copy link
Contributor Author

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
Contributor Author

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
@heschi
Copy link
Contributor

heschi commented Dec 16, 2019

Duplicate of #20818.

@heschi heschi closed this as completed Dec 16, 2019
@golang golang locked and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants