Skip to content

x/tools/cmd/goimports: Unexpected goimports behaviour depending on prior imports #29180

@rileykarson

Description

@rileykarson

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

go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

darwin (MacOs), amd64

What did you do?

I work on a code generator that generates a Terraform provider for Google Cloud Platform; we generate code without imports and use goimports to add them.

I updated my local copy of goimports today, and ran into some very unexpected changes. There was a large diff, but most importantly goimports isn't generating correct imports for at least 1 file anymore.

These examples aren't runnable; they're variants of this file, the correct output: https://github.com/terraform-providers/terraform-provider-google/blob/2.0.0/google/resource_compute_subnetwork.go

Given this file: https://play.golang.org/p/Ku1KKO3urCd

when I run goimports -v -w google/resource_compute_subnetwork.go

What did you expect to see?

I expect to see https://github.com/terraform-providers/terraform-provider-google/blob/2.0.0/google/resource_compute_subnetwork.go

If I run goimports in a separate directory with no other files, I see the correct import path. When I run it on an unformatted copy of the file in the provider repo, I get an incorrect output with the bad import path.

What did you see instead?

-compute "google.golang.org/api/compute/v1"
+computeBeta "google.golang.org/api/compute/v0.beta"

Full file: https://play.golang.org/p/wKgEyMWdZto

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions