1
0
mirror of https://github.com/golang/go synced 2024-10-01 07:18:32 -06:00
Commit Graph

36 Commits

Author SHA1 Message Date
Keith Rarick
ed69e84b15 cmd/goimports, imports: add -local flag
This allows the caller to indicate they want certain
import paths to sort into another group after 3rd-party
imports when added by goimports. For example, running
'goimports -local example.com/' might produce

    import (
        "database/sql"
        "io"
        "strconv"

        "golang.org/x/net/context"

        "example.com/foo/bar"
        "example.com/foo/baz"
    )

Resolves golang/go#12420

Change-Id: If6d88599f6cca2f102313bce95ba6ac46ffec1fe
Reviewed-on: https://go-review.googlesource.com/25145
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-23 01:23:54 +00:00
Alex Brainman
682b241254 imports: make filepath.Rel work on windows
filepath.Rel will always fail if one parameter starts with
drive letter and the other does not. Make both filepath.Rel
parameters absolute paths, to give it chance to succeed.

Fixes broken tools build on windows.

Change-Id: Ibefcfe283f28977503323f01bc3a698478227f84
Reviewed-on: https://go-review.googlesource.com/25120
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-21 14:33:34 +00:00
Brad Fitzpatrick
d4a8e583a1 imports: skip "node_modules" directories
Updates golang/go#16417

Change-Id: Ia4a5f997036274d09cca2ff10e500e403c1725ba
Reviewed-on: https://go-review.googlesource.com/25044
Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-07-19 16:05:27 +00:00
Brad Fitzpatrick
edf8e6fef8 cmd/goimports, imports: optimize directory scanning and other things
This brings goimports from 160ms to 100ms on my laptop, and under 50ms
on my Linux machine.

Using cmd/trace, I noticed that filepath.Walk is inherently slow.
See https://golang.org/issue/16399 for details.

Instead, this CL introduces a new (private) filepath.Walk
implementation, optimized for speed and avoiding unnecessary work.

In addition to avoid an Lstat per file, it also reads directories
concurrently. The old goimports code did that too, but now that logic
is removed from goimports and the code is simplified.

This also adds some profiling command line flags to goimports that I
found useful.

Updates golang/go#16367 (goimports is slow)
Updates golang/go#16399 (filepath.Walk is slow)

Change-Id: I708d570cbaad3fa9ad75a12054f5a932ee159b84
Reviewed-on: https://go-review.googlesource.com/25001
Reviewed-by: Andrew Gerrand <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-19 03:21:45 +00:00
Brad Fitzpatrick
caebc7a51c imports: ignore case and hyphens when lexically filtering candidate dirs
This means a directory /gopath/src/foo/Go-Bar is considered as maybe
containing package "bar" or even "gobar".

Also, more tests.

Fixes golang/go#16402

Change-Id: I14208d738e3a081cb6d9bcd83d777280e118f4e7
Reviewed-on: https://go-review.googlesource.com/25030
Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-07-18 23:38:47 +00:00
Brad Fitzpatrick
29481a356d imports: skip test on plan9
Change-Id: I608a1ba47a049b7eb5f4ded481744d8b25e68800
Reviewed-on: https://go-review.googlesource.com/24988
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2016-07-17 16:31:09 +00:00
Josh Bleecher Snyder
6d32be89d5 imports: minor fixes
These are mostly comments for CL 24941 that didn't
get sent quickly enough.

strings.Trim(`\"`) works, but by accident.
It trims all leading and trailing "s and \s,
but there are never leading or trailing \s.

Semantic line breaks and punctuation cleanup.

The reflow of comments in the pkgName == "main" if
block is to silence this spurious vet failure:

fix.go:247: +build comment must appear before package clause and be followed by a blank line

Plain sync.Once values are enough.

Change-Id: I241f3025031b6f21605da78ea52066713a203327
Reviewed-on: https://go-review.googlesource.com/24983
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-07-16 22:16:59 +00:00
Brad Fitzpatrick
ffe4e61c64 imports: add configuration mechanism to exclude directories
Each $GOPATH entry may have a file $GOPATH/src/.goimportsignore which
may contain blank lines, #comment lines, or lines naming a directory
relative to the configuration file to ignore when scanning.  No
globbing or regex patterns are allowed.

