Skip to content

x/tools/imports: Process does not remove all unused imports sharing a package name #45398

@Deiz

Description

@Deiz

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

$ go version
go version go1.16 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

See https://play.golang.org/p/yv78HSm0AX6 for a full example, and https://play.golang.org/p/GmUF-tayOgI for an interactive example (hit "Format" with imports enabled).

What did you expect to see?

package main

import (
	"errors"

	"github.com/pkg/errors"
	"github.com/juju/errors"
)

func main() {
}

This file has three imported errors packages, none of them are referenced outside of the import block, so they should all be removed, resulting in this output:

package main

func main() {
}

What did you see instead?

Only one errors package is removed per iteration - seemingly always the last one to appear. Because we have three imported packages, we need to run imports.Process three times to prune all the unused imports.

Iteration 1:

package main

import (
    "errors"

    "github.com/pkg/errors"
)

func main() {
}

Iteration 2:

package main

import (
    "errors"
)

func main() {
}

Iteration 3:

package main

func main() {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.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