1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:26:11 -07:00
Commit Graph

136 Commits

Author SHA1 Message Date
Ian Cottrell
c1547a3f90 imports: Limit the amount of text printed in the large token case
If the test fails, it tries to print a million characters.
This limits it to just the start of got and want, which is enough to see
the imports block anyway.

Change-Id: I2c58db8e96e73da436ca16fa8a57c820a95242ca
Reviewed-on: https://go-review.googlesource.com/100216
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-03-13 18:49:02 +00:00
Nishanth Shanmugham
059bec968c cmd/goimports: support multiple comma-separated imports in -local flag
In cmd/goimports, allow for the -local flag to accept a comma-separated
list of import path prefixes. Also, update the imports package
accordingly to support this.

Fixes golang/go#19188

Change-Id: I083d584df8c3a77532f0f66e9c5d970960180e0d
Reviewed-on: https://go-review.googlesource.com/85397
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-03-05 19:26:41 +00:00
Yury Smolsky
73e16cff9e imports: show detailed error when directory cannot be opened
Fixes golang/go#24154

Change-Id: I2834cc2578e911499cbce5412fe6a9041248794d
Reviewed-on: https://go-review.googlesource.com/97359
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-02-27 18:22:17 +00:00
Antonio Antelo
6c27c68f27 imports: fix mangled comments after package clause insertion
Fixes golang/go#12097

Change-Id: Ie6a6aa997e89700e49d703b7fd00f515b03ad6f8
Reviewed-on: https://go-review.googlesource.com/93235
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-02-14 03:58:58 +00:00
Daniel Martí
90b807ada4 all: fix a few issues found by unparam
In cmd/present, a mode was being passed to the function parse, but it
wasn't actually being used. Use it.

In go/ssa, checkFinalInstr received an idx integer but it doesn't
actually need it. Get rid of it.

Lastly, in imports, findImportStdlib always returned rename==false. Get
rid of that result parameter.

Change-Id: I719006b69ee80a3ef4b0ea24c1c206016a7e304b
Reviewed-on: https://go-review.googlesource.com/93596
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-02-13 22:52:51 +00:00
Nikhil Benesch
c0251d31d2 imports: handle arbitrarily long lines
Previous work to resolve golang/go#18201 increased the maximum line
length that goimports could handle to 1MiB (CL83800), but generated code
can result in Go files with longer lines. Use a bufio.Reader instead of
a bufio.Scanner to support arbitrarily long lines, as permitted by the
Go spec.

Change-Id: If719e531859804304d60a8c00db6304ab3d5fe5e
Reviewed-on: https://go-review.googlesource.com/93439
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-02-13 22:26:52 +00:00
Josh Bleecher Snyder
95b47aa5df imports: update zstdlib to Go 1.10
Change-Id: I36e7a5638de2cdcd3523bb3c6c89399586aab938
Reviewed-on: https://go-review.googlesource.com/93195
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-02-10 03:09:51 +00:00
Michael Fraenkel
8cab8a1319 imports: sibling imports must have matching references
When selecting a sibling's import, the unresolved reference must have
been also used otherwise use the normal search to determine the best
possible package to import.

Fixes golang/go#23001

Change-Id: I38a983569991464970ad5921fe7f280dd3e35a2c
Reviewed-on: https://go-review.googlesource.com/82875
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2018-01-05 20:15:57 +00:00
Steve Gilbert
96b5a5404f imports: support files with longer lines
This change set fixes the issue by specifying a max buffer size of ~1 megabyte for Scanner.
Previously the max was not set resulting in the default max which is ~64k.  This increased max should not increase
memory use for smaller, normal files because it is a max and the code expands the buffer as needed for large tokens.

The change set includes an additional change to return an error from the Scanner which was ignored previously.

Fixes golang/go#18201

Change-Id: I11be39af74d5eb3b353ad81ba1cb5404207aa65d
Reviewed-on: https://go-review.googlesource.com/83800
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-01-03 17:47:52 +00:00
Michael Fraenkel
9c57063f67 imports: prioritize closer packages
Prefer imports that are closer to the current package.

Fixes golang/go#17557

