Skip to content

Commit

Permalink
deps: update x/tools and gopls to db4c57db (#1021)
Browse files Browse the repository at this point in the history
* gopls/internal/regtest: split regtests up into multiple packages db4c57db
* internal/lsp/cache: lock in snapshot.knownFilesInDir f871472f
* internal/lsp/source: make it an error to rename embedded fields c2bea79d
* gopls/internal/hooks: improve license file test 514964b7
* internal/lsp/cmd: improve help output of gopls subcommands 68bf78a6
* go/analysis/passes/fieldalignment: delete doc style comments in fix 4922717d
* gopls/internal/regtest: automate counting of editor notifications to await 917f61df
* internal/lsp: correct links provided in critical error pop-ups 2972602e
* internal/lsp: display current diagnostics in the debug server e13398c8
* gopls: factor out advanced documentation from the README cf1022a4
* gopls: mention workspaces and build systems in the README 87bc10f2
* internal/lsp: don't show context cancellation in the progress bar ce34e269
* gopls: merge README and user.md bec622c3
* gopls/internal/regtest: re-enable android builder 7e51fbd4
  • Loading branch information
myitcv committed Jan 29, 2021
1 parent e320576 commit a31027d
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 65 deletions.
10 changes: 6 additions & 4 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ func (s *snapshot) workspaceLayoutError(ctx context.Context) *source.CriticalErr
if !s.ValidBuildConfiguration() {
msg := `gopls requires a module at the root of your workspace.
You can work with multiple modules by opening each one as a workspace folder.
Improvements to this workflow will be coming soon (https://github.com/golang/go/issues/32394),
and you can learn more here: https://github.com/golang/go/issues/36899.`
Improvements to this workflow will be coming soon, and you can learn more here:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`
return &source.CriticalError{
MainError: errors.Errorf(msg),
ErrorList: s.applyCriticalErrorToFiles(ctx, msg, openFiles),
Expand Down Expand Up @@ -236,13 +236,15 @@ and you can learn more here: https://github.com/golang/go/issues/36899.`
msg := fmt.Sprintf(`This file is in %s, which is a nested module in the %s module.
gopls currently requires one module per workspace folder.
Please open %s as a separate workspace folder.
You can learn more here: https://github.com/golang/go/issues/36899.
You can learn more here: https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.
`, modDir, filepath.Dir(rootModURI.Filename()), modDir)
srcErrs = append(srcErrs, s.applyCriticalErrorToFiles(ctx, msg, uris)...)
}
if len(srcErrs) != 0 {
return &source.CriticalError{
MainError: errors.Errorf(`You are working in a nested module. Please open it as a separate workspace folder.`),
MainError: errors.Errorf(`You are working in a nested module.
Please open it as a separate workspace folder. Learn more:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`),
ErrorList: srcErrs,
}
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ func (s *snapshot) allKnownSubdirs(ctx context.Context) map[span.URI]struct{} {
// the given directory. It does not respect symlinks.
func (s *snapshot) knownFilesInDir(ctx context.Context, dir span.URI) []span.URI {
var files []span.URI
s.mu.Lock()
defer s.mu.Unlock()

for uri := range s.files {
if source.InDir(dir.Filename(), uri.Filename()) {
files = append(files, uri)
Expand Down Expand Up @@ -1009,6 +1012,9 @@ func (s *snapshot) awaitLoaded(ctx context.Context) error {

func (s *snapshot) GetCriticalError(ctx context.Context) *source.CriticalError {
loadErr := s.awaitLoadedAllErrors(ctx)
if errors.Is(loadErr, context.Canceled) {
return nil
}

// Even if packages didn't fail to load, we still may want to show
// additional warnings.
Expand Down Expand Up @@ -1075,6 +1081,10 @@ func (s *snapshot) awaitLoadedAllErrors(ctx context.Context) error {
// Do not return results until the snapshot's view has been initialized.
s.AwaitInitialized(ctx)

if ctx.Err() != nil {
return ctx.Err()
}

if err := s.reloadWorkspace(ctx); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Example:
$ # 1-indexed location (:line:column or :#offset) of the target identifier
$ gopls call_hierarchy helper/helper.go:8:6
$ gopls call_hierarchy helper/helper.go:#53
gopls call_hierarchy flags are:
`)
f.PrintDefaults()
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/govim/internal/golang_org_x_tools/lsp/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ func (c *check) DetailedHelp(f *flag.FlagSet) {
Example: show the diagnostic results of this file:
$ gopls check internal/lsp/cmd/check.go
gopls check flags are:
`)
f.PrintDefaults()
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/govim/internal/golang_org_x_tools/lsp/cmd/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Example:
$ # 1-indexed location (:line:column or :#offset) of the target identifier
$ gopls highlight helper/helper.go:8:6
$ gopls highlight helper/helper.go:#53
gopls highlight flags are:
`)
f.PrintDefaults()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Example:
$ # 1-indexed location (:line:column or :#offset) of the target identifier
$ gopls implementation helper/helper.go:8:6
$ gopls implementation helper/helper.go:#53
gopls implementation flags are:
`)
f.PrintDefaults()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Example:
$ # 1-indexed location (:line:column or :#offset) of the target identifier
$ gopls prepare_rename helper/helper.go:8:6
$ gopls prepare_rename helper/helper.go:#53
gopls prepare_rename flags are:
`)
f.PrintDefaults()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ func (c *semtok) DetailedHelp(f *flag.FlagSet) {
Example: show the semantic tokens for this file:
$ gopls semtok internal/lsp/cmd/semtok.go
gopls semtok flags are:
`)
f.PrintDefaults()
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/govim/internal/golang_org_x_tools/lsp/cmd/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ Example:
$ # 1-indexed location (:line:column or :#offset) of the target identifier
$ gopls signature helper/helper.go:8:6
$ gopls signature helper/helper.go:#53
gopls signature flags are:
`)
f.PrintDefaults()
}
Expand Down
34 changes: 34 additions & 0 deletions cmd/govim/internal/golang_org_x_tools/lsp/debug/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ type Client struct {
Logfile string
GoplsPath string
ServerID string
Service protocol.Server
}

// A Server is an outgoing connection to a remote LSP server.
Expand Down Expand Up @@ -314,6 +315,16 @@ func (i *Instance) getInfo(r *http.Request) interface{} {
return template.HTML(buf.String())
}

func (i *Instance) AddService(s protocol.Server, session *cache.Session) {
for _, c := range i.State.clients {
if c.Session == session {
c.Service = s
return
}
}
stdlog.Printf("unable to find a Client to add the protocol.Server to")
}

func getMemory(r *http.Request) interface{} {
var m runtime.MemStats
runtime.ReadMemStats(&m)
Expand Down Expand Up @@ -785,6 +796,29 @@ Using session: <b>{{template "sessionlink" .Session.ID}}</b><br>
{{if .DebugAddress}}Debug this client at: <a href="http://{{localAddress .DebugAddress}}">{{localAddress .DebugAddress}}</a><br>{{end}}
Logfile: {{.Logfile}}<br>
Gopls Path: {{.GoplsPath}}<br>
<h2>Diagnostics</h2>
{{/*Service: []protocol.Server; each server has map[uri]fileReports;
each fileReport: map[diagnosticSoure]diagnosticReport
diagnosticSource is one of 5 source
diagnosticReport: snapshotID and map[hash]*source.Diagnostic
sourceDiagnostic: struct {
Range protocol.Range
Message string
Source string
Code string
CodeHref string
Severity protocol.DiagnosticSeverity
Tags []protocol.DiagnosticTag
Related []RelatedInformation
}
RelatedInformation: struct {
URI span.URI
Range protocol.Range
Message string
}
*/}}
<ul>{{range $k, $v := .Service.Diagnostics}}<li>{{$k}}:<ol>{{range $v}}<li>{{.}}</li>{{end}}</ol></li>{{end}}</ul>
{{end}}
`))

Expand Down
48 changes: 48 additions & 0 deletions cmd/govim/internal/golang_org_x_tools/lsp/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ type fileReports struct {
reports map[diagnosticSource]diagnosticReport
}

func (d diagnosticSource) String() string {
switch d {
case modSource:
return "FromSource"
case gcDetailsSource:
return "FromGCDetails"
case analysisSource:
return "FromAnalysis"
case typeCheckSource:
return "FromTypeChecking"
case orphanedSource:
return "FromOrphans"
default:
return fmt.Sprintf("From?%d?", d)
}
}

// hashDiagnostics computes a hash to identify diags.
func hashDiagnostics(diags ...*source.Diagnostic) string {
source.SortDiagnostics(diags)
Expand Down Expand Up @@ -546,3 +563,34 @@ func (s *Server) shouldIgnoreError(ctx context.Context, snapshot source.Snapshot
})
return !hasGo
}

// Diagnostics formattedfor the debug server
// (all the relevant fields of Server are private)
// (The alternative is to export them)
func (s *Server) Diagnostics() map[string][]string {
ans := make(map[string][]string)
s.diagnosticsMu.Lock()
defer s.diagnosticsMu.Unlock()
for k, v := range s.diagnostics {
fn := k.Filename()
for typ, d := range v.reports {
if len(d.diags) == 0 {
continue
}
for _, dx := range d.diags {
ans[fn] = append(ans[fn], auxStr(dx, d, typ))
}
}
}
return ans
}

func auxStr(v *source.Diagnostic, d diagnosticReport, typ diagnosticSource) string {
// Tags? RelatedInformation?
msg := fmt.Sprintf("(%s)%q(source:%q,code:%q,severity:%s,snapshot:%d,type:%s)",
v.Range, v.Message, v.Source, v.Code, v.Severity, d.snapshotID, typ)
for _, r := range v.Related {
msg += fmt.Sprintf(" [%s:%s,%q]", r.URI.Filename(), r.Range, r.Message)
}
return msg
}
81 changes: 53 additions & 28 deletions cmd/govim/internal/golang_org_x_tools/lsp/fake/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ type Editor struct {
buffers map[string]buffer
// Capabilities / Options
serverCapabilities protocol.ServerCapabilities

// Call metrics for the purpose of expectations. This is done in an ad-hoc
// manner for now. Perhaps in the future we should do something more
// systematic.
calls CallCounts
// systematic. Guarded with a separate mutex as calls may need to be accessed
// asynchronously via callbacks into the Editor.
callsMu sync.Mutex
calls CallCounts
}

type CallCounts struct {
DidOpen, DidChange, DidChangeWatchedFiles int
DidOpen, DidChange, DidSave, DidChangeWatchedFiles, DidClose uint64
}

type buffer struct {
Expand Down Expand Up @@ -140,8 +143,8 @@ func (e *Editor) Connect(ctx context.Context, conn jsonrpc2.Conn, hooks ClientHo
}

func (e *Editor) Stats() CallCounts {
e.mu.Lock()
defer e.mu.Unlock()
e.callsMu.Lock()
defer e.callsMu.Unlock()
return e.calls
}

Expand Down Expand Up @@ -291,36 +294,48 @@ func (e *Editor) initialize(ctx context.Context, workspaceFolders []string) erro
return nil
}

// onFileChanges is registered to be called by the Workdir on any writes that
// go through the Workdir API. It is called synchronously by the Workdir.
func (e *Editor) onFileChanges(ctx context.Context, evts []FileEvent) {
if e.Server == nil {
return
}
e.mu.Lock()
defer e.mu.Unlock()
var lspevts []protocol.FileEvent
for _, evt := range evts {
// Always send an on-disk change, even for events that seem useless
// because they're shadowed by an open buffer.
lspevts = append(lspevts, evt.ProtocolEvent)

if buf, ok := e.buffers[evt.Path]; ok {
// Following VS Code, don't honor deletions or changes to dirty buffers.
if buf.dirty || evt.ProtocolEvent.Type == protocol.Deleted {
continue
}

content, err := e.sandbox.Workdir.ReadFile(evt.Path)
if err != nil {
continue // A race with some other operation.
// e may be locked when onFileChanges is called, but it is important that we
// synchronously increment this counter so that we can subsequently assert on
// the number of expected DidChangeWatchedFiles calls.
e.callsMu.Lock()
e.calls.DidChangeWatchedFiles++
e.callsMu.Unlock()

// Since e may be locked, we must run this mutation asynchronously.
go func() {
e.mu.Lock()
defer e.mu.Unlock()
var lspevts []protocol.FileEvent
for _, evt := range evts {
// Always send an on-disk change, even for events that seem useless
// because they're shadowed by an open buffer.
lspevts = append(lspevts, evt.ProtocolEvent)

if buf, ok := e.buffers[evt.Path]; ok {
// Following VS Code, don't honor deletions or changes to dirty buffers.
if buf.dirty || evt.ProtocolEvent.Type == protocol.Deleted {
continue
}

content, err := e.sandbox.Workdir.ReadFile(evt.Path)
if err != nil {
continue // A race with some other operation.
}
// During shutdown, this call will fail. Ignore the error.
_ = e.setBufferContentLocked(ctx, evt.Path, false, strings.Split(content, "\n"), nil)
}
// During shutdown, this call will fail. Ignore the error.
_ = e.setBufferContentLocked(ctx, evt.Path, false, strings.Split(content, "\n"), nil)
}
}
e.Server.DidChangeWatchedFiles(ctx, &protocol.DidChangeWatchedFilesParams{
Changes: lspevts,
})
e.calls.DidChangeWatchedFiles++
e.Server.DidChangeWatchedFiles(ctx, &protocol.DidChangeWatchedFilesParams{
Changes: lspevts,
})
}()
}

// OpenFile creates a buffer for the given workdir-relative file.
Expand Down Expand Up @@ -371,7 +386,9 @@ func (e *Editor) createBuffer(ctx context.Context, path string, dirty bool, cont
}); err != nil {
return errors.Errorf("DidOpen: %w", err)
}
e.callsMu.Lock()
e.calls.DidOpen++
e.callsMu.Unlock()
}
return nil
}
Expand All @@ -393,6 +410,9 @@ func (e *Editor) CloseBuffer(ctx context.Context, path string) error {
}); err != nil {
return errors.Errorf("DidClose: %w", err)
}
e.callsMu.Lock()
e.calls.DidClose++
e.callsMu.Unlock()
}
return nil
}
Expand Down Expand Up @@ -458,6 +478,9 @@ func (e *Editor) SaveBufferWithoutActions(ctx context.Context, path string) erro
if err := e.Server.DidSave(ctx, params); err != nil {
return errors.Errorf("DidSave: %w", err)
}
e.callsMu.Lock()
e.calls.DidSave++
e.callsMu.Unlock()
}
return nil
}
Expand Down Expand Up @@ -653,7 +676,9 @@ func (e *Editor) setBufferContentLocked(ctx context.Context, path string, dirty
if err := e.Server.DidChange(ctx, params); err != nil {
return errors.Errorf("DidChange: %w", err)
}
e.callsMu.Lock()
e.calls.DidChange++
e.callsMu.Unlock()
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/govim/internal/golang_org_x_tools/lsp/fake/workdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (w *Workdir) sendEvents(ctx context.Context, evts []FileEvent) {
copy(watchers, w.watchers)
w.watcherMu.Unlock()
for _, w := range watchers {
go w(ctx, evts)
w(ctx, evts)
}
}

Expand Down
1 change: 1 addition & 0 deletions cmd/govim/internal/golang_org_x_tools/lsp/lsprpc/lsprpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (s *StreamServer) ServeStream(ctx context.Context, conn jsonrpc2.Conn) erro
server := s.serverForTest
if server == nil {
server = lsp.NewServer(session, client)
debug.GetInstance(ctx).AddService(server, session)
}
// Clients may or may not send a shutdown message. Make sure the server is
// shut down.
Expand Down
Loading

0 comments on commit a31027d

Please sign in to comment.