Skip to content

x/tools/cmd/goimports: Not honoring existing import blocks #46401

Closed
@chris-annino-sp

Description

@chris-annino-sp

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

$ go version
go version go1.16.3 darwin/amd64

Does this issue reproduce with the latest release?

Unsure

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/chris.annino/Library/Caches/go-build"
GOENV="/Users/chris.annino/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/chris.annino/go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/chris.annino/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/n7/b590_5_x2fq9l3wvs9v_t_bhmxcn41/T/go-build3430492942=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

import (
  "github.com/local"
  "github.com/external"
  "fmt"
)

then run:

goimports -local "github.com/local" -w file.go

works wonderfully:

import (
  "fmt"

  "github.com/external"

  "github.com/local"
)

However, my IDE is dumb and doesn't honor import blocks, instead inserting to the first block. I am expecting goimports to help with this and honor existing blocks of system, external and local blocks.

import (
  "fmt"
  "github.com/local/subpackage"

  "github.com/external"

  "github.com/local"
)
goimports -local "github.com/local" -w file.go

What did you expect to see?

import (
  "fmt"
 
  "github.com/external"

  "github.com/local"
  "github.com/local/subpackage"
)

What did you see instead?

import (
  "fmt"

  "github.com/local/subpackage"
 
  "github.com/external"

  "github.com/local"
)

This behavior seems to happen whether an external import or local import is included in the first block. In fact, adding a local import to the external block we see similar behavior where instead of adding to the existing local block, it creates a new local block above the existing local block. Is this a bug or by design? I wish it worked differently if this is by design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis 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