Change-Id: Iec55a294d396feac6234be307e08608b8559f65c
Reviewed-on: https://go-review.googlesource.com/37070
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-11-13 21:23:40 +00:00
Andrew Bonventre
3b1faeda9a imports: fix +build tags to exclude syscall deps on appengine
The following tag

// +build linux,!appengine darwin

Translated to (linux AND !appengine) OR (darwin)

Causing issues on darwin when attempting to run dev_appserver.

Change this to always exclude these files from appengine builds.

Change-Id: Ifd63a884747001797d0b0e828f0c9c391bc7c73c
Reviewed-on: https://go-review.googlesource.com/60911
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Chris Broadfoot <cbro@golang.org>
2017-09-01 21:04:08 +00:00
haya14busa
84a35ef54d go/ast/astutil: new third-party imports shouldn't go in the std group
Before this change, astutil would only do a prefix match of a new import
with all the existing ones, to try to place it in the correct group. If
none was found, the new import would be placed at the beginning of the
first import group.

This works well for new std imports, but it doesn't work well for new
third-party packages that don't share any prefix with any of the
existing imports.

Example:

	import (
		"time"

		"github.com/golang/snappy"
	)

When adding "golang.org/x/sys/unix" with astutil.AddImport, the import
is inserted as follows:

	import (
		"golang.org/x/sys/unix"
		"time"

		"github.com/golang/snappy"
	)

And goimports reorganizes the imports to separate std and third-party
packages:

	import (
		"time"

		"golang.org/x/sys/unix"

		"github.com/golang/snappy"
	)

We usually don't want to introduce a new import group; in most cases,
the desired behavior is separating std from third-party packages.

With this CL, new imports that don't share prefix with any existing ones
will be placed with the first group of third-party imports, if any
exist. If no third-party import group exists, a new one will be added.
In the case of our example above, this will be the new outcome:

	import (
		"time"

		"github.com/golang/snappy"
		"golang.org/x/sys/unix"
	)

Fixes golang/go#19190.

