github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/diagnostics.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package lsp 6 7 import ( 8 "context" 9 "crypto/sha256" 10 "fmt" 11 "os" 12 "path/filepath" 13 "strings" 14 "sync" 15 "time" 16 17 "github.com/powerman/golang-tools/internal/event" 18 "github.com/powerman/golang-tools/internal/lsp/debug/log" 19 "github.com/powerman/golang-tools/internal/lsp/debug/tag" 20 "github.com/powerman/golang-tools/internal/lsp/mod" 21 "github.com/powerman/golang-tools/internal/lsp/protocol" 22 "github.com/powerman/golang-tools/internal/lsp/source" 23 "github.com/powerman/golang-tools/internal/lsp/template" 24 "github.com/powerman/golang-tools/internal/lsp/work" 25 "github.com/powerman/golang-tools/internal/span" 26 "github.com/powerman/golang-tools/internal/xcontext" 27 errors "golang.org/x/xerrors" 28 ) 29 30 // diagnosticSource differentiates different sources of diagnostics. 31 type diagnosticSource int 32 33 const ( 34 modSource diagnosticSource = iota 35 gcDetailsSource 36 analysisSource 37 typeCheckSource 38 orphanedSource 39 workSource 40 ) 41 42 // A diagnosticReport holds results for a single diagnostic source. 43 type diagnosticReport struct { 44 snapshotID uint64 45 publishedHash string 46 diags map[string]*source.Diagnostic 47 } 48 49 // fileReports holds a collection of diagnostic reports for a single file, as 50 // well as the hash of the last published set of diagnostics. 51 type fileReports struct { 52 snapshotID uint64 53 publishedHash string 54 reports map[diagnosticSource]diagnosticReport 55 } 56 57 func (d diagnosticSource) String() string { 58 switch d { 59 case modSource: 60 return "FromSource" 61 case gcDetailsSource: 62 return "FromGCDetails" 63 case analysisSource: 64 return "FromAnalysis" 65 case typeCheckSource: 66 return "FromTypeChecking" 67 case orphanedSource: 68 return "FromOrphans" 69 default: 70 return fmt.Sprintf("From?%d?", d) 71 } 72 } 73 74 // hashDiagnostics computes a hash to identify diags. 75 func hashDiagnostics(diags ...*source.Diagnostic) string { 76 source.SortDiagnostics(diags) 77 h := sha256.New() 78 for _, d := range diags { 79 for _, t := range d.Tags { 80 fmt.Fprintf(h, "%s", t) 81 } 82 for _, r := range d.Related { 83 fmt.Fprintf(h, "%s%s%s", r.URI, r.Message, r.Range) 84 } 85 fmt.Fprintf(h, "%s%s%s%s", d.Message, d.Range, d.Severity, d.Source) 86 } 87 return fmt.Sprintf("%x", h.Sum(nil)) 88 } 89 90 func (s *Server) diagnoseDetached(snapshot source.Snapshot) { 91 ctx := snapshot.BackgroundContext() 92 ctx = xcontext.Detach(ctx) 93 s.diagnose(ctx, snapshot, false) 94 s.publishDiagnostics(ctx, true, snapshot) 95 } 96 97 func (s *Server) diagnoseSnapshots(snapshots map[source.Snapshot][]span.URI, onDisk bool) { 98 var diagnosticWG sync.WaitGroup 99 for snapshot, uris := range snapshots { 100 diagnosticWG.Add(1) 101 go func(snapshot source.Snapshot, uris []span.URI) { 102 defer diagnosticWG.Done() 103 s.diagnoseSnapshot(snapshot, uris, onDisk) 104 }(snapshot, uris) 105 } 106 diagnosticWG.Wait() 107 } 108 109 func (s *Server) diagnoseSnapshot(snapshot source.Snapshot, changedURIs []span.URI, onDisk bool) { 110 ctx := snapshot.BackgroundContext() 111 ctx, done := event.Start(ctx, "Server.diagnoseSnapshot", tag.Snapshot.Of(snapshot.ID())) 112 defer done() 113 114 delay := snapshot.View().Options().DiagnosticsDelay 115 if delay > 0 { 116 // 2-phase diagnostics. 117 // 118 // The first phase just parses and checks packages that have been 119 // affected by file modifications (no analysis). 120 // 121 // The second phase does everything, and is debounced by the configured 122 // delay. 123 s.diagnoseChangedFiles(ctx, snapshot, changedURIs, onDisk) 124 s.publishDiagnostics(ctx, false, snapshot) 125 if ok := <-s.diagDebouncer.debounce(snapshot.View().Name(), snapshot.ID(), time.After(delay)); ok { 126 s.diagnose(ctx, snapshot, false) 127 s.publishDiagnostics(ctx, true, snapshot) 128 } 129 return 130 } 131 132 // Ignore possible workspace configuration warnings in the normal flow. 133 s.diagnose(ctx, snapshot, false) 134 s.publishDiagnostics(ctx, true, snapshot) 135 } 136 137 func (s *Server) diagnoseChangedFiles(ctx context.Context, snapshot source.Snapshot, uris []span.URI, onDisk bool) { 138 ctx, done := event.Start(ctx, "Server.diagnoseChangedFiles", tag.Snapshot.Of(snapshot.ID())) 139 defer done() 140 141 packages := make(map[source.Package]struct{}) 142 for _, uri := range uris { 143 // If the change is only on-disk and the file is not open, don't 144 // directly request its package. It may not be a workspace package. 145 if onDisk && !snapshot.IsOpen(uri) { 146 continue 147 } 148 // If the file is not known to the snapshot (e.g., if it was deleted), 149 // don't diagnose it. 150 if snapshot.FindFile(uri) == nil { 151 continue 152 } 153 // Don't call PackagesForFile for builtin.go, as it results in a 154 // command-line-arguments load. 155 if snapshot.IsBuiltin(ctx, uri) { 156 continue 157 } 158 pkgs, err := snapshot.PackagesForFile(ctx, uri, source.TypecheckFull, false) 159 if err != nil { 160 // TODO (findleyr): we should probably do something with the error here, 161 // but as of now this can fail repeatedly if load fails, so can be too 162 // noisy to log (and we'll handle things later in the slow pass). 163 continue 164 } 165 for _, pkg := range pkgs { 166 packages[pkg] = struct{}{} 167 } 168 } 169 var wg sync.WaitGroup 170 for pkg := range packages { 171 wg.Add(1) 172 173 go func(pkg source.Package) { 174 defer wg.Done() 175 176 s.diagnosePkg(ctx, snapshot, pkg, false) 177 }(pkg) 178 } 179 wg.Wait() 180 } 181 182 // diagnose is a helper function for running diagnostics with a given context. 183 // Do not call it directly. forceAnalysis is only true for testing purposes. 184 func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, forceAnalysis bool) { 185 ctx, done := event.Start(ctx, "Server.diagnose", tag.Snapshot.Of(snapshot.ID())) 186 defer done() 187 188 // Wait for a free diagnostics slot. 189 select { 190 case <-ctx.Done(): 191 return 192 case s.diagnosticsSema <- struct{}{}: 193 } 194 defer func() { 195 <-s.diagnosticsSema 196 }() 197 198 // First, diagnose the go.mod file. 199 modReports, modErr := mod.Diagnostics(ctx, snapshot) 200 if ctx.Err() != nil { 201 log.Trace.Log(ctx, "diagnose cancelled") 202 return 203 } 204 if modErr != nil { 205 event.Error(ctx, "warning: diagnose go.mod", modErr, tag.Directory.Of(snapshot.View().Folder().Filename()), tag.Snapshot.Of(snapshot.ID())) 206 } 207 for id, diags := range modReports { 208 if id.URI == "" { 209 event.Error(ctx, "missing URI for module diagnostics", fmt.Errorf("empty URI"), tag.Directory.Of(snapshot.View().Folder().Filename())) 210 continue 211 } 212 s.storeDiagnostics(snapshot, id.URI, modSource, diags) 213 } 214 215 // Diagnose the go.work file, if it exists. 216 workReports, workErr := work.Diagnostics(ctx, snapshot) 217 if ctx.Err() != nil { 218 log.Trace.Log(ctx, "diagnose cancelled") 219 return 220 } 221 if workErr != nil { 222 event.Error(ctx, "warning: diagnose go.work", workErr, tag.Directory.Of(snapshot.View().Folder().Filename()), tag.Snapshot.Of(snapshot.ID())) 223 } 224 for id, diags := range workReports { 225 if id.URI == "" { 226 event.Error(ctx, "missing URI for work file diagnostics", fmt.Errorf("empty URI"), tag.Directory.Of(snapshot.View().Folder().Filename())) 227 continue 228 } 229 s.storeDiagnostics(snapshot, id.URI, workSource, diags) 230 } 231 232 // Diagnose all of the packages in the workspace. 233 wsPkgs, err := snapshot.ActivePackages(ctx) 234 if s.shouldIgnoreError(ctx, snapshot, err) { 235 return 236 } 237 criticalErr := snapshot.GetCriticalError(ctx) 238 239 // Show the error as a progress error report so that it appears in the 240 // status bar. If a client doesn't support progress reports, the error 241 // will still be shown as a ShowMessage. If there is no error, any running 242 // error progress reports will be closed. 243 s.showCriticalErrorStatus(ctx, snapshot, criticalErr) 244 245 // There may be .tmpl files. 246 for _, f := range snapshot.Templates() { 247 diags := template.Diagnose(f) 248 s.storeDiagnostics(snapshot, f.URI(), typeCheckSource, diags) 249 } 250 251 // If there are no workspace packages, there is nothing to diagnose and 252 // there are no orphaned files. 253 if len(wsPkgs) == 0 { 254 return 255 } 256 257 var ( 258 wg sync.WaitGroup 259 seen = map[span.URI]struct{}{} 260 ) 261 for _, pkg := range wsPkgs { 262 wg.Add(1) 263 264 for _, pgf := range pkg.CompiledGoFiles() { 265 seen[pgf.URI] = struct{}{} 266 } 267 268 go func(pkg source.Package) { 269 defer wg.Done() 270 271 s.diagnosePkg(ctx, snapshot, pkg, forceAnalysis) 272 }(pkg) 273 } 274 wg.Wait() 275 276 // Confirm that every opened file belongs to a package (if any exist in 277 // the workspace). Otherwise, add a diagnostic to the file. 278 for _, o := range s.session.Overlays() { 279 if _, ok := seen[o.URI()]; ok { 280 continue 281 } 282 diagnostic := s.checkForOrphanedFile(ctx, snapshot, o) 283 if diagnostic == nil { 284 continue 285 } 286 s.storeDiagnostics(snapshot, o.URI(), orphanedSource, []*source.Diagnostic{diagnostic}) 287 } 288 } 289 290 func (s *Server) diagnosePkg(ctx context.Context, snapshot source.Snapshot, pkg source.Package, alwaysAnalyze bool) { 291 ctx, done := event.Start(ctx, "Server.diagnosePkg", tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(pkg.ID())) 292 defer done() 293 enableDiagnostics := false 294 includeAnalysis := alwaysAnalyze // only run analyses for packages with open files 295 for _, pgf := range pkg.CompiledGoFiles() { 296 enableDiagnostics = enableDiagnostics || !snapshot.IgnoredFile(pgf.URI) 297 includeAnalysis = includeAnalysis || snapshot.IsOpen(pgf.URI) 298 } 299 // Don't show any diagnostics on ignored files. 300 if !enableDiagnostics { 301 return 302 } 303 304 pkgDiagnostics, err := snapshot.DiagnosePackage(ctx, pkg) 305 if err != nil { 306 event.Error(ctx, "warning: diagnosing package", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(pkg.ID())) 307 return 308 } 309 for _, cgf := range pkg.CompiledGoFiles() { 310 // builtin.go exists only for documentation purposes, and is not valid Go code. 311 // Don't report distracting errors 312 if !snapshot.IsBuiltin(ctx, cgf.URI) { 313 s.storeDiagnostics(snapshot, cgf.URI, typeCheckSource, pkgDiagnostics[cgf.URI]) 314 } 315 } 316 if includeAnalysis && !pkg.HasListOrParseErrors() { 317 reports, err := source.Analyze(ctx, snapshot, pkg, false) 318 if err != nil { 319 event.Error(ctx, "warning: analyzing package", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(pkg.ID())) 320 return 321 } 322 for _, cgf := range pkg.CompiledGoFiles() { 323 s.storeDiagnostics(snapshot, cgf.URI, analysisSource, reports[cgf.URI]) 324 } 325 } 326 327 // If gc optimization details are requested, add them to the 328 // diagnostic reports. 329 s.gcOptimizationDetailsMu.Lock() 330 _, enableGCDetails := s.gcOptimizationDetails[pkg.ID()] 331 s.gcOptimizationDetailsMu.Unlock() 332 if enableGCDetails { 333 gcReports, err := source.GCOptimizationDetails(ctx, snapshot, pkg) 334 if err != nil { 335 event.Error(ctx, "warning: gc details", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(pkg.ID())) 336 } 337 s.gcOptimizationDetailsMu.Lock() 338 _, enableGCDetails := s.gcOptimizationDetails[pkg.ID()] 339 340 // NOTE(golang/go#44826): hold the gcOptimizationDetails lock, and re-check 341 // whether gc optimization details are enabled, while storing gc_details 342 // results. This ensures that the toggling of GC details and clearing of 343 // diagnostics does not race with storing the results here. 344 if enableGCDetails { 345 for id, diags := range gcReports { 346 fh := snapshot.FindFile(id.URI) 347 // Don't publish gc details for unsaved buffers, since the underlying 348 // logic operates on the file on disk. 349 if fh == nil || !fh.Saved() { 350 continue 351 } 352 s.storeDiagnostics(snapshot, id.URI, gcDetailsSource, diags) 353 } 354 } 355 s.gcOptimizationDetailsMu.Unlock() 356 } 357 } 358 359 // storeDiagnostics stores results from a single diagnostic source. If merge is 360 // true, it merges results into any existing results for this snapshot. 361 func (s *Server) storeDiagnostics(snapshot source.Snapshot, uri span.URI, dsource diagnosticSource, diags []*source.Diagnostic) { 362 // Safeguard: ensure that the file actually exists in the snapshot 363 // (see golang.org/issues/38602). 364 fh := snapshot.FindFile(uri) 365 if fh == nil { 366 return 367 } 368 s.diagnosticsMu.Lock() 369 defer s.diagnosticsMu.Unlock() 370 if s.diagnostics[uri] == nil { 371 s.diagnostics[uri] = &fileReports{ 372 publishedHash: hashDiagnostics(), // Hash for 0 diagnostics. 373 reports: map[diagnosticSource]diagnosticReport{}, 374 } 375 } 376 report := s.diagnostics[uri].reports[dsource] 377 // Don't set obsolete diagnostics. 378 if report.snapshotID > snapshot.ID() { 379 return 380 } 381 if report.diags == nil || report.snapshotID != snapshot.ID() { 382 report.diags = map[string]*source.Diagnostic{} 383 } 384 report.snapshotID = snapshot.ID() 385 for _, d := range diags { 386 report.diags[hashDiagnostics(d)] = d 387 } 388 s.diagnostics[uri].reports[dsource] = report 389 } 390 391 // clearDiagnosticSource clears all diagnostics for a given source type. It is 392 // necessary for cases where diagnostics have been invalidated by something 393 // other than a snapshot change, for example when gc_details is toggled. 394 func (s *Server) clearDiagnosticSource(dsource diagnosticSource) { 395 s.diagnosticsMu.Lock() 396 defer s.diagnosticsMu.Unlock() 397 for _, reports := range s.diagnostics { 398 delete(reports.reports, dsource) 399 } 400 } 401 402 const WorkspaceLoadFailure = "Error loading workspace" 403 404 // showCriticalErrorStatus shows the error as a progress report. 405 // If the error is nil, it clears any existing error progress report. 406 func (s *Server) showCriticalErrorStatus(ctx context.Context, snapshot source.Snapshot, err *source.CriticalError) { 407 s.criticalErrorStatusMu.Lock() 408 defer s.criticalErrorStatusMu.Unlock() 409 410 // Remove all newlines so that the error message can be formatted in a 411 // status bar. 412 var errMsg string 413 if err != nil { 414 event.Error(ctx, "errors loading workspace", err.MainError, tag.Snapshot.Of(snapshot.ID()), tag.Directory.Of(snapshot.View().Folder())) 415 for _, d := range err.DiagList { 416 s.storeDiagnostics(snapshot, d.URI, modSource, []*source.Diagnostic{d}) 417 } 418 errMsg = strings.ReplaceAll(err.MainError.Error(), "\n", " ") 419 } 420 421 if s.criticalErrorStatus == nil { 422 if errMsg != "" { 423 s.criticalErrorStatus = s.progress.Start(ctx, WorkspaceLoadFailure, errMsg, nil, nil) 424 } 425 return 426 } 427 428 // If an error is already shown to the user, update it or mark it as 429 // resolved. 430 if errMsg == "" { 431 s.criticalErrorStatus.End("Done.") 432 s.criticalErrorStatus = nil 433 } else { 434 s.criticalErrorStatus.Report(errMsg, 0) 435 } 436 } 437 438 // checkForOrphanedFile checks that the given URIs can be mapped to packages. 439 // If they cannot and the workspace is not otherwise unloaded, it also surfaces 440 // a warning, suggesting that the user check the file for build tags. 441 func (s *Server) checkForOrphanedFile(ctx context.Context, snapshot source.Snapshot, fh source.VersionedFileHandle) *source.Diagnostic { 442 if snapshot.View().FileKind(fh) != source.Go { 443 return nil 444 } 445 // builtin files won't have a package, but they are never orphaned. 446 if snapshot.IsBuiltin(ctx, fh.URI()) { 447 return nil 448 } 449 pkgs, err := snapshot.PackagesForFile(ctx, fh.URI(), source.TypecheckWorkspace, false) 450 if len(pkgs) > 0 || err == nil { 451 return nil 452 } 453 pgf, err := snapshot.ParseGo(ctx, fh, source.ParseHeader) 454 if err != nil { 455 return nil 456 } 457 spn, err := span.NewRange(snapshot.FileSet(), pgf.File.Name.Pos(), pgf.File.Name.End()).Span() 458 if err != nil { 459 return nil 460 } 461 rng, err := pgf.Mapper.Range(spn) 462 if err != nil { 463 return nil 464 } 465 // If the file no longer has a name ending in .go, this diagnostic is wrong 466 if filepath.Ext(fh.URI().Filename()) != ".go" { 467 return nil 468 } 469 // TODO(rstambler): We should be able to parse the build tags in the 470 // file and show a more specific error message. For now, put the diagnostic 471 // on the package declaration. 472 return &source.Diagnostic{ 473 URI: fh.URI(), 474 Range: rng, 475 Severity: protocol.SeverityWarning, 476 Source: source.ListError, 477 Message: fmt.Sprintf(`No packages found for open file %s: %v. 478 If this file contains build tags, try adding "-tags=<build tag>" to your gopls "buildFlags" configuration (see (https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags-string). 479 Otherwise, see the troubleshooting guidelines for help investigating (https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md). 480 `, fh.URI().Filename(), err), 481 } 482 } 483 484 // publishDiagnostics collects and publishes any unpublished diagnostic reports. 485 func (s *Server) publishDiagnostics(ctx context.Context, final bool, snapshot source.Snapshot) { 486 ctx, done := event.Start(ctx, "Server.publishDiagnostics", tag.Snapshot.Of(snapshot.ID())) 487 defer done() 488 s.diagnosticsMu.Lock() 489 defer s.diagnosticsMu.Unlock() 490 491 published := 0 492 defer func() { 493 log.Trace.Logf(ctx, "published %d diagnostics", published) 494 }() 495 496 for uri, r := range s.diagnostics { 497 // Snapshot IDs are always increasing, so we use them instead of file 498 // versions to create the correct order for diagnostics. 499 500 // If we've already delivered diagnostics for a future snapshot for this 501 // file, do not deliver them. 502 if r.snapshotID > snapshot.ID() { 503 continue 504 } 505 anyReportsChanged := false 506 reportHashes := map[diagnosticSource]string{} 507 var diags []*source.Diagnostic 508 for dsource, report := range r.reports { 509 if report.snapshotID != snapshot.ID() { 510 continue 511 } 512 var reportDiags []*source.Diagnostic 513 for _, d := range report.diags { 514 diags = append(diags, d) 515 reportDiags = append(reportDiags, d) 516 } 517 hash := hashDiagnostics(reportDiags...) 518 if hash != report.publishedHash { 519 anyReportsChanged = true 520 } 521 reportHashes[dsource] = hash 522 } 523 524 if !final && !anyReportsChanged { 525 // Don't invalidate existing reports on the client if we haven't got any 526 // new information. 527 continue 528 } 529 source.SortDiagnostics(diags) 530 hash := hashDiagnostics(diags...) 531 if hash == r.publishedHash { 532 // Update snapshotID to be the latest snapshot for which this diagnostic 533 // hash is valid. 534 r.snapshotID = snapshot.ID() 535 continue 536 } 537 var version int32 538 if fh := snapshot.FindFile(uri); fh != nil { // file may have been deleted 539 version = fh.Version() 540 } 541 if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{ 542 Diagnostics: toProtocolDiagnostics(diags), 543 URI: protocol.URIFromSpanURI(uri), 544 Version: version, 545 }); err == nil { 546 published++ 547 r.publishedHash = hash 548 r.snapshotID = snapshot.ID() 549 for dsource, hash := range reportHashes { 550 report := r.reports[dsource] 551 report.publishedHash = hash 552 r.reports[dsource] = report 553 } 554 } else { 555 if ctx.Err() != nil { 556 // Publish may have failed due to a cancelled context. 557 log.Trace.Log(ctx, "publish cancelled") 558 return 559 } 560 event.Error(ctx, "publishReports: failed to deliver diagnostic", err, tag.URI.Of(uri)) 561 } 562 } 563 } 564 565 func toProtocolDiagnostics(diagnostics []*source.Diagnostic) []protocol.Diagnostic { 566 reports := []protocol.Diagnostic{} 567 for _, diag := range diagnostics { 568 related := make([]protocol.DiagnosticRelatedInformation, 0, len(diag.Related)) 569 for _, rel := range diag.Related { 570 related = append(related, protocol.DiagnosticRelatedInformation{ 571 Location: protocol.Location{ 572 URI: protocol.URIFromSpanURI(rel.URI), 573 Range: rel.Range, 574 }, 575 Message: rel.Message, 576 }) 577 } 578 pdiag := protocol.Diagnostic{ 579 // diag.Message might start with \n or \t 580 Message: strings.TrimSpace(diag.Message), 581 Range: diag.Range, 582 Severity: diag.Severity, 583 Source: string(diag.Source), 584 Tags: diag.Tags, 585 RelatedInformation: related, 586 } 587 if diag.Code != "" { 588 pdiag.Code = diag.Code 589 } 590 if diag.CodeHref != "" { 591 pdiag.CodeDescription = &protocol.CodeDescription{Href: diag.CodeHref} 592 } 593 reports = append(reports, pdiag) 594 } 595 return reports 596 } 597 598 func (s *Server) shouldIgnoreError(ctx context.Context, snapshot source.Snapshot, err error) bool { 599 if err == nil { // if there is no error at all 600 return false 601 } 602 if errors.Is(err, context.Canceled) { 603 return true 604 } 605 // If the folder has no Go code in it, we shouldn't spam the user with a warning. 606 var hasGo bool 607 _ = filepath.Walk(snapshot.View().Folder().Filename(), func(path string, info os.FileInfo, err error) error { 608 if err != nil { 609 return err 610 } 611 if !strings.HasSuffix(info.Name(), ".go") { 612 return nil 613 } 614 hasGo = true 615 return errors.New("done") 616 }) 617 return !hasGo 618 } 619 620 // Diagnostics formattedfor the debug server 621 // (all the relevant fields of Server are private) 622 // (The alternative is to export them) 623 func (s *Server) Diagnostics() map[string][]string { 624 ans := make(map[string][]string) 625 s.diagnosticsMu.Lock() 626 defer s.diagnosticsMu.Unlock() 627 for k, v := range s.diagnostics { 628 fn := k.Filename() 629 for typ, d := range v.reports { 630 if len(d.diags) == 0 { 631 continue 632 } 633 for _, dx := range d.diags { 634 ans[fn] = append(ans[fn], auxStr(dx, d, typ)) 635 } 636 } 637 } 638 return ans 639 } 640 641 func auxStr(v *source.Diagnostic, d diagnosticReport, typ diagnosticSource) string { 642 // Tags? RelatedInformation? 643 msg := fmt.Sprintf("(%s)%q(source:%q,code:%q,severity:%s,snapshot:%d,type:%s)", 644 v.Range, v.Message, v.Source, v.Code, v.Severity, d.snapshotID, typ) 645 for _, r := range v.Related { 646 msg += fmt.Sprintf(" [%s:%s,%q]", r.URI.Filename(), r.Range, r.Message) 647 } 648 return msg 649 }