Updates golang/go#16367 (goimports speed)
Fixes golang/go#16386 (add mechanism to ignore directories)

Change-Id: I8f1a88ae6c4d0ed3075444d70aec3e2228c5ce6a
Reviewed-on: https://go-review.googlesource.com/24971
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-15 19:48:34 +00:00
Brad Fitzpatrick
7c26c99973 imports: do less I/O finding the package name of an import
When goimports was run on a file like:

    package main

    import (
        "example.net/foo"
        "example.net/bar"
    )

    var _, _ = foo.Foo, bar.Bar

... even though there looks to be no work to do, it still needs to
verify that "example.net/foo" is really package "foo" (even though it
looks like it) and "example.net/bar" is really package
"bar". (Packages in the standard library are hard-coded since the
previous commit and not verified for consistency since they're always consistent)

To do that verification for non-std packages, go/build.Import was
being used before, but Import reads all files in the directory to make
sure they're consistent. That's unnecessary. Instead, stop after the
first file. If example.net/foo has foo.go with "package foo" and
just_kidding.go with "package other", we never read that far to find
the inconsistency. Oh well. Prefer speed.

Updates golang/go#16367

Change-Id: I9fc3fefbee0e8a6bc287bf2a565257fb9523fd5c
Reviewed-on: https://go-review.googlesource.com/24948
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2016-07-15 19:34:14 +00:00
Brad Fitzpatrick
e047ae774b cmd/goimports, imports: make goimports great again
I felt the burn of my laptop on my legs, spinning away while processing
goimports, and felt that it was time to make goimports great again.

Over the past few years goimports fell into a slow state of disrepair
with too many feature additions and no attention to the performance
death by a thousand cuts. This was particularly terrible on OS X with
its lackluster filesystem buffering.

This CL makes goimports stronger, together with various optimizations
and more visibility into what goimports is doing.

* adds more internal documentation

* avoids scanning $GOPATH for answers when running goimports on a file
  under $GOROOT (for Go core hackers)

* don't read all $GOROOT & $GOPATH directories' Go code looking for
  their package names until much later. Require the package name of
  missing imports to be present in the last two directory path
  components.  Then only try importing them in order from best to
  worst (shortest to longest, as before), so we can stop early.

* when adding imports, add names to imports when the imported package name
  doesn't match the baes of its import path. For example:
        import foo "example.net/foo/v1"

* don't read all *.go files in a package directory once the first file
  in a directory has revealed itself to be a package we're not looking
  for. For example, if we're looking for the right "client" for "client.Foo",
  we used to consider a directory "bar/client" as a candidate and read
  all 50 of its *.go files instead of stopping after its first *.go
  file had a "package main" line.

* add some fast paths to remove allocations

* add some fast paths to remove disk I/O when looking up the base
  package name of a standard library import (of existing imports in a
  file, which are very common)

* adds a special case for import "C", to avoid some disk I/O.

* add a -verbose flag to goimports for debugging

On my Mac laptop with a huge $GOPATH, with a test file like:

	package foo
	import (
	       "fmt"
	       "net/http"
	)

	/*

	*/
	import "C"

	var _ = cloudbilling.New
	var _ = http.NewRequest
	var _ = client.New

... this took like 10 seconds before, and now 1.3 seconds. (Still
slow; disk-based caching can come later)

Updates golang/go#16367 (goimports is slow)
Updates golang/go#16384 (refactor TestRename is broken on Windows)

Change-Id: I97e85d3016afc9f2ad5501f97babad30c7989183
Reviewed-on: https://go-review.googlesource.com/24941
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-15 05:29:07 +00:00
Brad Fitzpatrick
0835c73534 imports: special case rand.Read, prevent math/rand by chance
In Go 1.7, math/rand.Read was added. Previously, the only package
containing "rand.Read" was "crypto/rand".

goimports was updated to know that, and zstdlib.go contains a note
that it's ambiguous:

        "rand.Perm":        "math/rand",
        "rand.Prime":       "crypto/rand",
        "rand.Rand":        "math/rand",
        // "rand.Read" is ambiguous
        "rand.Reader":                   "crypto/rand",
        "rand.Seed":                     "math/rand",
        "rand.Source":                   "math/rand",

The intention originally was that such ambiguous things would
never be resolved, even randomly.

But a later change added support for build.Default.SrcDirs, which
meant GOROOT was also searched for ambiguous things. Or maybe I forget
the history.

In any case, when goimports tried to resolve "rand.Read", the
findImportStdlib check was returning nothing, which lead to the
$GOROOT being searched, where math/rand was picked by chance. That's a
dangerous default when the intentional might've been crypto/rand.

Special case it and prefer crypto/rand if there's no more specific
clue either way.

Change-Id: Ib5f8f297f72fa309d5ca9b15a37493df2e17567c
Reviewed-on: https://go-review.googlesource.com/24847
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-07-14 05:39:27 +00:00
Filippo Valsorda
95963e031d imports: support symlinks in GOPATH
The go tool will import a package with a import path that traverses
symlinks with no problems, but goimports would remove that import
because it would fail to recognize the package as existent.

Fixes golang/go#14845

Note: if the file you are currently processing also has a name inside
the symlink, you might have to use the "long" path for vendoring to
work, as it wouldn't be recognized as "deeper" than the vendor folder
otherwise. For example in this tree:

.
├── myfile.go
└── x
    ├── vendor
    │   └── mypkg
    │       └── foo.go
    └── y -> ..

If myfile.go imports mypkg, you will have to process it as
./x/y/myfile.go, not ./myfile.go.

Change-Id: Ic8f41baed3f28d4e9b813160d91aef09ece1fc9f
Reviewed-on: https://go-review.googlesource.com/23803
Reviewed-by: Andrew Gerrand <adg@golang.org>
Run-TryBot: Andrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-06-09 03:45:06 +00:00
Cezar Sa Espinola
c86fe5956d imports: try importing pkg path from vendor when looking for pkg name
Since CL 17728 goimports was using the file path to try finding packages
inside its /vendor directory. This CL builds upon this previous work to
also consider packages inside /vendor when converting an import path to
a package name.

Change-Id: I173472ac51e3f681d758ec52add60ffc04c62c1a
Reviewed-on: https://go-review.googlesource.com/22020
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2016-05-10 17:11:45 +00:00
Brad Fitzpatrick
02e8ee6893 imports: add Options.FormatOnly
Fixes golang/go#14500

Change-Id: Ied9d772e5f606ce6716193faa2c1a285f0ab00b9
Reviewed-on: https://go-review.googlesource.com/21532
Reviewed-by: Robert Griesemer <gri@golang.org>
2016-04-05 16:34:54 +00:00
Robert Griesemer
5a2fc32f44 imports: adjust vendored packages test to keep working against 1.6
The prior commit fixed this test for tip (1.7) but broke it for 1.6.

Change-Id: Ic39d215ac8bc60dba4b66201f1f16713b2e02d9c
Reviewed-on: https://go-review.googlesource.com/21144
Reviewed-by: Alan Donovan <adonovan@google.com>
2016-03-25 17:41:17 +00:00
Robert Griesemer
35f54f0ea0 imports: adjust tests to check vendored packages again (fix build)
Change-Id: I616dccc07c255bb7ca1146ad0d37d05999eeb772
Reviewed-on: https://go-review.googlesource.com/21142
Reviewed-by: Alan Donovan <adonovan@google.com>
2016-03-25 17:11:48 +00:00
Ahmy Yulrizka
6e94e8a89f imports: cleanup test due to assignment copies of sync.Once
go vet detected that the value of pkgIndexOnce
is copied through assignments in the test.
This patch fixes that by converting it to *sync.Once instead.

Change-Id: I90b2252942625b2f8a93eb5da24d16c6a8a56e4e
Reviewed-on: https://go-review.googlesource.com/20910
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2016-03-20 22:53:05 +00:00
Robert Griesemer
95e5e62424 imports: fix TestFindImportVendor for 1.5 (fix build)
TBR=adonovan

Change-Id: I7edeb180bdf09648e7539b9371db0d14272b9030
Reviewed-on: https://go-review.googlesource.com/20697
Reviewed-by: Robert Griesemer <gri@golang.org>
2016-03-15 04:38:51 +00:00
Robert Griesemer
8852e49d27 imports: adjust tests to fix build
These changes will need to be reverted once we use
vendoring in the stdlib again, but it's trivial to
do so when the time comes.

TBR=adonovan

Fixes #14823.

Change-Id: I2173c48d5466874492affc679332f6484bf96592
Reviewed-on: https://go-review.googlesource.com/20695
Reviewed-by: Robert Griesemer <gri@golang.org>
2016-03-15 04:38:37 +00:00
David R. Jenni
e852fdd89f go/ast/astutil: add function DeleteNamedImport.
DeleteNamedImport deletes the import with a given
name and path from a parsed file, if present.

imports uses this function to delete named imports.

Fixes golang/go#8149.

Change-Id: I84539d5627191c45f06db2d008507aee4d3b1eb2
Reviewed-on: https://go-review.googlesource.com/19951
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-02-26 17:37:10 +00:00
Russ Cox
bf084ef758 imports: add support for vendor directories
Editor modes that invoke the goimports command on temporary copies
of actual source files will need to invoke goimports -srcdir now to say
where the real source directory is. Otherwise goimports will not consider
vendored or internal packages when looking for new imports.

In lieu of a test for cmd/goimports (because it has no tests),
a command transcript:

	$ cd /tmp
	$ cat x.go
	package p
	var _ = hpack.HuffmanDecode
	$

	$ GOPATH= goimports < x.go
	package p

	var _ = hpack.HuffmanDecode
	$ GOPATH= goimports x.go
	package p

	var _ = hpack.HuffmanDecode
	$

But with the new flag:

	$ GOPATH= goimports -srcdir $GOROOT/src/math < x.go
	package p

	import "golang.org/x/net/http2/hpack"

	var _ = hpack.HuffmanDecode
	$ GOPATH= goimports -srcdir $GOROOT/src/math x.go
	package p

	import "golang.org/x/net/http2/hpack"

	var _ = hpack.HuffmanDecode
	$

The tests in this CL and the above transcript assume that
$GOROOT/src/vendor/golang.org/x/net/http2/hpack exists.
It did in 40a26c9, but it does not today.
It will again soon (once Go 1.7 opens).

For golang/go#12278 (original request).

Change-Id: I27b136041f54edcde4bf474215b48ebb0417f34d
Reviewed-on: https://go-review.googlesource.com/17728
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-02-18 16:47:46 +00:00
David R. Jenni
4f50f44d7a imports: fix bug, where unused named import is mistaken for unnamed import.
Fixes #8149.

Change-Id: Ia3d318f70981b2032a71d3fd32eaffba20cfbcbd
Reviewed-on: https://go-review.googlesource.com/13371
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2015-08-12 17:18:37 +00:00
Josh Bleecher Snyder
57d2ff39c7 go/ast/astutil: match prefix segments when adding imports
AddImport and AddNamedImport attempt to place new
imports in roughly the correct place--and thus the
correct group--by matching prefixes. Matching prefixes
byte-by-byte led to "regexp" being grouped with "rsc.io/p".
Instead, match prefixes by segments.

Fixes golang/go#9961.

Change-Id: I52b7c58a9a2fbe85c2b5297e50c87d409364bda3
Reviewed-on: https://go-review.googlesource.com/8090
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-03-30 17:57:25 +00:00
David Symonds
0513cb08b6 go.tools/imports: Permit fix_test.go to work pre-Go 1.4.
LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/156980043
2014-10-13 08:44:37 +11:00
Alan Donovan
159879b336 go.tools/imports: src/pkg -> src
LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/141260043
2014-09-08 14:20:12 -04:00
Josh Bleecher Snyder
cc1e254c18 go.tools/astutil: delete all matching imports in DeleteImport
Fixes golang/go#8459.

LGTM=crawshaw, bradfitz
R=bradfitz, crawshaw
CC=golang-codereviews
https://golang.org/cl/128220043
2014-08-14 11:51:51 -07:00
Josh Bleecher Snyder
3442faf3c1 go.tools/imports: go/format outbound code
Fixes golang/go#8035.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews, shurcool
https://golang.org/cl/120840044
2014-07-28 17:15:17 -07:00
Dmitri Shuralyov
1154a04eb0 go.tools/imports: fix case where output not gofmt-compatible
Fixes 7866. Adds a test case.

When trying to add newlines before certain imports via text manipulation,
a regex is used to iterate over all imports. The regex failed to match
dot imports because \w doesn't match a literal dot. This changes the regex
to accept a dot as well.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/99400043
2014-05-20 14:02:16 -07:00
Dmitri Shuralyov
c0060eca2c go.tools/astutil: fix edge case in DeleteImport causing merging of import sections.
The issue occurs only when deleting an import that has a blank line immediately preceding,
and other imports before that.

Currently, DeleteImport assumes there's a blank line-sized hole left behind
where the import was, and always deletes it. That blank line-sized hole is there in all cases
except the above edge case.

This fix checks for that edge case, and does not remove the blank line-sized hole.

The CL also adds a previously failing test case that catches this scenario. After the change to
DeleteImport, the new test passes (along with all other tests).

Fixes golang/go#7679.

Note that there is no attempt to ensure the result *ast.File and *token.FileSet are perfectly
matching to what you would get if you printed the AST and parsed it back. This is how the
rest of the package and the current tests work (i.e., they only check that printing the AST gives
the correct output).
Changing that is very hard, if not impossible, at least not
without resorting to manipulating AST via printing, text manipulation and parsing.
This is okay for most usages, but it does create potential problems. For example,
astutil.Imports() currently only works correctly on freshly parsed AST. If that AST
is manipulated via astutil funcs, then Imports() may not always generate correct
output. However, thas is a separate issue and should be treated as such.

LGTM=bradfitz
R=golang-codereviews, gobot, adonovan, bradfitz
CC=golang-codereviews
https://golang.org/cl/92250045
2014-05-19 14:04:30 -07:00
Stephen Weinberg
55d5722095 imports, cmd/goimports: add missing package main if func main exists
Assuming:
        1) package declaration does not exist
        2) the Fragment option is set
        3) a main function exists

We will assume it is a main package and add the declaration.

This change also sets the Fragment option in goimports.

LGTM=crawshaw, bradfitz
R=bradfitz, crawshaw
CC=golang-codereviews
https://golang.org/cl/96850044
2014-04-28 17:15:33 -04:00
Sameer Ajmani
a1c1cf19ba imports: extend findImports to return a boolean, rename, that tells
goimports to use the package name as a local qualifier in an import.
For example, if findImports("pkg", "X") returns ("foo/bar",
rename=true), then goimports adds the import line:
  import pkg "foo/bar"
to satisfy uses of pkg.X in the file.

This change doesn't add any implementations of rename=true, though one
is sketched in a TODO.

LGTM=crawshaw
R=crawshaw, rsc
CC=bradfitz, golang-codereviews
https://golang.org/cl/76400050
2014-03-25 09:37:10 -04:00
Brad Fitzpatrick
a0c2140b91 imports: fix a case where we weren't gofmt-compatible
Because goimports also sorts stdlib-vs-external differently
(whereas gofmt just sorts string-wise), we need to put a blank
line between imports of different classes. This happened in
some cases, but not all.

Fixes golang/go#7132

LGTM=kamil.kisiel
R=golang-codereviews, kamil.kisiel
CC=golang-codereviews
https://golang.org/cl/60870047
2014-02-07 17:03:34 -08:00
Brad Fitzpatrick
eda00ba4ec imports: bake in the standard library again
Fast path for the common case. Avoids scanning GOPATH usually.

LGTM=r, crawshaw
R=david.crawshaw, adg, r, crawshaw
CC=golang-codereviews
https://golang.org/cl/56820043
2014-01-26 09:47:31 -08:00
Brad Fitzpatrick
b6e674b8e7 go.tools/imports: fix fileset mismatch bug
Fixes golang/go#6884

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/43890043
2013-12-18 09:09:37 -08:00
David Crawshaw
64c6d0410b go.tools/imports: fix test broken in b33d2e25015b6793aac4f9dcef3d8a8d1c9243b3
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/41950048
2013-12-18 03:44:50 -08:00
David Crawshaw
c87866116c go.tools/imports: move goimports from github to go.tools.
From revision d0880223588919729793727c9d65f202a73cda77.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/35850048
2013-12-17 21:21:03 -05:00