Change-Id: Id4630015c029bd815234a6c8726cb97f4af16f1c
Reviewed-on: https://go-review.googlesource.com/37552
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2017-08-15 00:39:00 +00:00
Axel Wagner
0f5d61c4c1 imports: print dir of candidates in addition to import path
The import path is ambiguous in the presence of vendoring (e.g. golang/go#20610)

Change-Id: I22f372b233b8554e3d9210b383a7df7a6a0f3eee
Reviewed-on: https://go-review.googlesource.com/53470
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-08-06 16:51:46 +00:00
Josh Bleecher Snyder
478150226d imports: update zstdlib to Go 1.9
We're at beta 2 already, so the API is likely stable.
If anything gets rolled back, we can update imports again.

Change-Id: I87e0adde062e45cab16e2bee2f929c1ac1bb828d
Reviewed-on: https://go-review.googlesource.com/49150
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-07-17 03:03:41 +00:00
Brad Fitzpatrick
6e57528ade imports: fix reading from stdin on Windows
Don't panic when reading from stdin on Windows. This is a regression
from https://golang.org/cl/43454

Also fix some weird behavior with stdin processing I noticed during
reviewing the code: don't allow the -w (write) flag, and adust the
filename shown with the -d (diff) flag.

Fixes golang/go#20941

Change-Id: I73d0a1dc74c919238a3bb72823585bbf1b7daba1
Reviewed-on: https://go-review.googlesource.com/47810
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Jones <rbjones@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-07-07 17:20:05 +00:00
Koichi Shiraishi
e6cb469339 imports: fix LocalPrefix document prefix
golint suggested:
  comment on exported var LocalPrefix should be of the form "LocalPrefix ..."

Change-Id: I1d2f92ba76e3c091440f676936dbb1fc1ce07f3a
Reviewed-on: https://go-review.googlesource.com/46432
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-06-23 12:26:59 +00:00
Brad Jones
e011c1062a imports: prefer paths imported by sibling files.
Adds an Imports field to packageInfo with the imports used by sibling
files, and uses it preferentially if it matches a missing import.

Example: if foo/foo.go imports "local/log", it's a reasonable assumption
that foo/bar.go will also want "local/log" instead of "log".

Change-Id: Ifb504ed5e00ff18459f19d8598cc2c94099ae563
Reviewed-on: https://go-review.googlesource.com/43454
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-06-09 17:18:19 +00:00
Brad Fitzpatrick
81478017b6 imports: wait for fastWalk workers to finish before returning (take 2)
This is Joël Stemmer's https://golang.org/cl/40092 again, but with
a fix to prevent workers from deadlocking on send if the caller had
already started to shut down. See:

https://github.com/golang/go/issues/16399#issuecomment-293278556

Updates golang/go#16399
Fixes golang/go#20109 (it looks like)

Change-Id: I3d1cf6f24563d02e1369a4496c2d37dcc1f5e5b8
Reviewed-on: https://go-review.googlesource.com/41681
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joël Stemmer <jstemmer@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-04-25 17:30:35 +00:00
Brad Jones
663269851c imports: allow dirPackageInfo to be overridden
Change-Id: Ie90d2681e364e2a3f905675db266cb8960e08277
Reviewed-on: https://go-review.googlesource.com/39995
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-04-16 02:23:49 +00:00
Brad Fitzpatrick
7ee420f17d Revert "imports: wait for fastWalk workers to finish before returning"
This reverts commit 4436e54754.

Reason for revert: Breaks goimports. See:
https://github.com/golang/go/issues/16399#issuecomment-293248363

Change-Id: I3bda8f0fd32380d19d7daecf3489a24e51abfbe7
Reviewed-on: https://go-review.googlesource.com/40296
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-04-11 14:41:52 +00:00
Joël Stemmer
4436e54754 imports: wait for fastWalk workers to finish before returning
In some cases walkFn is being called after the fastWalk function has
returned. This often happens when an error was encountered early on in
scanning directories with many entries.

It is caused by fastWalk not waiting for its workers to complete their
work. A sync.WaitGroup is used to wait for all workers to finish when
the function returns.

Updates golang/go#16399

Change-Id: I695d30c18e4878b789520b9d8a650f9688d896ac
Reviewed-on: https://go-review.googlesource.com/40092
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-04-09 19:02:35 +00:00
Kevin Burke
620ecdb8d7 all: apply new machine-generated doc spec to files
Updates golang/go#13560.

Change-Id: I1664807a065c7982a57d4dc9cee22ce261536c5b
Reviewed-on: https://go-review.googlesource.com/39202
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-04-01 01:24:29 +00:00
haya14busa
1f0f7f68c9 imports: return when done channel is closed
Change-Id: I49317a947ec58572fd16078279678d4514cc2a4b
Reviewed-on: https://go-review.googlesource.com/37919
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-03-27 15:17:22 +00:00
Brad Fitzpatrick
f60b69ed8c imports: fix symlink loop detection, deflaking TestImportSymlinks
Change the shouldTraverse function to no longer keep a global map of
which inodes it's seen. Instead, whenever a symlink is seen for a path
name, check every directory entry in that path name and see if any are
the same inode as the current one, detecting any loop just from the
name itself.

More details of why the test was flaky are in the bug.

Fixes golang/go#18142

Change-Id: I869f7a13d130c63d78b7af81802a16c4b4b2f3bd
Reviewed-on: https://go-review.googlesource.com/37947
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-03-08 23:27:09 +00:00
David R. Jenni
1dce95f761 x/tools/imports: remove unused type gate.
Found with honnef.co/go/tools/cmd/unused.

Change-Id: I3e40d58044c250bfeafc77e5eef4533390ace12b
Reviewed-on: https://go-review.googlesource.com/37607
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-01 15:49:35 +00:00
Chris Broadfoot
f7f4b0b08a imports: update zstdlib to Go 1.8
Change-Id: I22d9e395bb901ba0c2d949a464228b46911c9405
Reviewed-on: https://go-review.googlesource.com/37139
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-02-16 21:22:49 +00:00
haya14busa
8524ce5143 imports, go/ast/astutil: do not make grouped imports non-grouped when removing
import (
		"fmt"
		"strings"
	)

When deleting "fmt" import statement, code should be converted to the
following code.

	import (
		"strings"
	)

Instead of

	import "strings"

Diff becomes nicer by this change and it avoids that rewriting grouped
imports non-grouped may result in confusion comments.

Example:

	// comment 1
	import (
		"fmt"
	        // comment 2
		"strings"
	)

should be

	// comment 1
	import (
	        // comment 2
		"strings"
	)

instead of

	// comment 1
        // comment 2
	import "strings"

Fixes golang/go#18051

Change-Id: I3c07b70b657191eacf83c3197a965e587286c950
Reviewed-on: https://go-review.googlesource.com/36853
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2017-02-13 18:56:44 +00:00
haya14busa
13507735c4 imports: fix formatting directive error found by go vet
Change-Id: Ib702ecf928b84bfc18ec250adc1d3670c66cfead
Reviewed-on: https://go-review.googlesource.com/36852
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-02-12 17:59:58 +00:00
Ludi Rehak
721f218496 imports: remove unnecessary string conversion
This change remove an unnecessary string conversion of
a variable that is already a string.

Change-Id: I64d9a6c059276fa22f1be9f2dba02bbeb63cb8fb
Reviewed-on: https://go-review.googlesource.com/35560
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-01-22 22:46:49 +00:00
Chris Stockton
4c6345e8dc imports: respect ignoredDirs for symlinks
Fixes golang/go#17916

Added tests.

Change-Id: Ie44e4bcbec267b6c16249336c5d48bae86acc2b5
Reviewed-on: https://go-review.googlesource.com/33246
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-11-23 02:24:47 +00:00
Alan Donovan
d268a2ba76 imports: revert support for Go 1.8 aliases
Change-Id: I8f7c0ff2c544fa51686b2e178ba82a486398651c
Reviewed-on: https://go-review.googlesource.com/32834
Reviewed-by: Robert Griesemer <gri@golang.org>
2016-11-04 21:17:16 +00:00
Alan Donovan
4549178751 x/tools: clone some files in preparation for alias changes
This CL only copies files and updates build tags.
Substantive changes will come in follow-ups.
This is a workaround for git's lack of rename/copy tracking.

Tested with go1.6, go1.7, and tip (go1.8).

Change-Id: Id88a05273fb963586b228d5e5dfacab32133a960
Reviewed-on: https://go-review.googlesource.com/32630
Reviewed-by: Robert Griesemer <gri@golang.org>
2016-11-02 16:57:37 +00:00
Brad Fitzpatrick
3fe2afc9e6 imports: update zstdlib to Go 1.7
Change-Id: I936c6f03e8c3dc14832c8493937fdb64e6d1a853
Reviewed-on: https://go-review.googlesource.com/27079
Reviewed-by: Chris Broadfoot <cbro@golang.org>
2016-08-15 22:30:11 +00:00
Chris Broadfoot
c66da98c4f imports: don't use fastpath on appengine (syscall not available)
Change-Id: I02fbdf1a08894b35a19a3638357c0d42e64d42cc
Reviewed-on: https://go-review.googlesource.com/27078
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-08-15 22:01:42 +00:00
Brian Kennedy
62f0028207 imports: properly handle global variables when creating imports
Fixes golang/go#7463

Change-Id: Ib8b511a34c562a2b3cdce6bb143caa7cf453bd00
Reviewed-on: https://go-review.googlesource.com/23444
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2016-08-11 06:06:14 +00:00
Brad Fitzpatrick
1c6f639aae imports: don't ignore GOPATH if GOROOT is a prefix of GOPATH
Fixes golang/go#16458

Change-Id: I1aaec5d115dd703dd702101f6bec37bf8b02a73d
Reviewed-on: https://go-review.googlesource.com/25192
Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-07-26 13:45:23 +00:00
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
Josh Bleecher Snyder
1727758746 imports: add unsafe symbols to zstdlib
Change-Id: Ia5856e2197925671ebaa5e673af3e97fad1cdaee
Reviewed-on: https://go-review.googlesource.com/24463
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-06-24 22:29:30 +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
Konstantin Shaposhnikov
d4437bf597 imports: add go:generate directive for generating zstdlib.go
Change-Id: I8c54e01799f690819cda746ad5fb7c4d87e769c4
Reviewed-on: https://go-review.googlesource.com/22541
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-04-29 02:56:15 +00:00
Konstantin Shaposhnikov
e48e17a0b5 imports: update static stdlib index to go1.6
Change-Id: Ie19594472ec0fa2455a3f1aeac681e74eb3a6b0b
Reviewed-on: https://go-review.googlesource.com/22539
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-04-28 14:59:03 +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 Symonds
8297301f3a imports: Remove ancient Go 1.1 compatibility indirections.
Change-Id: Id389cba2d3696c3bd2c2987ddc1768059747e8ac
Reviewed-on: https://go-review.googlesource.com/17793
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-12-14 00:47:31 +00:00
Andrew Gerrand
d868c356db imports: update static stdlib index to go1.5
Fixes golang/go#12609

Change-Id: Ic7efa796d50037a3b0fbf87a748c07bd41f07e02
Reviewed-on: https://go-review.googlesource.com/14576
Reviewed-by: Dave Cheney <dave@cheney.net>
2015-09-15 05:17:18 +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
Peter Collingbourne
4f8578d2c0 astutil: move to go/ast/astutil
Change-Id: I9a45bfc07613eb2210081d306d71f0a4d152eda5
Reviewed-on: https://go-review.googlesource.com/2592
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-01-13 15:47:47 +00:00
David Symonds
24257c8cd2 tools: add import comments.
Change-Id: Idda6e64580432cb9a731e4ebf4005ee4ceb4202d
Reviewed-on: https://go-review.googlesource.com/1244
Reviewed-by: Andrew Gerrand <adg@golang.org>
2014-12-09 22:42:16 +00:00
Dmitri Shuralyov
a26eca502f go.tools/imports: use same logic as 'go' tool for pruning package search.
The previous logic would descend into "_"-prefixed names, "testdata" folders,
and skipped folders beginning with a digit.

Fixes golang/go#8958.

LGTM=bradfitz
R=golang-codereviews, dave, bradfitz
CC=golang-codereviews
https://golang.org/cl/167000043
2014-11-12 12:45:17 -08:00
Andrew Gerrand
5ebbcd132f go.tools: use golang.org/x/... import paths
Rewrite performed with this command:
  sed -i '' 's_code.google.com/p/go\._golang.org/x/_g' \
    $(grep -lr 'code.google.com/p/go.' *)

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/170920043
2014-11-10 08:50:40 +11: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
Josh Bleecher Snyder
b6c30b58fc go.tools/imports: recognize exports in cgo files
Fixes golang/go#8815.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/143660044
2014-09-29 12:46:11 -07: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
Russ Cox
27a49c973b goimports: print \n at end of errors
LGTM=r
R=r
CC=bradfitz, golang-codereviews
https://golang.org/cl/136230045
2014-09-05 14:13:44 -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
Robert Griesemer
30b1abe2f7 go.tools: fix various typos
LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/97920045
2014-05-02 14:38:08 -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
Josh Bleecher Snyder
3bca0c7d87 imports: use chan struct{} for disk semaphore
Update golang/go#7747

LGTM=bradfitz
R=bradfitz, iant
CC=golang-codereviews
https://golang.org/cl/86280043
2014-04-09 17:40:05 -07:00
Brad Fitzpatrick
87f95283ac imports: limit local disk concurrency, avoid reads in non-Go directories
If $GOPATH was large, or $GOPATH was $HOME and $HOME/src had
many files, the unbounded concurrency in loadPkgIndex/loadPkg
could make the operating system unhappy with so many
threads. (sigh once again for no async file IO and needing
threads for file operations)

In addition, don't call go/build.Context.Import on directories
that we've already determined to have no go files in them.
It's just a waste of time. Makes it about 3x faster on my
machine with hot caches and a big $HOME/src.

Fixes golang/go#7731

LGTM=iant, adg
R=golang-codereviews, iant, adg
CC=david.crawshaw, golang-codereviews
https://golang.org/cl/85670044
2014-04-08 16:43:52 -07: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