This makes the format code use the AST that is already cached on the file to do
the formatting. It also moves the core format code into the source directory.
Change-Id: Iaa79169708e92525cce326ea094ab98144fe1011
Reviewed-on: https://go-review.googlesource.com/c/148198
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Use the packagestest framework to test completion. Add support for a
slice of token.Position to packagestest to support this.
Change-Id: Ie5ddece4446a3c74419727461a77faa3788cb040
Reviewed-on: https://go-review.googlesource.com/c/148197
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
The fallback driver wasn't returning the roots in a
deterministic order because it was using sticking them in
a map and then iterating over that map to get each element.
Put them into a slice instead (and make a few small
associated changes) to preserve behavior.
Fixesgolang/go#28040Fixesgolang/go#28609
Change-Id: Ib8f8c88d65b7a48b2b04ca91e2d3c316d5bb5803
Reviewed-on: https://go-review.googlesource.com/c/148880
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This mainly entails writing compiler output files to a temporary
directory, as well as the corrupted files in TestVersionHandling.
This is a backport of CL 146119.
Fixesgolang/go#28644
Change-Id: I5701fe3fda7d6364411eef8265c575c279dbf9a1
Reviewed-on: https://go-review.googlesource.com/c/149017
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This change adds to the list of standard library functions known to be
print or printf wrappers.
Although the printf Analyzer is capable of identifying wrapper
functions in the standard library, some drivers (e.g. Bazel) do not
apply analyzers to the standard packages. Really this is a bug
in those drivers but it is not likely to be fixed for a while.
Change-Id: I2032d0cb5fcb50e7b9933a75809becdd680380ec
Reviewed-on: https://go-review.googlesource.com/c/148572
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Details:
- Add -source, -v, and -all flags to vet-lite.
These have no effect and issue a warning.
- Add usage message to vet-lite that lists all
analyzers and explains -foo.enable and other flags.
- Factor this help message (common to vet-lite and
multichecker) into analysisflags.
- Add legacy aliases of new flags.
e.g. -printfuncs is now -printf.funcs
The old names work but issue a warning when used.
Also: update comments to say -vettool not$GOVETTOOL
I think we should probably do away with singlechecker
in a follow-up: a singleton multichecker is good enough,
and will allow us to remove cases in the flag-processing
logic.
Change-Id: Ib62f16b5e2f4c382a29e6300a6246b2db9e08049
Reviewed-on: https://go-review.googlesource.com/c/148559
Reviewed-by: Michael Matloob <matloob@golang.org>
When main.main returns, the process exits, so there's no need to cancel contexts.
This CL was originally reviewed as
https://go-review.googlesource.com/c/go/+/106915
and then retried in
https://go-review.googlesource.com/c/go/+/148758
but then reverted due to an embarrassing sequence
of careless moves.
Change-Id: Icdee0650996a442023e030697f10d2c31fd5fdff
Reviewed-on: https://go-review.googlesource.com/c/148877
Run-TryBot: Alan Donovan <adonovan@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This enables bundling of programs with `main` method to single file.
I used this to upload code for the new codejam.
Change-Id: I9e65ba8717c1d81d6fb9bef8f234c21fc5b91421
GitHub-Last-Rev: 5d0d623c46ff1629e7371693ddf0916df328e319
GitHub-Pull-Request: golang/tools#32
Reviewed-on: https://go-review.googlesource.com/c/105515
Reviewed-by: Alan Donovan <adonovan@google.com>
In vet, the shadow check is experimental, meaning not on by default.
The new analysis API has no concept of experimental, but you can
easily supply a different checker. By providing a shadow command, we
make it easy for users that want it to run it:
$ go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
$ go vet -vettool $(which shadow) my/project
Change-Id: I25dc7f3c830296121c7217e4615e8ff90e1b7c79
Reviewed-on: https://go-review.googlesource.com/c/148565
Reviewed-by: Michael Matloob <matloob@golang.org>
The printf checker is in the passes/ subdirectory.
Change-Id: I0a912231280bc954fee3088050541ba5ecb17dde
Reviewed-on: https://go-review.googlesource.com/c/148571
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
The cgocall analyzer was originally written in vet to analyze "raw" cgo files,
which are type-checked on a best-effort basis. However, the go/analysis
API presents analyzers with "cooked" cgo files, which are well-typed
legal Go but obscure the relationship between a call C.f(...) and its arguments.
Prior to this CL, cgocall attempted to "uncook" the file, which was
as predictably fragile as it sounds, and it rapidly broke as the cgo
recipe evolved.
This change causes cgocall to parse, modify, type-check and analyze "raw"
cgo files. The approach (based on dot-importing the "cooked" package)
is rather too clever but should be more robust than the one it replaces.
Fixesgolang/go#28566
Change-Id: I3092a313c64d27153eaaa115fe8635abfed17023
Reviewed-on: https://go-review.googlesource.com/c/147317
Reviewed-by: Michael Matloob <matloob@golang.org>
The completion function belongs in internal/lsp/source, so move it
there. Some small refactoring of completion, by moving each type of
completion into helper functions that append to the list of results.
Change-Id: I8599092906609591d499183657fe2d21d1f74df1
Reviewed-on: https://go-review.googlesource.com/c/148397
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This reverts CL 145699 (commit 864069cfd1)
Reason for revert: If the mismatch is bad enough that goimports can't find
it again, then the import is just removed, even if the user adds it back again.
Fixes#28645.
Change-Id: I6c8fc5434c2b56c73b260bcec2c12d8746fac4ad
Reviewed-on: https://go-review.googlesource.com/c/148237
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This change makes AddNamedImport more symmetrical to DeleteNamedImport
in behavior, signature, and documentation.
In Go, it is valid to import a package (identified by an import path)
with different names in a single .go file¹. For example, this is ok,
and the program will build as long as there are no unused imports²:
import (
"path"
. "path"
_ "path"
pathpkg "path"
)
The four imports (represented by ast.ImportSpec type) have the same
import path, but different names.
It's currently possible to use DeleteNamedImport to delete the exact
{name, import path} ast.ImportSpec from an AST.
Previously, it wasn't possible to use AddNamedImport to add an exact
{name, import path} ast.ImportSpec if the AST already has another
ast.ImportSpec with the same import path (but different name).
This change fixes that, making it possible to use AddNamedImport as
expected.
Rename the ipath parameter to path in AddNamedImport for consistency
with DeleteNamedImport. Also change the language used in its
documentation to be more precise and consistent with DeleteNamedImport.
Add test cases for this behavior. The DeleteNamedImport test cases
were passing before any changes to the astutil code.
Add test coverage for the return bool value of {Add,Delete}NamedImport.
Document the behavior of DeleteNamedImport when there are duplicate
matching imports.
Simplify string(buf.Bytes()) to buf.String() in test helper code.
¹ https://golang.org/ref/spec#Import_declarations
² https://play.golang.org/p/bi6L34rQbcDFixesgolang/go#28605Fixesgolang/go#16411
Change-Id: I70e887f5174764ab1cf32a761c8c734e216eea67
Reviewed-on: https://go-review.googlesource.com/c/147448
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This will allow Sizes to be marshalled and unmarshalled. All the Sizes
we care about ane StdSizes anyways.
Change-Id: I79d1dcaebba32f7730de4375945e372eeefa78fe
Reviewed-on: https://go-review.googlesource.com/c/147978
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
go list in module mode doesn't like looking at vendor directories in
GOROOT. Skip them.
Change-Id: Iec501fbab70914ea6dd76dcbed97ecda461358d0
Reviewed-on: https://go-review.googlesource.com/c/148159
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
For clarity and performance reasons, we want the fast path of goimports
to be purely syntactic. Packages whose import paths don't match their
package names make this harder. Before this CL, we parsed each imported
package to get its real package name. Now, we make named imports for
such packages, and on subsequent runs we don't have to do the extra
work.
A package name matches its import path if the name is the last segment
of the path, or the next-to-last before a version suffix vNN. gopkg.in
style .vNN suffixes are considered mismatching.
goimports already had almost exactly the desired logic, but only when
adding a new import. So the bulk of this change is simply removing the
logic that allowed it to recognize that a mismatched import satisfied
some uses. With that gone, it will remove those imports as unused, then
add a new renamed import. Some comments may be destroyed.
Fixesgolang/go#28428
Change-Id: I53846e6046affb420f41719f84c71086c5b9e5e6
Reviewed-on: https://go-review.googlesource.com/c/145699
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This enables go to definition for the lsp.
It has one known non working case (where the filenames have $GOROOT in them)
Change-Id: I142c6e04b8691c5076dfcd55592ea710b4b361a4
Reviewed-on: https://go-review.googlesource.com/c/148158
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
I realized this was a mistake, we should try to keep the source
directory independent of the LSP protocol itself, and adapt in
the outer layer.
This will keep us honest about capabilities, let us add the
caching and conversion layers easily, and also allow for a future
where we expose the source directory as a supported API for other
tools.
The outer lsp package then becomes the adapter from the core
features to the specifics of the LSP protocol.
Change-Id: I68fd089f1b9f2fd38decc1cbc13c6f0f86157b94
Reviewed-on: https://go-review.googlesource.com/c/148157
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Add an autocompletion implementation to the LSP. Also, add a function
that type-checks a package at a certain position, returning an *ast.File
and token.Pos for querying completion.
Change-Id: I288d4e6ed168e6014e79a4436f31f5b4a99b7139
Reviewed-on: https://go-review.googlesource.com/c/147657
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This is more reliable than looking in the environment.
Change-Id: I96c093b89faaece6b6256eefb4a4aac4d66b9cc9
Reviewed-on: https://go-review.googlesource.com/c/146757
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
A file is strongly associated with a view, and knows how to manage it's own
contents.
We can also now track files that are not "active"
Change-Id: Ib9474cd40e5caa3db6596548612a9f90168b8a19
Reviewed-on: https://go-review.googlesource.com/c/147204
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This allows us to write the lsp verbs in terms of a stable underlying source
management layer.
This should make it easier to refactor the underlying layer to add more powerful
caching and incremental modes as we go.
Change-Id: Iab97b061d80394a6fa6748a93a4c68f2deb46129
Reviewed-on: https://go-review.googlesource.com/c/147201
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
The new go/analysis/cmd/vet tool found a few issues. Fix them by
explicitly ignoring results where needed and renaming examples in
accordance to package testing's guidelines to make them appear on the
godoc pages.
This change doesn't touch go/ssa/interp/external.go because the
possible misuse of package unsafe there looks intentional.
Change-Id: Id63109e3797818a5e869fe8ed1bc1476959541f2
Reviewed-on: https://go-review.googlesource.com/c/147297
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This is based on 147340 and has the same test, but with a different fix.
We now produce a nice error if there is an empty root, instead of
crashing, and the cause of empty roots in go list has been fixed.
The underlying call to go list is returning the same package more than
once, and we only fix the first entry in the root list, so the second
one got left blank.
The fix was to not add the second duplicate copy to the output of the
driver at all.
Change-Id: I9f1b2f0fd63635ba101cdd3c8a5108530e968ba9
Reviewed-on: https://go-review.googlesource.com/c/147440
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Change golang/go#145697 added tests for diagnostics in the LSP implementation,
but these test did not work with Go 1.10. This change skips tests that
require Go 1.11.
Change-Id: I52bd2df484b5786395edac2c1c8592c83ac1aaa4
Reviewed-on: https://go-review.googlesource.com/c/147439
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This improves the logging capabilities of the jsonrpc 2 library to always
include the method and also an optional elapsed time.
This is used to implement an lsp inspector compatible logging mode in the golsp.
Change-Id: I2f7ac8b9298c4364b1b89cf6f696b534557ed139
Reviewed-on: https://go-review.googlesource.com/c/146157
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
For performance, bail out of runNamedQueries before running go env if
there's nothing to do.
Add t.Helper() calls to packagestest.TestAll.
Escape # in packagestest.Export. The testing package adds #NN suffixes
to subtests that have redundant names.
Log how long gopathwalk.Walk took for name= queries when debug is on.
Change-Id: I37cb0ed11cf58e1693e29dea04697e5885ecc62b
Reviewed-on: https://go-review.googlesource.com/c/147203
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Add some basic tests for diagnostics using the new
go/packages/packagestest framework.
Change-Id: I6a7bfba6c392928a9eb123ab71ceb73785c12600
Reviewed-on: https://go-review.googlesource.com/c/145697
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
When the name= query constructs the temporary module, it may find things
that don't resolve. In at least some cases (#28518), allowing go list
to access the network results in not just bad performance but actual
failures. Default GOPROXY to "off" when doing queries on the temporary
module to try to address that.
Also, add some more debug logging, including various environment
variables, so that it's easier to reproduce failing commands.
Change-Id: I1a6d3ffa5c845271ce48e9fe802a2491ccadcd7c
Reviewed-on: https://go-review.googlesource.com/c/146477
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
The test correctly identified a problem.
Suppressing test to keep dashboard cleean, pending a fix.
Updates golang/go#28566
Change-Id: Ib3a8dbdd617c9f5701b5d6673434917d284dfb32
Reviewed-on: https://go-review.googlesource.com/c/147199
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This adds a library that allows markers and actions inside comments in go source
files.
It then also adds an easy way to use that library for tests using packagestest.Expect
This is used to easily write code inspection and manipulation tests in a
language that is common to all tests.
Change-Id: I755caaad1557c9b8779ad9ecda2b3309550d6976
Reviewed-on: https://go-review.googlesource.com/c/142998
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Also:
- add cmd/vet-lite, a version of cmd/vet that doesn't depend on
go/packages and must be run under "go vet". This will be vendored
into $GOROOT/src/cmd/vet.
- add an integration test for a unitchecker-based command under "go vet".
Change-Id: Id613dac2812816c6d6372fa6d1536c8d4e4c2676
Reviewed-on: https://go-review.googlesource.com/c/143418
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This permits constants of the form `const X = T(A)` to add `X` to the
stringer output for type `T`.
While those constants can be rewritten as `const X T = T(A)`, that
becomes tedious and visually noisy when `T` is a long name. It is quite
easy to address this easy and common case, while not attempting to solve
this with full generality.
Fixes#11581.
Change-Id: Ifb8e43515f05493de190e02577260d94dd851581
Reviewed-on: https://go-review.googlesource.com/c/146577
Run-TryBot: David Symonds <dsymonds@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
We do insist that drivers are stable (not sorted, just stable) and that
refine is also stable, which allows us to promise a stable output.
I also changed refine so it returns the root set in the same order that
the original root id list was supplied, as this seems to be a strictly
better experience.
Change-Id: I8eb0bffd7547865d14a6c6f18646018b9af140bd
Reviewed-on: https://go-review.googlesource.com/c/145877
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
If a Module's Name ends with a version suffix (v2), create its module
with an appropriate version number (v2.0.0).
Change-Id: I1a16f054fac5717e79871ba1bf9a6343c0ce2f5b
Reviewed-on: https://go-review.googlesource.com/c/146097
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
stringer's usage currently uses os.Args[0] to refer to the program name
as a defensive measure in case stringer is compiled and then run using a
name other than stringer, e.g. mystringer. However, the following lines
of the usage docs hardcode "stringer." So it seems this has little
benefit.
Indeed this style of using os.Args[0] can make the usage information of
stringer extremely ugly when stringer itself is invoked with an absolute
path:
Usage of /tmp/tmp.ahddh3ZjE7/.gobincache/golang.org/x/tools/@v/v0.0.0-20181030000716-a0a13e073c7b/golang.org/x/tools/cmd/stringer/stringer:
stringer [flags] -type T [directory]
stringer [flags] -type T files... # Must be a single package
...
Instead, opt for the simpler approach of hardcoding stringer throughout.
Change-Id: Ia296c8d9a91c94e77b7cc2b6242908c959fb5985
Reviewed-on: https://go-review.googlesource.com/c/145799
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Rob Pike <r@golang.org>