Skip to content

cmd/fix: go fix adds duplicate imports if dot-imports are used #78165

@thaJeztah

Description

@thaJeztah

Go version

go version go1.26.1 darwin/arm64

Output of go env in your module/workspace:

not relevant

What did you do?

When inlining (//go:fix inline) code, go fix doesn't account for dot-imports, which results in duplicate imports.

Admitted, dot-imports should generally be avoided, but I noticed this when I tried to simplify my examples for #78164, so thought I'd open a ticket (maybe it's a documentation change only 😅)

What did you see happen?

When inlining (//go:fix inline) code, go fix doesn't account for dot-imports, which results in duplicate imports, for example;

Before:

package main

import . "example.com/client"

func OK() {
	opts := []Opt{
		FromEnv,
		WithLegacyOpt("x"),
		WithLegacyOpt2(),
	}

	c := NewWithOpts(opts...)
	c.Create(CreateOpt{Cmd: StrSlice{"top"}})

	cmd := StrSlice{"top"}
	c.CreateLegacy(CreateOpt{Cmd: cmd})
}

After:

package main

import "example.com/client"

import . "example.com/client"

func OK() {
	opts := []Opt{
		FromEnv,
		client.WithOpt("x"),
		client.WithOpt2(),
	}

	c := client.New(opts...)
	c.Create(CreateOpt{Cmd: []string{"top"}})

	cmd := []string{"top"}
	c.Create(CreateOpt{Cmd: cmd})
}

In case useful, I also attached an example module that I was using for testing; example.zip

What did you expect to see?

I expected either a failure to be reported, or the duplicate import being detected and corrected;

package main

import . "example.com/client"

func OK() {
	opts := []Opt{
		FromEnv,
		WithOpt("x"),
		WithOpt2(),
	}

	c := New(opts...)
	c.Create(CreateOpt{Cmd: []string{"top"}})

	cmd := []string{"top"}
	c.Create(CreateOpt{Cmd: cmd})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions