Skip to content

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

Closed
@hidu

Description

@hidu

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"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions