| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"slices" |
| 15 |
"sort" |
| 16 |
"strings" |
| 17 |
"sync" |
| 18 |
"time" |
| 19 |
|
| 20 |
"golang.org/x/tools/gopls/internal/cache" |
| 21 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 22 |
"golang.org/x/tools/gopls/internal/file" |
| 23 |
"golang.org/x/tools/gopls/internal/golang" |
| 24 |
"golang.org/x/tools/gopls/internal/label" |
| 25 |
"golang.org/x/tools/gopls/internal/mod" |
| 26 |
"golang.org/x/tools/gopls/internal/protocol" |
| 27 |
"golang.org/x/tools/gopls/internal/settings" |
| 28 |
"golang.org/x/tools/gopls/internal/template" |
| 29 |
"golang.org/x/tools/gopls/internal/util/moremaps" |
| 30 |
"golang.org/x/tools/gopls/internal/work" |
| 31 |
"golang.org/x/tools/internal/event" |
| 32 |
"golang.org/x/tools/internal/event/keys" |
| 33 |
"golang.org/x/tools/internal/jsonrpc2" |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
func (s *server) Diagnostic(ctx context.Context, params *protocol.DocumentDiagnosticParams) (*protocol.DocumentDiagnosticReport, error) { |
| 47 |
ctx, done := event.Start(ctx, "server.Diagnostic") |
| 48 |
defer done() |
| 49 |
|
| 50 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
defer release() |
| 55 |
|
| 56 |
jsonrpc2.Async(ctx) |
| 57 |
|
| 58 |
uri := fh.URI() |
| 59 |
kind := snapshot.FileKind(fh) |
| 60 |
var diagnostics []*cache.Diagnostic |
| 61 |
switch kind { |
| 62 |
case file.Go: |
| 63 |
diagnostics, err = golang.DiagnoseFile(ctx, snapshot, uri) |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
default: |
| 68 |
return nil, fmt.Errorf("pull diagnostics not supported for this file kind") |
| 69 |
} |
| 70 |
return &protocol.DocumentDiagnosticReport{ |
| 71 |
Value: protocol.RelatedFullDocumentDiagnosticReport{ |
| 72 |
FullDocumentDiagnosticReport: protocol.FullDocumentDiagnosticReport{ |
| 73 |
Items: cache.ToProtocolDiagnostics(diagnostics...), |
| 74 |
}, |
| 75 |
}, |
| 76 |
}, nil |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
type fileDiagnostics struct { |
| 81 |
publishedHash file.Hash |
| 82 |
mustPublish bool |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
orphanedAt uint64 |
| 88 |
orphanedFileDiagnostics []*cache.Diagnostic |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
byView map[*cache.View]viewDiagnostics |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
type viewDiagnostics struct { |
| 98 |
snapshot uint64 |
| 99 |
version int32 |
| 100 |
diagnostics []*cache.Diagnostic |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
type ( |
| 105 |
viewSet = map[*cache.View]unit |
| 106 |
diagMap = map[protocol.DocumentURI][]*cache.Diagnostic |
| 107 |
) |
| 108 |
|
| 109 |
func sortDiagnostics(d []*cache.Diagnostic) { |
| 110 |
sort.Slice(d, func(i int, j int) bool { |
| 111 |
a, b := d[i], d[j] |
| 112 |
if r := protocol.CompareRange(a.Range, b.Range); r != 0 { |
| 113 |
return r < 0 |
| 114 |
} |
| 115 |
if a.Source != b.Source { |
| 116 |
return a.Source < b.Source |
| 117 |
} |
| 118 |
return a.Message < b.Message |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
func (s *server) diagnoseChangedViews(ctx context.Context, modID uint64, lastChange map[*cache.View][]protocol.DocumentURI, cause ModificationSource) { |
| 123 |
|
| 124 |
s.modificationMu.Lock() |
| 125 |
needsDiagnosis := moremaps.KeySlice(s.viewsToDiagnose) |
| 126 |
s.modificationMu.Unlock() |
| 127 |
|
| 128 |
|
| 129 |
var wg sync.WaitGroup |
| 130 |
for _, v := range needsDiagnosis { |
| 131 |
snapshot, release, err := v.Snapshot() |
| 132 |
if err != nil { |
| 133 |
s.modificationMu.Lock() |
| 134 |
|
| 135 |
|
| 136 |
delete(s.viewsToDiagnose, v) |
| 137 |
s.modificationMu.Unlock() |
| 138 |
continue |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
uris := lastChange[v] |
| 145 |
if snapshot.Options().DiagnosticsTrigger == settings.DiagnosticsOnSave && cause == FromDidChange { |
| 146 |
|
| 147 |
|
| 148 |
release() |
| 149 |
continue |
| 150 |
} |
| 151 |
|
| 152 |
wg.Add(1) |
| 153 |
go func(snapshot *cache.Snapshot, uris []protocol.DocumentURI) { |
| 154 |
defer release() |
| 155 |
defer wg.Done() |
| 156 |
s.diagnoseSnapshot(ctx, snapshot, uris, snapshot.Options().DiagnosticsDelay) |
| 157 |
s.modificationMu.Lock() |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if ctx.Err() == nil && s.viewsToDiagnose[v] <= modID { |
| 166 |
delete(s.viewsToDiagnose, v) |
| 167 |
} |
| 168 |
s.modificationMu.Unlock() |
| 169 |
}(snapshot, uris) |
| 170 |
} |
| 171 |
|
| 172 |
wg.Wait() |
| 173 |
|
| 174 |
|
| 175 |
orphanedFileDiagnostics, err := s.session.OrphanedFileDiagnostics(ctx) |
| 176 |
if err == nil { |
| 177 |
err = s.updateOrphanedFileDiagnostics(ctx, modID, orphanedFileDiagnostics) |
| 178 |
} |
| 179 |
if err != nil { |
| 180 |
if ctx.Err() == nil { |
| 181 |
event.Error(ctx, "warning: while diagnosing orphaned files", err) |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
func (s *server) diagnoseSnapshot(ctx context.Context, snapshot *cache.Snapshot, changedURIs []protocol.DocumentURI, delay time.Duration) { |
| 202 |
ctx, done := event.Start(ctx, "server.diagnoseSnapshot", snapshot.Labels()...) |
| 203 |
defer done() |
| 204 |
|
| 205 |
if delay > 0 { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
if len(changedURIs) > 0 { |
| 215 |
diagnostics, err := diagnoseChangedFiles(ctx, snapshot, changedURIs) |
| 216 |
if err != nil { |
| 217 |
if ctx.Err() == nil { |
| 218 |
event.Error(ctx, "warning: while diagnosing changed files", err, snapshot.Labels()...) |
| 219 |
} |
| 220 |
return |
| 221 |
} |
| 222 |
s.updateDiagnostics(ctx, snapshot, diagnostics, false) |
| 223 |
} |
| 224 |
|
| 225 |
select { |
| 226 |
case <-time.After(delay): |
| 227 |
case <-ctx.Done(): |
| 228 |
return |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
diagnostics, err := s.diagnose(ctx, snapshot) |
| 233 |
if err != nil { |
| 234 |
if ctx.Err() == nil { |
| 235 |
event.Error(ctx, "warning: while diagnosing snapshot", err, snapshot.Labels()...) |
| 236 |
} |
| 237 |
return |
| 238 |
} |
| 239 |
s.updateDiagnostics(ctx, snapshot, diagnostics, true) |
| 240 |
} |
| 241 |
|
| 242 |
func diagnoseChangedFiles(ctx context.Context, snapshot *cache.Snapshot, uris []protocol.DocumentURI) (diagMap, error) { |
| 243 |
ctx, done := event.Start(ctx, "server.diagnoseChangedFiles", snapshot.Labels()...) |
| 244 |
defer done() |
| 245 |
|
| 246 |
toDiagnose := make(map[metadata.PackageID]*metadata.Package) |
| 247 |
for _, uri := range uris { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if !snapshot.IsOpen(uri) { |
| 256 |
continue |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
if fh, err := snapshot.ReadFile(ctx, uri); err != nil || fh == nil { |
| 261 |
continue |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
if snapshot.IsBuiltin(uri) { |
| 266 |
continue |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
if snapshot.IgnoredFile(uri) { |
| 271 |
continue |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
meta, err := snapshot.NarrowestMetadataForFile(ctx, uri) |
| 276 |
if err != nil { |
| 277 |
if ctx.Err() != nil { |
| 278 |
return nil, ctx.Err() |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
continue |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if snapshot.IsWorkspacePackage(meta.ID) { |
| 289 |
toDiagnose[meta.ID] = meta |
| 290 |
} |
| 291 |
} |
| 292 |
diags, err := snapshot.PackageDiagnostics(ctx, moremaps.KeySlice(toDiagnose)...) |
| 293 |
if err != nil { |
| 294 |
if ctx.Err() == nil { |
| 295 |
event.Error(ctx, "warning: diagnostics failed", err, snapshot.Labels()...) |
| 296 |
} |
| 297 |
return nil, err |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
for _, meta := range toDiagnose { |
| 303 |
for _, uri := range meta.CompiledGoFiles { |
| 304 |
if _, ok := diags[uri]; !ok { |
| 305 |
diags[uri] = nil |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
return diags, nil |
| 310 |
} |
| 311 |
|
| 312 |
func (s *server) diagnose(ctx context.Context, snapshot *cache.Snapshot) (diagMap, error) { |
| 313 |
ctx, done := event.Start(ctx, "server.diagnose", snapshot.Labels()...) |
| 314 |
defer done() |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
select { |
| 322 |
case <-ctx.Done(): |
| 323 |
return nil, ctx.Err() |
| 324 |
case s.diagnosticsSema <- struct{}{}: |
| 325 |
} |
| 326 |
defer func() { |
| 327 |
<-s.diagnosticsSema |
| 328 |
}() |
| 329 |
|
| 330 |
var ( |
| 331 |
diagnosticsMu sync.Mutex |
| 332 |
diagnostics = make(diagMap) |
| 333 |
) |
| 334 |
|
| 335 |
store := func(operation string, diagsByFile diagMap, err error) { |
| 336 |
if err != nil { |
| 337 |
if ctx.Err() == nil { |
| 338 |
event.Error(ctx, "warning: while "+operation, err, snapshot.Labels()...) |
| 339 |
} |
| 340 |
return |
| 341 |
} |
| 342 |
diagnosticsMu.Lock() |
| 343 |
defer diagnosticsMu.Unlock() |
| 344 |
for uri, diags := range diagsByFile { |
| 345 |
diagnostics[uri] = append(diagnostics[uri], diags...) |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
workReports, workErr := work.Diagnostics(ctx, snapshot) |
| 354 |
if ctx.Err() != nil { |
| 355 |
return nil, ctx.Err() |
| 356 |
} |
| 357 |
store("diagnosing go.work file", workReports, workErr) |
| 358 |
|
| 359 |
|
| 360 |
modReports, modErr := mod.ParseDiagnostics(ctx, snapshot) |
| 361 |
if ctx.Err() != nil { |
| 362 |
return nil, ctx.Err() |
| 363 |
} |
| 364 |
store("diagnosing go.mod file", modReports, modErr) |
| 365 |
|
| 366 |
|
| 367 |
upgradeReports, upgradeErr := mod.UpgradeDiagnostics(ctx, snapshot) |
| 368 |
if ctx.Err() != nil { |
| 369 |
return nil, ctx.Err() |
| 370 |
} |
| 371 |
store("diagnosing go.mod upgrades", upgradeReports, upgradeErr) |
| 372 |
|
| 373 |
|
| 374 |
vulnReports, vulnErr := mod.VulnerabilityDiagnostics(ctx, snapshot) |
| 375 |
if ctx.Err() != nil { |
| 376 |
return nil, ctx.Err() |
| 377 |
} |
| 378 |
store("diagnosing vulnerabilities", vulnReports, vulnErr) |
| 379 |
|
| 380 |
workspacePkgs, err := snapshot.WorkspaceMetadata(ctx) |
| 381 |
if s.shouldIgnoreError(snapshot, err) { |
| 382 |
return diagnostics, ctx.Err() |
| 383 |
} |
| 384 |
|
| 385 |
initialErr := snapshot.InitializationError() |
| 386 |
if ctx.Err() != nil { |
| 387 |
|
| 388 |
return nil, ctx.Err() |
| 389 |
} |
| 390 |
|
| 391 |
if initialErr != nil { |
| 392 |
store("critical error", initialErr.Diagnostics, nil) |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
statusErr := initialErr |
| 400 |
if len(snapshot.Overlays()) == 0 { |
| 401 |
|
| 402 |
|
| 403 |
statusErr = nil |
| 404 |
} |
| 405 |
s.updateCriticalErrorStatus(ctx, snapshot, statusErr) |
| 406 |
|
| 407 |
|
| 408 |
tmplReports := template.Diagnostics(snapshot) |
| 409 |
|
| 410 |
|
| 411 |
store("diagnosing templates", tmplReports, nil) |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if len(workspacePkgs) == 0 { |
| 416 |
return diagnostics, nil |
| 417 |
} |
| 418 |
|
| 419 |
var wg sync.WaitGroup |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
wg.Go(func() { |
| 425 |
modTidyReports, err := mod.TidyDiagnostics(ctx, snapshot) |
| 426 |
store("running go mod tidy", modTidyReports, err) |
| 427 |
}) |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
var ( |
| 449 |
toDiagnose = make(map[metadata.PackageID]*metadata.Package) |
| 450 |
toAnalyze = make(map[metadata.PackageID]*metadata.Package) |
| 451 |
|
| 452 |
|
| 453 |
toAnalyzeWidest = make(map[golang.PackagePath]*metadata.Package) |
| 454 |
) |
| 455 |
for _, mp := range workspacePkgs { |
| 456 |
var hasNonIgnored, hasOpenFile bool |
| 457 |
for _, uri := range mp.CompiledGoFiles { |
| 458 |
if !hasNonIgnored && !snapshot.IgnoredFile(uri) { |
| 459 |
hasNonIgnored = true |
| 460 |
} |
| 461 |
if !hasOpenFile && snapshot.IsOpen(uri) { |
| 462 |
hasOpenFile = true |
| 463 |
} |
| 464 |
} |
| 465 |
if hasNonIgnored { |
| 466 |
toDiagnose[mp.ID] = mp |
| 467 |
if hasOpenFile { |
| 468 |
if prev, ok := toAnalyzeWidest[mp.PkgPath]; ok { |
| 469 |
if len(prev.CompiledGoFiles) >= len(mp.CompiledGoFiles) { |
| 470 |
|
| 471 |
continue |
| 472 |
} |
| 473 |
|
| 474 |
delete(toAnalyze, prev.ID) |
| 475 |
} |
| 476 |
toAnalyze[mp.ID] = mp |
| 477 |
toAnalyzeWidest[mp.PkgPath] = mp |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
wg.Go(func() { |
| 483 |
compilerOptDetailsDiags, err := s.compilerOptDetailsDiagnostics(ctx, snapshot, toDiagnose) |
| 484 |
store("collecting compiler optimization details", compilerOptDetailsDiags, err) |
| 485 |
}) |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
var pkgDiags, analysisDiags diagMap |
| 490 |
|
| 491 |
wg.Go(func() { |
| 492 |
var err error |
| 493 |
pkgDiags, err = snapshot.PackageDiagnostics(ctx, moremaps.KeySlice(toDiagnose)...) |
| 494 |
if err != nil { |
| 495 |
event.Error(ctx, "warning: diagnostics failed", err, snapshot.Labels()...) |
| 496 |
} |
| 497 |
}) |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
wg.Go(func() { |
| 502 |
var err error |
| 503 |
|
| 504 |
|
| 505 |
analysisDiags, err = golang.Analyze(ctx, snapshot, toAnalyze, s.progress) |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
for uri, diags := range analysisDiags { |
| 512 |
if !snapshot.IsOpen(uri) { |
| 513 |
newDiags := slices.DeleteFunc(diags, func(diag *cache.Diagnostic) bool { |
| 514 |
return diag.Severity == protocol.SeverityHint |
| 515 |
}) |
| 516 |
if len(newDiags) == 0 { |
| 517 |
delete(analysisDiags, uri) |
| 518 |
} else { |
| 519 |
analysisDiags[uri] = newDiags |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
if err != nil { |
| 525 |
event.Error(ctx, "warning: analyzing package", err, append(snapshot.Labels(), label.Package.Of(keys.Join(moremaps.KeySlice(toDiagnose))))...) |
| 526 |
return |
| 527 |
} |
| 528 |
}) |
| 529 |
|
| 530 |
wg.Wait() |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
combinedDiags := make(diagMap) |
| 535 |
for uri, adiags := range analysisDiags { |
| 536 |
tdiags := pkgDiags[uri] |
| 537 |
combinedDiags[uri] = golang.CombineDiagnostics(tdiags, adiags) |
| 538 |
} |
| 539 |
for uri, tdiags := range pkgDiags { |
| 540 |
if _, ok := combinedDiags[uri]; !ok { |
| 541 |
combinedDiags[uri] = tdiags |
| 542 |
} |
| 543 |
} |
| 544 |
store("type checking and analysing", combinedDiags, nil) |
| 545 |
|
| 546 |
return diagnostics, nil |
| 547 |
} |
| 548 |
|
| 549 |
func (s *server) compilerOptDetailsDiagnostics(ctx context.Context, snapshot *cache.Snapshot, toDiagnose map[metadata.PackageID]*metadata.Package) (diagMap, error) { |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
diagnostics := make(diagMap) |
| 556 |
seenDirs := make(map[protocol.DocumentURI]bool) |
| 557 |
for _, mp := range toDiagnose { |
| 558 |
if len(mp.CompiledGoFiles) == 0 { |
| 559 |
continue |
| 560 |
} |
| 561 |
dir := mp.CompiledGoFiles[0].Dir() |
| 562 |
if snapshot.WantCompilerOptDetails(dir) { |
| 563 |
if !seenDirs[dir] { |
| 564 |
seenDirs[dir] = true |
| 565 |
|
| 566 |
perFileDiags, err := golang.CompilerOptDetails(ctx, snapshot, dir) |
| 567 |
if err != nil { |
| 568 |
event.Error(ctx, "warning: compiler optimization details", err, append(snapshot.Labels(), label.URI.Of(dir))...) |
| 569 |
continue |
| 570 |
} |
| 571 |
for uri, diags := range perFileDiags { |
| 572 |
diagnostics[uri] = append(diagnostics[uri], diags...) |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
return diagnostics, nil |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
func (s *server) mustPublishDiagnostics(uri protocol.DocumentURI) { |
| 586 |
s.diagnosticsMu.Lock() |
| 587 |
defer s.diagnosticsMu.Unlock() |
| 588 |
|
| 589 |
if s.diagnostics[uri] == nil { |
| 590 |
s.diagnostics[uri] = new(fileDiagnostics) |
| 591 |
} |
| 592 |
s.diagnostics[uri].mustPublish = true |
| 593 |
} |
| 594 |
|
| 595 |
const WorkspaceLoadFailure = "Error loading workspace" |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
func (s *server) updateCriticalErrorStatus(ctx context.Context, snapshot *cache.Snapshot, err *cache.InitializationError) { |
| 603 |
s.criticalErrorStatusMu.Lock() |
| 604 |
defer s.criticalErrorStatusMu.Unlock() |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
var errMsg string |
| 609 |
if err != nil { |
| 610 |
errMsg = strings.ReplaceAll(err.MainError.Error(), "\n", " ") |
| 611 |
} |
| 612 |
|
| 613 |
if s.criticalErrorStatus == nil { |
| 614 |
if errMsg != "" { |
| 615 |
event.Error(ctx, "errors loading workspace", err.MainError, snapshot.Labels()...) |
| 616 |
s.criticalErrorStatus = s.progress.Start(ctx, WorkspaceLoadFailure, errMsg, nil, nil) |
| 617 |
} |
| 618 |
return |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
if errMsg == "" { |
| 624 |
s.criticalErrorStatus.End(ctx, "Done.") |
| 625 |
s.criticalErrorStatus = nil |
| 626 |
} else { |
| 627 |
s.criticalErrorStatus.Report(ctx, errMsg, 0) |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
func (s *server) updateDiagnostics(ctx context.Context, snapshot *cache.Snapshot, diagnostics diagMap, final bool) { |
| 634 |
ctx, done := event.Start(ctx, "server.publishDiagnostics") |
| 635 |
defer done() |
| 636 |
|
| 637 |
s.diagnosticsMu.Lock() |
| 638 |
defer s.diagnosticsMu.Unlock() |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if ctx.Err() != nil { |
| 653 |
return |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
viewMap := make(viewSet) |
| 662 |
for _, v := range s.session.Views() { |
| 663 |
viewMap[v] = unit{} |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
updateAndPublish := func(uri protocol.DocumentURI, f *fileDiagnostics, diags []*cache.Diagnostic) error { |
| 670 |
current, ok := f.byView[snapshot.View()] |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
if !ok || current.snapshot < snapshot.SequenceID() || (current.snapshot == snapshot.SequenceID() && final) { |
| 680 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 681 |
if err != nil { |
| 682 |
return err |
| 683 |
} |
| 684 |
current = viewDiagnostics{ |
| 685 |
snapshot: snapshot.SequenceID(), |
| 686 |
version: fh.Version(), |
| 687 |
diagnostics: diags, |
| 688 |
} |
| 689 |
if f.byView == nil { |
| 690 |
f.byView = make(map[*cache.View]viewDiagnostics) |
| 691 |
} |
| 692 |
f.byView[snapshot.View()] = current |
| 693 |
} |
| 694 |
|
| 695 |
return s.publishFileDiagnosticsLocked(ctx, viewMap, uri, current.version, f) |
| 696 |
} |
| 697 |
|
| 698 |
seen := make(map[protocol.DocumentURI]bool) |
| 699 |
for uri, diags := range diagnostics { |
| 700 |
f, ok := s.diagnostics[uri] |
| 701 |
if !ok { |
| 702 |
f = new(fileDiagnostics) |
| 703 |
s.diagnostics[uri] = f |
| 704 |
} |
| 705 |
seen[uri] = true |
| 706 |
if err := updateAndPublish(uri, f, diags); err != nil { |
| 707 |
if ctx.Err() != nil { |
| 708 |
return |
| 709 |
} else { |
| 710 |
event.Error(ctx, "updateDiagnostics: failed to deliver diagnostics", err, label.URI.Of(uri)) |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
if final { |
| 720 |
for uri, f := range s.diagnostics { |
| 721 |
if !seen[uri] { |
| 722 |
if err := updateAndPublish(uri, f, nil); err != nil { |
| 723 |
if ctx.Err() != nil { |
| 724 |
return |
| 725 |
} else { |
| 726 |
event.Error(ctx, "updateDiagnostics: failed to deliver diagnostics", err, label.URI.Of(uri)) |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
func (s *server) updateOrphanedFileDiagnostics(ctx context.Context, modID uint64, diagnostics diagMap) error { |
| 737 |
views := s.session.Views() |
| 738 |
viewSet := make(viewSet) |
| 739 |
for _, v := range views { |
| 740 |
viewSet[v] = unit{} |
| 741 |
} |
| 742 |
|
| 743 |
s.diagnosticsMu.Lock() |
| 744 |
defer s.diagnosticsMu.Unlock() |
| 745 |
|
| 746 |
for uri, diags := range diagnostics { |
| 747 |
f, ok := s.diagnostics[uri] |
| 748 |
if !ok { |
| 749 |
f = new(fileDiagnostics) |
| 750 |
s.diagnostics[uri] = f |
| 751 |
} |
| 752 |
if f.orphanedAt > modID { |
| 753 |
continue |
| 754 |
} |
| 755 |
f.orphanedAt = modID |
| 756 |
f.orphanedFileDiagnostics = diags |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
fh, err := s.session.ReadFile(ctx, uri) |
| 761 |
if err != nil { |
| 762 |
return err |
| 763 |
} |
| 764 |
if err := s.publishFileDiagnosticsLocked(ctx, viewSet, uri, fh.Version(), f); err != nil { |
| 765 |
return err |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
for uri, f := range s.diagnostics { |
| 771 |
if f.orphanedAt < modID { |
| 772 |
f.orphanedFileDiagnostics = nil |
| 773 |
} |
| 774 |
fh, err := s.session.ReadFile(ctx, uri) |
| 775 |
if err != nil { |
| 776 |
return err |
| 777 |
} |
| 778 |
if err := s.publishFileDiagnosticsLocked(ctx, viewSet, uri, fh.Version(), f); err != nil { |
| 779 |
return err |
| 780 |
} |
| 781 |
} |
| 782 |
return nil |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
func (s *server) publishFileDiagnosticsLocked(ctx context.Context, views viewSet, uri protocol.DocumentURI, version int32, f *fileDiagnostics) error { |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
type diagSuffix struct { |
| 793 |
diag *cache.Diagnostic |
| 794 |
suffix string |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
diagSuffixes := make(map[file.Hash][]diagSuffix) |
| 799 |
add := func(diag *cache.Diagnostic, suffix string) { |
| 800 |
h := diag.Hash() |
| 801 |
diagSuffixes[h] = append(diagSuffixes[h], diagSuffix{diag, suffix}) |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
for _, diag := range f.orphanedFileDiagnostics { |
| 806 |
add(diag, "") |
| 807 |
} |
| 808 |
|
| 809 |
var allViews []*cache.View |
| 810 |
for view, viewDiags := range f.byView { |
| 811 |
if _, ok := views[view]; !ok { |
| 812 |
delete(f.byView, view) |
| 813 |
continue |
| 814 |
} |
| 815 |
if viewDiags.version != version { |
| 816 |
continue |
| 817 |
} |
| 818 |
allViews = append(allViews, view) |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
relevantViews, err := cache.RelevantViews(ctx, s.session, uri, allViews) |
| 829 |
if err != nil { |
| 830 |
return err |
| 831 |
} |
| 832 |
|
| 833 |
if len(relevantViews) == 0 { |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
relevantViews = allViews |
| 842 |
} |
| 843 |
|
| 844 |
for _, view := range relevantViews { |
| 845 |
viewDiags := f.byView[view] |
| 846 |
|
| 847 |
var suffix string |
| 848 |
{ |
| 849 |
var words []string |
| 850 |
if view.GOOS() != runtime.GOOS { |
| 851 |
words = append(words, view.GOOS()) |
| 852 |
} |
| 853 |
if view.GOARCH() != runtime.GOARCH { |
| 854 |
words = append(words, view.GOARCH()) |
| 855 |
} |
| 856 |
if len(words) > 0 { |
| 857 |
suffix = fmt.Sprintf(" [%s]", strings.Join(words, ",")) |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
for _, diag := range viewDiags.diagnostics { |
| 862 |
add(diag, suffix) |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
var ( |
| 868 |
hash file.Hash |
| 869 |
unique []*cache.Diagnostic |
| 870 |
) |
| 871 |
for h, items := range diagSuffixes { |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
sort.Slice(items, func(i, j int) bool { |
| 876 |
return items[i].suffix < items[j].suffix |
| 877 |
}) |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
first := items[0] |
| 882 |
if first.suffix != "" { |
| 883 |
diag2 := *first.diag |
| 884 |
diag2.Message += first.suffix |
| 885 |
first.diag = &diag2 |
| 886 |
h = diag2.Hash() |
| 887 |
} |
| 888 |
|
| 889 |
hash.XORWith(h) |
| 890 |
unique = append(unique, first.diag) |
| 891 |
} |
| 892 |
sortDiagnostics(unique) |
| 893 |
|
| 894 |
|
| 895 |
if hash != f.publishedHash || f.mustPublish { |
| 896 |
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{ |
| 897 |
Diagnostics: cache.ToProtocolDiagnostics(unique...), |
| 898 |
URI: uri, |
| 899 |
Version: version, |
| 900 |
}); err != nil { |
| 901 |
return err |
| 902 |
} |
| 903 |
f.publishedHash = hash |
| 904 |
f.mustPublish = false |
| 905 |
} |
| 906 |
return nil |
| 907 |
} |
| 908 |
|
| 909 |
func (s *server) shouldIgnoreError(snapshot *cache.Snapshot, err error) bool { |
| 910 |
if err == nil { |
| 911 |
return false |
| 912 |
} |
| 913 |
if errors.Is(err, context.Canceled) { |
| 914 |
return true |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
var hasGo bool |
| 920 |
_ = filepath.Walk(snapshot.Folder().Path(), func(_ string, info os.FileInfo, err error) error { |
| 921 |
if err != nil { |
| 922 |
return err |
| 923 |
} |
| 924 |
if !strings.HasSuffix(info.Name(), ".go") { |
| 925 |
return nil |
| 926 |
} |
| 927 |
hasGo = true |
| 928 |
return errors.New("done") |
| 929 |
}) |
| 930 |
return !hasGo |
| 931 |
} |