Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: cmd/gofmt: organize renamed imports #26846

Closed
jjuliano opened this issue Aug 7, 2018 · 3 comments
Closed

proposal: cmd/gofmt: organize renamed imports #26846

jjuliano opened this issue Aug 7, 2018 · 3 comments

Comments

@jjuliano
Copy link

jjuliano commented Aug 7, 2018

Currently gofmt will sort all imports alphabetically, and if you have aliased imports on your imports, this would result to a mixed untidy import.

For example, the following import will be sorted alphabetically

 import(
    "foo"
    "bar"
    "baz"
        
    "example.com/quux"
    "example.com/garply"
    "example.com/plugh"
    "example.com/xyzzy"
    "example.com/thud"
    corge "example.com/corge"
    grault "example.com/grault"
    waldo "example.com/waldo"
    fred "example.com/fred"
)

which would result to an untidy mix

 import(
    "bar"
    "baz"
    "foo"
        
    corge "example.com/corge"
    fred "example.com/fred"
    "example.com/garply"
    grault "example.com/grault"
    "example.com/plugh"
    "example.com/quux"
    "example.com/thud"
    waldo "example.com/waldo"
    "example.com/xyzzy"
)

I proposed that aliased imports to be organized to it's own block, for example

 import(
    "bar"
    "baz"
    "foo"
        
    "example.com/garply"
    "example.com/plugh"
    "example.com/quux"
    "example.com/thud"
    "example.com/xyzzy"

    corge "example.com/corge"
    fred "example.com/fred"
    grault "example.com/grault"
    waldo "example.com/waldo"
)

So it would be much cleaner and easily readable.

@gopherbot gopherbot added this to the Proposal milestone Aug 7, 2018
@agnivade
Copy link
Contributor

agnivade commented Aug 7, 2018

/cc @griesemer

@mvdan
Copy link
Member

mvdan commented Aug 7, 2018

This seems like a job for goimports, since gofmt has never split import groups yet goimports does so by design.

@rsc
Copy link
Contributor

rsc commented Aug 13, 2018

If you have a bunch of related renamings then you may want to see them all together. If you have just a few in a large list of imports, the benefit of sorting them with the normal imports is that when you look for a particular import you only need to consider one list. For example in cmd/go we have variables named 'path' quite a bit, so conventionally the import of "path" comes in as pathpkg. Here's one real import block:

import (
	"bytes"
	"fmt"
	"go/build"
	"go/token"
	"io/ioutil"
	"os"
	pathpkg "path"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"unicode"
	"unicode/utf8"

	"cmd/go/internal/base"
	"cmd/go/internal/cfg"
	"cmd/go/internal/modinfo"
	"cmd/go/internal/search"
	"cmd/go/internal/str"
)

I think it would be inappropriate for gofmt or goimports to force moving the path import into its own section.

We intentionally leave this to local custom - gofmt and goimports will never move imports across blank lines in the import block. So if I want path with the others, I can do that, and gofmt/goimports respect that. And if you want all your renamed imports together, you can do that, and gofmt/goimports respect that too.

There's not a compelling reason to force everyone onto one side or the other.

@rsc rsc closed this as completed Aug 13, 2018
@rsc rsc changed the title proposal: cmd/gofmt: organize aliased imports proposal: cmd/gofmt: organize renamed imports Aug 13, 2018
@golang golang locked and limited conversation to collaborators Aug 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants