Commit Graph

6897 Commits

Author SHA1 Message Date
Robert Findley ffb862b544 gopls/internal/lsp/cache: remove stray print statement
A stray print statement was accidentally left in CL 441877. Remove it.

Change-Id: I44e4408059f30d35ad8c84b070aea3e197762d1c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/442782
gopls-CI: kokoro <noreply+kokoro@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-13 17:41:26 +00:00
Charlie Vieth f87c1ed972 internal/fastwalk: improve Darwin performance by ~3x
On darwin/cgo use readdir_r instead of syscall.ReadDirent() since the
later is simulated and slow (see: golang/go#30933).

Unlike CL 392094, this uses CGO instead of "//go:linkname" and assembly.

goos: darwin
goarch: arm64

FastWalk-10    68.9ms ±11%    20.7ms ± 2%  -69.89%  (p=0.008 n=5+5)

name         old alloc/op   new alloc/op   delta
FastWalk-10    1.49MB ± 0%    1.51MB ± 0%   +1.06%  (p=0.008 n=5+5)

name         old allocs/op  new allocs/op  delta
FastWalk-10     32.2k ± 0%     30.7k ± 0%   -4.61%  (p=0.016 n=5+4)

Fixes golang/go#51356

Change-Id: Ia3afd06c8f14bd2036b2a1ea6e3cafbef81d3530
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436780
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Auto-Submit: Heschi Kreinick <heschi@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-13 16:31:57 +00:00
Robert Findley b2533141c6 gopls/internal/lsp/cache: add support for loading standalone main files
Add support in gopls for working on "standalone main files", which are
Go source files that should be treated as standalone packages.
Standalone files are identified by a specific build tag, which may be
configured via the new standaloneTags setting. For example, it is common
to use the directive "//go:build ignore" to colocate standalone files
with other package files.

Specifically,
- add a new loadScope interface for use in snapshot.load, to add a bit
  of type safety
- add a new standaloneTags setting to allow configuring the set of build
  constraints that define standalone main files
- add an isStandaloneFile function that detects standalone files based
  on build constraints
- implement the loading of standalone files, by querying go/packages for
  the standalone file path
- rewrite getOrLoadIDsForURI, which had inconsistent behavior with
  respect to error handling and the experimentalUseInvalidMetadata
  setting
- update the WorkspaceSymbols handler to properly format
  command-line-arguments packages
- add regression tests for LSP behavior with standalone files, and for
  dynamic configuration of standalone files

Fixes golang/go#49657

Change-Id: I7b79257a984a87b67e476c32dec3c122f9bbc636
Reviewed-on: https://go-review.googlesource.com/c/tools/+/441877
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-13 15:07:40 +00:00
Alan Donovan 3beecff0f6 gopls/internal/span: some cleanups
This change eliminates these redundant helper functions
- cache.rangeFromPositions
- source.LineToRange
- source.ByteOffsetsToRange
and makes various other simplifications and documentation
improvements.

Change-Id: Ic820ab560d71b88bde00b005e3a919334a5d1856
Reviewed-on: https://go-review.googlesource.com/c/tools/+/442015
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
2022-10-13 13:15:27 +00:00
Suzy Mueller d3752388a2 gopls: dedup upgrade code actions for vulncheck
Make sure we aren't sending multiple code actions
that do the same thing. This also adds a upgrade
to latest code action.

Change-Id: Ic9cecd0a9410648673d4afe63da5a940960a4afc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436776
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
2022-10-12 18:24:48 +00:00
Alan Yee b20ae4bcf7 README: format install command
Separate command into a separate line in order to make it easier to copy and paste into the terminal

Change-Id: I2e446475201c40ddb5054fe403ff23ed79b0742e
GitHub-Last-Rev: bbd650aa12415725168b59ec934aafeeaf0f21aa
GitHub-Pull-Request: golang/tools#346
Reviewed-on: https://go-review.googlesource.com/c/tools/+/361394
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
2022-10-12 14:51:54 +00:00
Hana (Hyang-Ah) Kim 19a5504ffc gopls/internal/lsp: use the golang.org/x/vuln/exp/govulncheck
Change-Id: Ifac1c9398a7d0923fa84c175ce8eea40e41a93f6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/435362
Reviewed-by: Suzy Mueller <suzmue@golang.org>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-11 21:42:28 +00:00
Tim King ab79327e05 cmd/ssadump: disable run mode with runtime package
Changes `ssadump -run` to ensure that the package runtime
is not imported (changed from must be imported). For several
years, the runtime package has used unsafe constructs
x/tools/go/ssa/interp cannot interpret. This must have been
failing a similar amount of time.

This is unfortunate, but is unlikely to be addressed soon.

For golang/go#43163

Change-Id: I9e2aee640ff7b1123e591e6c49cac9967c5e8da8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/441817
Run-TryBot: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-11 20:40:41 +00:00
Alan Donovan 29429f53af gopls/internal/lsp/source: sort protocol edits
The LSP protocol doesn't require edits to be sorted,
but some clients such as govim were relying on it,
as we learned when I recently removed the sort.
This change restores the sort behavior.

See also govim/govim#1171

Change-Id: I589b6cfde933ea6907859bf40acd33c1a7fcde02
Reviewed-on: https://go-review.googlesource.com/c/tools/+/442256
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
2022-10-11 18:07:12 +00:00
Michael Matloob 49b340b352 go/analysis: update tests for different go list error behavior
golang.org/cl/437298 updates go list to not exit with a non-zero
error code in some cases and instead place the error on the Package
struct. Also, as a cleanup, some places where exit status 2 was returned
will return exit status 1 (bringing it in line with other errors
returned by the go command).

TestEncodeDecode in facts_test.go has been updated to fix a missing
function body that is not relevant to the test, but that was causing an
error because the Package struct now has an error on it.

TestRunDespiteErrors in checker_test.go has been updated to reflect that
in some cases an analysis with RunDespiteErrors will fail to run because
a build error returned by go list when it's run to get export data is
not recognized as being a parse/typechecker error (the kind of error
allowed by TestRunDespiteError).

TestIntegration in unitchecker_test.go has been updated to reflect that
go vet running unitchecker will now fail with exit status 1 instead of 2
(so it just checks for a zero or non-zero status).

For golang/go#25842

Change-Id: Idbbd19b5de661e6e5f49e0475c5bc918d8e33803
Reviewed-on: https://go-review.googlesource.com/c/tools/+/441879
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-11 16:22:44 +00:00
Robert Findley cd0288ff85 internal/lsp/cache: invalidate analysis results on packages.Load
Most invalidation happens in snapshot.clone, but to be safe we were also
invalidating package data when new metadata is set via packages.Load. At
the time, I wasn't sure why this was necessary.

Now I understand: with experimentalUseInvalidMetadata it is possible
that we re-compute invalidated data using stale metadata in between the
initial clone and the subsequent reload. I noticed that it was also
possible to have stale analysis results after the Load results arrive.

Fix this by invalidating analysis results on Load, in addition to
packages. Factor out invalidation into a new helper method.

Since we believe this may fix analyzer panics, re-enable strict handling
of analysis panics during tests.

For golang/go#56035
For golang/go#54762
For golang/go#42857

Change-Id: I8c28e0700f8c16c58df4ecf60f6127b1c05d6dc5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/420538
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
2022-10-11 16:03:54 +00:00
Robert Findley 906c733cc2 gopls/internal/lsp/source: find references in test packages
When finding references or implementations, we must include objects
defined in intermediate test variants. However, as we have seen we
should be careful to avoid accidentally type-checking intermediate test
variants in ParseFull, which is not their workspace parse mode.
Therefore eliminate the problematic TypecheckAll type-check mode in
favor of special handling in this one case where it is necessary.

Along the way:
 - Simplify the mapping of protocol position->offset. This should not
   require getting a package, or even parsing a file. For isolation,
   just use the NewColumnMapper constructor, even though it may
   technically result in building a token.File multiple times.
 - Update package renaming logic to use TypecheckWorkspace, since we
   only need to rename within the workspace.
 - Add regtest support for Implementations requests.

Fixes golang/go#43144

Change-Id: I41f684ad766f5af805abbd7c5ee0a010ff9b9b8c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438755
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
2022-10-11 14:54:39 +00:00
Robert Findley 2a41b2554b internal/robustio: fix log.Fatal calls that should be log.Fatalf
Change-Id: I810160ebabfdd6062f5cdd902ceda77b5912a6fa
Reviewed-on: https://go-review.googlesource.com/c/tools/+/441876
Reviewed-by: Peter Weinberger <pjw@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
2022-10-10 21:31:25 +00:00
Robert Findley 150b5f8bb6 internal/imports: read entire API dir in mkstdlib.go
Read the entire API directory, so that we don't need to edit mkstdlib.go
when regenerating zstdlib.go for new go versions.

Joint with Dylan Le.

Co-Authored-By: dle8@u.rochester.edu.
Change-Id: If5c71bfcfd2104f923df10cf21d16b5280fed025
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438504
Reviewed-by: Dylan Le <dungtuanle@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
2022-10-10 18:26:39 +00:00
Robert Findley 19cfa79754 internal/lsp/source: switch the default diff algorithm to "checked"
We've been using this setting in experimental for some time without
issue, so switch it to default.

For golang/go#52967

Change-Id: Ib4d786e689d4b0f009195cc86d7dd5d8269cf424
Reviewed-on: https://go-review.googlesource.com/c/tools/+/427534
Reviewed-by: Peter Weinberger <pjw@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-10 17:23:42 +00:00
Suzy Mueller fa6bd3b49c gopls: include informational vulnerability diagnostics
Unaffecting vulnerabilities that appear should be shown as
informational diagnostics. These do not have current version.

Change-Id: I5dc8d111fd9de8388195627c8f050a2660426abb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/441875
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
2022-10-10 16:14:14 +00:00
Robert Findley 89b4335324 gopls/internal/lsp: merge notifications about deprecated settings
VS Code suppresses notifications if we send too many, but we don't want
users to miss warnings about deprecated settings. Merge them all into a
single message body.

Also fix a race in a test that added in the preceding CL: the old go
warnings may race with the initial workspace load.

For golang/vscode-go#2487

Change-Id: I69d61a17e0e6e888fa04fa1edce864e28a8d650e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/440180
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2022-10-10 13:27:02 +00:00
cui fliter f90d8ad46c all: fix a few function names on comments
Change-Id: I91eec68ebd9394685a6586c8d0cf01ecf3fd82e2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/441775
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2022-10-10 13:22:38 +00:00
Robert Findley 350f1e2c0a gopls/internal/lsp/fake: retry ephemeral errors when renaming on windows
Investigation of renaming flakes revealed that renaming is known to be
flaky on windows, and the go command has a robustio package that works
around known flakes for certain IO operations on darwin and windows.

Rather than duplicate this logic piecemeal, copy the entire robustio
package to the gopls module, along with a script to sync it from GOROOT
using go generate. Use this new package to de-flake renaming, and
replace an existing workaround.

The copy script got a little out of hand at the point where I needed to
add +build constraints. Nevertheless, I've decided to keep it with the
caveat that it may be removed if it proves too difficult to maintain. As
is, it at least serves as documentation for how the sync was done.

For golang/go#56040
For golang/go#56039
For golang/go#56038
For golang/go#55324

Change-Id: Ifeda408ac44a2866e84015a2a38ae340dc0a88bb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/440181
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-07 21:46:09 +00:00
Alan Donovan 8b1d1ec39c gopls/internal/lsp/cache: suppress panics during analysis
We now have a plausible model for the cause of the analysis
crashes and are disabling this check until we have submitted
a plausible fix.

(Specifically: actionHandle.promise may be a stale promise
for an older package with the same ID, since snapshot.actions
uses the package ID as a key. This causes us to re-use
stale inspectors, whose syntax nodes won't be found in TypesInfo.)

Updates golang/go#54762
Updates golang/go#56035

Change-Id: I1a7cc1b02b8c322692b1a6bee03f6cd858c08ea0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/440179
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
2022-10-07 18:11:00 +00:00
Robert Findley 20c1ee70e4 gopls/internal/lsp: warn about Go versions that are too old
Add a showMessage notification when the Go version in PATH is too old.

Also delete the unused View.Rebuild method.

Updates golang/go#50825

Change-Id: I279a04f021a0f8ddb09fcfe299fbab8d10e8c022
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439836
Run-TryBot: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2022-10-07 17:55:15 +00:00
Alan Donovan 709f10829c gopls/internal/lsp/cache: suppress crashes in fact_purity analyzer
It has a known bug, and the purpose of the PanicOnBugs is to
find unknown bugs.

Updates golang/go#54762
Updates golang/go#56035

Change-Id: Id9fdfff478ef52b1d864acee366991808baeb574
Reviewed-on: https://go-review.googlesource.com/c/tools/+/440178
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-07 17:27:56 +00:00
Alan Donovan a410e98a82 internal/diff: ToUnified may fail
LineEdits has similar consistency preconditions to ApplyEdits.
Previously they were assumed, and bad input would create bad
output or crashes; now it uses the same validation logic
as ApplyEdits. Since it reports an error, computation of a
unified diff can also fail if the edits are inconsistent.

The ToUnified([]Edit) function now returns an error. For
convenience we also provide a wrapper (Unified) that cannot
fail since it calls Strings and ToUnified consistently.

LineEdits itself is now private.

Change-Id: I3780827f501d7d5c9665ec8be5656331c0dcda8e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/440175
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
2022-10-07 15:16:55 +00:00
Alan Donovan 26a95e6901 gopls/internal/span: move internal/span into gopls
Spans are logically part of gopls, but could not be moved
into the gopls module because of a number of depenencies
from packagestest, analysis, and internal/diff.
Those edges are now broken.

Change-Id: Icba5ebec6b27974f832a1186120a4b87d5f87103
Reviewed-on: https://go-review.googlesource.com/c/tools/+/440176
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-07 14:50:44 +00:00
Alan Donovan f2c4579afe internal/diff: avoid unnecessary allocations
Previously, a diff of non-ASCII strings would incur
three complete copies of the buffers; this changes
reduces it to one.

Also:
- add diff.Bytes function, to avoid unnecessary conversions.
- remove unused diff.Lines and LineEdits functions from API.
- remove TODO to use []bytes everywhere.
  We tried it in CL 439277 and didn't like it.
- Document that the diff is textual, even when given []byte.

Change-Id: I2da3257cc3d12c569218a2d7ce182452e8647a96
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439835
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-07 14:48:35 +00:00
Alan Donovan 60ddccae85 internal/diff: Apply: validate inputs
Apply now checks that its edits are valid
(not out of bounds or overlapping),
and reports an error if not.

It also sorts them, if necessary, using (start, end)
as the key, to ensure that insertions (end=start)
are ordered before deletions at the same point
(but without changing the relative order of insertions).

Two other implementations of the diff.Apply algorithm
have been eliminated. (One of them failed to sort edits,
requiring the protocol sender to do so; that burden
is now gone.)

Change-Id: Ia76e485e6869db4a165835c3312fd14bc7d43db2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439278
Auto-Submit: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
2022-10-07 14:24:20 +00:00
Alan Donovan 02bef08ac8 go/packages/packagestest: break dependency on gopls' span package
The span package is properly part of gopls, and we'd like to move
it into that module. However, the packagestest package unfortunately
depends on span.Span as an alternative notation for Ranges.

This change decouples span.Span from packagestest.Range using a
new (unexported for now) rangeSetter interface, which Span implements.
Neither package depends on the other.

Technically this is a breaking API change:
all the Range methods have gone away, as have the Span, URI,
and Point types and their methods, which were accessible via
Range.Span(). However, clients would not be able to access
these internal types, and I think it is highly unlikely that
anyone depends on it. Nonethless this is a cautionary tale about
the risks from one innocuous-looking type alias declaration.

Change-Id: I8acb03f4acb1f798f304b03648445e37a44f9c45
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439715
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2022-10-07 14:18:09 +00:00
Alan Donovan 778f9457c0 go/analysis: break dependency on span package
The analysis driver uses a hack to reconstruct type
errors (which contain a token.Pos) from packages.Errors
(which contain file:line:col strings). This hack will
be obviated by https://go.dev/cl/425095 but in the meantime
it creates a dependency on the span package, which is
really part of gopls.

This change replaces the span.Parse function with simple
calls to the standard library to extract the line and column.
(Dependency aside, span.Parse was always overkill for this task,
but perhaps packages.Error should have a File,Line,Col accessor.)

Change-Id: I55108337f8a753523db68fe9e2aea15bb059cf15
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439755
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-06 21:06:44 +00:00
Hana (Hyang-Ah) Kim c68255586f gopls/internal/regtest/misc: delete testdata
Change-Id: I45b2b80a81e8604765944e1d91c3e9f392b3e899
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439716
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-06 18:44:06 +00:00
Hana (Hyang-Ah) Kim 15525299ae gopls/internal/regtest/misc: use vulntest for vuln_test
Make the test run only with go1.18+.

Change-Id: I8a2645929070bc1a63401ee942de1e88b5c083fa
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439275
Reviewed-by: Jamal Carvalho <jamal@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-06 17:35:09 +00:00
Tim King c4f49e40d0 go/analysis/passes/assign: fix map assignment
Eliminate false positives on map self-assignments. These have side-effects.

Fixes golang/go#54840

Change-Id: I1b419e142a9681c68ea6fcddf8b368af903b1b54
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438796
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Tim King <taking@google.com>
2022-10-06 16:57:58 +00:00
cui fliter bd8c28ff5c internal/diff/lcs: fix shell format error
Change-Id: Id333dd169c7b7a54823bd077ce677fc74b4eb057
GitHub-Last-Rev: f16abccaad199cf229fb2e6dbf126441b4e573b5
GitHub-Pull-Request: golang/tools#397
Reviewed-on: https://go-review.googlesource.com/c/tools/+/431136
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
2022-10-06 14:25:42 +00:00
Alan Donovan ede3ab263c gopls/internal/lsp/protocol: simplify OffsetRange, Position
These functions are now both expressed in terms of OffsetPosition,
which converts a file offset to a protocol (UTF-16) position.
No span.Point intermediaries are allocated.

Also, note some TODOs for further simplification.

Change-Id: I7e95c1b3ab16e4acfe4e461ee0aaa260efb6eec5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439276
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
2022-10-06 13:48:25 +00:00
Hana (Hyang-Ah) Kim dc3cf95c8a gopls/internal/vulncheck: use vulntest for test database creation
Change-Id: Ie224452dfa512647e4f829e7f3be48db129cf91b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438741
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
2022-10-05 23:17:51 +00:00
Hana (Hyang-Ah) Kim 4ef38dc8f2 gopls/internal/vulncheck/vulntest: add test helpers
This package hosts helper utilities for vulncheck features. This package requires go1.18+.

Most of the code were adopted from golang.org/x/vulndb/internal.

The first batch is NewDatabase reads YAML-format vulnerability
information files
(https://github.com/golang/vulndb/blob/master/doc/format.md)
packaged in txtar, and creates a filesystem-based vuln database
that can be used as a data source of golang.org/x/vuln/client
APIs.

See db_test.go for example.

* Source of the code

db.go:
  golang.org/x/vulndb/internal/database#Generate
report.go:
  golang.org/x/vulndb/internal/report#Report
stdlib.go:
  golang.org/x/vulndb/internal/stdlib

This change adds a new dependency on "gopkg.in/yaml.v3"
for parsing YAMLs in testing

Change-Id: Ica5da4284c38a8a9531b1f943deb4288a2058c9b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/435358
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
2022-10-05 23:17:31 +00:00
Hana (Hyang-Ah) Kim 2f57270232 gopls: update golang.org/x/vuln
This is to pick up the fix for the file-scheme url handling bug

cd gopls
GOPROXY=direct go get golang.org/x/vuln@2aa0553d353b
go mod tidy -compat=1.16

Also updates the tests to use span.URIFromPath to generate
correct test database file uris.

Change-Id: I6de296cd21f3b98d72700ea57d1aa867658e7ac3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438756
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-05 23:17:13 +00:00
Alan Donovan d96b2388c6 internal/diff: simplify API, break span dependency
diff.TextEdit (now called simply Edit) no longer has a Span,
and no longer depends on the span package, which is really
part of gopls. Instead, it records only start/end byte
offsets within an (implied) file.

The diff algorithms have been simplified to avoid the need to
map offsets to line/column numbers (e.g. using a token.File).
All the conditions actually needed by the logic can be derived
by local string operations on the source text.

This change will allow us to move the span package into the
gopls module.

I was expecting that gopls would want to define its own
Span-augmented TextEdit type but, surprisingly, diff.Edit
is quite convenient to use throughout the entire repo:
in all places in gopls that manipulate Edits, the implied
file is obvious. In most cases, less conversion boilerplate
is required than before.

API Notes:
- diff.TextEdit -> Edit (it needn't be text)
- diff.ApplyEdits -> Apply
- source.protocolEditsFromSource is now private

Change-Id: I4d7cef078dfbd189b4aef477f845db320af6c5f6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436781
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
2022-10-05 20:32:15 +00:00
Alan Donovan 9856077059 internal/diff: abolish errors
Computing the difference between two strings is logically an
infallible operation. This change makes the code reflect that. The
actual failures were unreachable given consistent inputs, but that was
hard to see from the complexity of the logic surrounding span.Span.
(The problem only occurs when converting offsets beyond the end of the
file to Spans, but the code preserves the integrity of offsets.)

gopls' "old" hooks.ComputeEdits impl (based on sergi/go-diff) now
reports a bug and returns a single diff for the entire file if it
panics.

Also, first steps towards simpler API and a
reusable diff package in x/tools:

- add TODO for new API. In particular, the diff package shouldn't care
  about filenames, spans, and URIs. These are gopls concerns.
- diff.String is the main diff function.
- diff.Unified prints edits in unified form;
  all its internals are now hidden.
- the ComputeEdits func type is moved to gopls (source.DiffFunction)
- remove all non-critical uses of myers.ComputeEdits. The only
  remaining one is in gopls' defaults, but perhaps that gets
  overridden by the default GoDiff setting in hooks, to BothDiffs
  (sergi + pjw impls), so maybe it's now actually unused in practice?

Change-Id: I6ceb5c670897abbf285b243530a7372dfa41edf6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436778
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-05 20:30:46 +00:00
Suzy Mueller 33c2dbf380 gopls/internal/lsp: remove extra newlines in vulncheck diagnostics
Diagnostics are populated with vuln details that are already
formatted with newlines to limit line length. The client UI has
its own wrapping logic, so we can remove this to allow the client
to do its own formatting.

Change-Id: Ida0ce71886add995fad7815e6a302d4b44de65e8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436775
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-05 18:20:14 +00:00
Robert Findley b280e27dc7 gopls/internal/lsp/cache: make IsIntermediateTestVariant a method
Metadata.IsIntermediateTestVariant can be derived from PkgPath and
ForTest, so make it a method. Along the way, document the easily missed
fact that intermediate test variants are not workspace packages.

For golang/go#55293

Change-Id: Ie03011aef9c91ebce89e8aad01ef39b65bdde09a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438497
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
2022-10-05 17:59:57 +00:00
Robert Findley c5514b75d9 gopls/internal/lsp/source: use PackageFromFile in Identifier
When searching for declaration information about a position, it should
suffice to search the narrowest fully type-checked package containing
the file.

This should significantly improve performance when there are many test
variants of the current package, that have not yet been type-checked in
the ParseFull mode (as reported in golang/go#55293).

For golang/go#55293

Change-Id: I89a1999f9fe82dea51dd47db769c90b69be5e454
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438496
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-05 17:59:47 +00:00
Robert Findley ff4ff8bf36 gopls/internal/lsp/source: don't type-check in FindPackageFromPos
In all cases where we call FindPackageFromPos, we know that the given
position must be in the forward transitive closure of an originating
package. Refactor to use this information, potentially saving
significant type-checking when searching for a package.

As a result of this change, we never need to search intermediate test
variants when querying PackagesForFile.

Also replace snapshot arguments with token.FileSet arguments, when the
snapshot is only needed for its FileSet.

For golang/go#55293

Change-Id: Icf6236bea76ab5105a6bab24ce3afc574147882b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438495
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-05 17:57:07 +00:00
Alan Donovan 2d32e152db gopls/internal/lsp/cache: in tests, show all stacks during analyzer crash
We recently started using bug.Errorf to report analyzer crashes,
causing tests to fail sporadically. Unfortunately the log included
only the "panicked" message but not the relevant stack of the panic
or of the other goroutines, giving limited evidence on which to
investigate the underlying cause, suspected to be in package loading,
not the analyzer itself.

This change causes such crashes to display all stacks so that we
can gather more evidence over the next few days before we suppress
the crashes again.

Updates golang/go#54762
Updates golang/go#56035

Change-Id: I2d29e05b5910540918816e695b458463a05f94d9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/439116
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-05 16:05:47 +00:00
cui fliter dc88e7b4a2 godoc: fix some comments
Change-Id: Ia43fc5183e97d7d612216722e5255734d28ac508
GitHub-Last-Rev: aa1b6bbb3740a32c8d141d122e880498ed46adba
GitHub-Pull-Request: golang/tools#403
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436455
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2022-10-04 18:24:48 +00:00
cui fliter 7f79a022cb gopls: use fmt.Fprintf
Change-Id: Ife7f3269df554d2ecb6d1ccf6d1c1fa6de49587c
GitHub-Last-Rev: 9faefaefc2cc344859edeb2912cc60e9f2be4850
GitHub-Pull-Request: golang/tools#406
Reviewed-on: https://go-review.googlesource.com/c/tools/+/438537
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-10-04 16:29:17 +00:00
Dylan Le 40dabfa858 gopls/internal/lsp: add support for package renaming
Users can now rename packages by performing a rename on the package name in package declarations. The editor will prompt user with a dialogue text field to change the package name. This rename feature then do the following:
- Rename all the external imports of the renaming package. In case there is renaming conflicts within a file, the feature repeatedly try fresh names constructed by appending new package name with number of try times so far until succeed.
- Rename all the internal references to the renaming package from its files.
- Rename the directory of the renamed package and update the imports' paths of any packages nested under the renamed directory.
- Rename the test package with the new package name and suffix "_test" with the current test package name ends with "_test", or just the new package name otherwise.

The regression tests accounts for these edge cases:
- Only rename other known packages if the module path is same for both the renaming package and the other known packages.
- Prevent renaming main package.
- Test if the renaming package name is different from its path base.

Todo:
- Add a test for the case when the renaming package's path contains "internal" as a segment.
- Allow edit to the whole package path not just only the last segment of the package path
- Reject renaming if the renamed subpackages don't belong to the same module with the renaming package
- Check the go.mod files in the workspace to see if any replace directives need to be fixed if the renaming affects the locations of any go.mod files

Change-Id: I4ccb700df5a142c3fc1c06f3e5835f0f23da1ec5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/420958
Run-TryBot: Dylan Le <dungtuanle@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
2022-10-04 16:03:06 +00:00
Alan Donovan 55e5cff611 internal/diff: unified: match GNU diff for empty file
GNU diff -u prints -0,0 for the special case of inserting
into an empty file. (I don't understand why this case is
special, but it's fair to assume it has been debugged;
perhaps patch(1) has a bug.)

This change matches that special behavior, and adds a test.
It also adds a (disabled) test for another bug I encountered.

Also, remove some overzealous uses of t.Helper() that served
to obscure the failure.

Change-Id: I3e0c389c478cde45163353130e36f2f8741a8659
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436782
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Alan Donovan <adonovan@google.com>
2022-10-03 15:32:10 +00:00
Alan Donovan 3e0355b898 gopls/.../fillstruct: support generic types
The code that generates a name for a struct type,
and a default value for a struct field, now
supports the case where either contains a type
parameter.

Also:
- use type-checker's name for the struct type unless
  it is a bare struct.
- populateValue: return *new(T) for a type parameter.
- various minor cleanups.
- various TODO comments for clarifications.
- fuzzy.FindBestMatch: use strings not identifiers.
  Remove Find prefix (also FindMatchingIdentifiers).

Fixes golang/go#54836

Change-Id: I4f6132598b4ac7e72ea1405e4a14d6a23c1eeeaa
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436777
Auto-Submit: Alan Donovan <adonovan@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-30 19:58:05 +00:00
Suzy Mueller ed98f10ce9 gopls: prefix vulncheck diagnostic message with title
To help indicate that the diagnostic is for an error in a dependency,
not in the user's code prefix the diagnostic with
"[module] has a known vulnerability:"

Change-Id: Ib48f2e2cb2620fc6d56c4b0fd5d125cb4789283c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/436217
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2022-09-30 17:40:30 +00:00
Lasse Folger b180effdbe x/tools/go/analysis: extend json output by SuggestedFixes
+ The JSON output now contains SuggestedFixes from the Diagnostics. The edits
  are encoded as replacements for ranges defined by 0 based byte start and end
  indicies into the original file.
+ This change also exports the structs that are used for JSON encoding
  and thus documents the JSON schema.

Fixes golang/go#55138

Change-Id: I93411626279a866de2986ff78d93775a86ae2213
Reviewed-on: https://go-review.googlesource.com/c/tools/+/435255
Reviewed-by: Tim King <taking@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
2022-09-30 13:58:12 +00:00