| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package cache |
| 11 |
|
| 12 |
import ( |
| 13 |
"context" |
| 14 |
"encoding/json" |
| 15 |
"errors" |
| 16 |
"fmt" |
| 17 |
"log" |
| 18 |
"maps" |
| 19 |
"os" |
| 20 |
"os/exec" |
| 21 |
"path" |
| 22 |
"path/filepath" |
| 23 |
"slices" |
| 24 |
"sort" |
| 25 |
"strings" |
| 26 |
"sync" |
| 27 |
"time" |
| 28 |
|
| 29 |
"golang.org/x/tools/gopls/internal/cache/typerefs" |
| 30 |
"golang.org/x/tools/gopls/internal/file" |
| 31 |
"golang.org/x/tools/gopls/internal/protocol" |
| 32 |
"golang.org/x/tools/gopls/internal/settings" |
| 33 |
"golang.org/x/tools/gopls/internal/util/moremaps" |
| 34 |
"golang.org/x/tools/gopls/internal/util/pathutil" |
| 35 |
"golang.org/x/tools/gopls/internal/vulncheck" |
| 36 |
"golang.org/x/tools/internal/event" |
| 37 |
"golang.org/x/tools/internal/gocommand" |
| 38 |
"golang.org/x/tools/internal/imports" |
| 39 |
"golang.org/x/tools/internal/modindex" |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
type Folder struct { |
| 51 |
Dir protocol.DocumentURI |
| 52 |
Name string |
| 53 |
Options *settings.Options |
| 54 |
Env GoEnv |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
type GoEnv struct { |
| 60 |
|
| 61 |
|
| 62 |
GOOS string |
| 63 |
GOARCH string |
| 64 |
GOCACHE string |
| 65 |
GOMODCACHE string |
| 66 |
GOPATH string |
| 67 |
GOPRIVATE string |
| 68 |
GOFLAGS string |
| 69 |
GO111MODULE string |
| 70 |
GOTOOLCHAIN string |
| 71 |
GOROOT string |
| 72 |
|
| 73 |
|
| 74 |
GoVersion int |
| 75 |
GoVersionOutput string |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
ExplicitGOWORK string |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
EffectiveGOPACKAGESDRIVER string |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
type View struct { |
| 98 |
id string |
| 99 |
|
| 100 |
*viewDefinition |
| 101 |
|
| 102 |
gocmdRunner *gocommand.Runner |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
baseCtx context.Context |
| 107 |
|
| 108 |
|
| 109 |
importsState *importsState |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
modcacheState *modcacheState |
| 116 |
|
| 117 |
|
| 118 |
pkgIndex *typerefs.PackageIndex |
| 119 |
|
| 120 |
|
| 121 |
parseCache *parseCache |
| 122 |
|
| 123 |
|
| 124 |
fs *overlayFS |
| 125 |
|
| 126 |
|
| 127 |
ignoreFilter *ignoreFilter |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
cancelInitialWorkspaceLoad context.CancelFunc |
| 132 |
|
| 133 |
snapshotMu sync.Mutex |
| 134 |
snapshot *Snapshot |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
initialWorkspaceLoad chan struct{} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
initializationSema chan struct{} |
| 149 |
|
| 150 |
|
| 151 |
filterFuncOnce sync.Once |
| 152 |
_filterFunc func(protocol.DocumentURI) bool |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
func (v *View) definition() *viewDefinition { return v.viewDefinition } |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
type viewDefinition struct { |
| 171 |
folder *Folder |
| 172 |
|
| 173 |
typ ViewType |
| 174 |
root protocol.DocumentURI |
| 175 |
gomod protocol.DocumentURI |
| 176 |
gowork protocol.DocumentURI |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
workspaceModFiles map[protocol.DocumentURI]struct{} |
| 187 |
workspaceModFilesErr error |
| 188 |
|
| 189 |
|
| 190 |
envOverlay map[string]string |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
func (d *viewDefinition) definition() *viewDefinition { return d } |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
func (d *viewDefinition) Type() ViewType { return d.typ } |
| 199 |
|
| 200 |
|
| 201 |
func (d *viewDefinition) Root() protocol.DocumentURI { return d.root } |
| 202 |
|
| 203 |
|
| 204 |
func (d *viewDefinition) GoMod() protocol.DocumentURI { return d.gomod } |
| 205 |
|
| 206 |
|
| 207 |
func (d *viewDefinition) GoWork() protocol.DocumentURI { return d.gowork } |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func (d *viewDefinition) EnvOverlay() []string { |
| 212 |
var env []string |
| 213 |
for k, v := range d.envOverlay { |
| 214 |
env = append(env, fmt.Sprintf("%s=%s", k, v)) |
| 215 |
} |
| 216 |
sort.Strings(env) |
| 217 |
return env |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
func (d *viewDefinition) GOOS() string { |
| 223 |
if goos, ok := d.envOverlay["GOOS"]; ok { |
| 224 |
return goos |
| 225 |
} |
| 226 |
return d.folder.Env.GOOS |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func (d *viewDefinition) GOARCH() string { |
| 232 |
if goarch, ok := d.envOverlay["GOARCH"]; ok { |
| 233 |
return goarch |
| 234 |
} |
| 235 |
return d.folder.Env.GOARCH |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
func (d viewDefinition) adjustedGO111MODULE() string { |
| 242 |
if d.folder.Env.GO111MODULE != "" { |
| 243 |
return d.folder.Env.GO111MODULE |
| 244 |
} |
| 245 |
return "auto" |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
func (d viewDefinition) ModFiles() []protocol.DocumentURI { |
| 251 |
return slices.Sorted(maps.Keys(d.workspaceModFiles)) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
func viewDefinitionsEqual(x, y *viewDefinition) bool { |
| 256 |
if (x.workspaceModFilesErr == nil) != (y.workspaceModFilesErr == nil) { |
| 257 |
return false |
| 258 |
} |
| 259 |
if x.workspaceModFilesErr != nil { |
| 260 |
if x.workspaceModFilesErr.Error() != y.workspaceModFilesErr.Error() { |
| 261 |
return false |
| 262 |
} |
| 263 |
} else if !moremaps.SameKeys(x.workspaceModFiles, y.workspaceModFiles) { |
| 264 |
return false |
| 265 |
} |
| 266 |
if len(x.envOverlay) != len(y.envOverlay) { |
| 267 |
return false |
| 268 |
} |
| 269 |
for i, xv := range x.envOverlay { |
| 270 |
if xv != y.envOverlay[i] { |
| 271 |
return false |
| 272 |
} |
| 273 |
} |
| 274 |
return x.folder == y.folder && |
| 275 |
x.typ == y.typ && |
| 276 |
x.root == y.root && |
| 277 |
x.gomod == y.gomod && |
| 278 |
x.gowork == y.gowork |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
type ViewType int |
| 288 |
|
| 289 |
const ( |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
GoPackagesDriverView ViewType = iota |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
GOPATHView |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
GoModView |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
GoWorkView |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
AdHocView |
| 319 |
) |
| 320 |
|
| 321 |
func (t ViewType) String() string { |
| 322 |
switch t { |
| 323 |
case GoPackagesDriverView: |
| 324 |
return "GoPackagesDriver" |
| 325 |
case GOPATHView: |
| 326 |
return "GOPATH" |
| 327 |
case GoModView: |
| 328 |
return "GoMod" |
| 329 |
case GoWorkView: |
| 330 |
return "GoWork" |
| 331 |
case AdHocView: |
| 332 |
return "AdHoc" |
| 333 |
default: |
| 334 |
return "Unknown" |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
func (typ ViewType) usesModules() bool { |
| 340 |
switch typ { |
| 341 |
case GoModView, GoWorkView: |
| 342 |
return true |
| 343 |
default: |
| 344 |
return false |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
func (v *View) ID() string { return v.id } |
| 350 |
|
| 351 |
|
| 352 |
func (v *View) GoCommandRunner() *gocommand.Runner { |
| 353 |
return v.gocmdRunner |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
func (v *View) Folder() *Folder { |
| 358 |
return v.folder |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
func (v *View) Env() []string { |
| 363 |
return slices.Concat( |
| 364 |
os.Environ(), |
| 365 |
v.folder.Options.EnvSlice(), |
| 366 |
[]string{"GO111MODULE=" + v.adjustedGO111MODULE()}, |
| 367 |
v.EnvOverlay(), |
| 368 |
) |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
func (v *View) ModcacheIndex() (*modindex.Index, error) { |
| 373 |
if v.modcacheState == nil { |
| 374 |
return nil, fmt.Errorf("view %q has no module cache", v.id) |
| 375 |
} |
| 376 |
return v.modcacheState.getIndex() |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
func (s *Session) UpdateFolders(ctx context.Context, newFolders []*Folder) error { |
| 383 |
s.viewMu.Lock() |
| 384 |
defer s.viewMu.Unlock() |
| 385 |
|
| 386 |
overlays := s.Overlays() |
| 387 |
var openFiles []protocol.DocumentURI |
| 388 |
for _, o := range overlays { |
| 389 |
openFiles = append(openFiles, o.URI()) |
| 390 |
} |
| 391 |
|
| 392 |
defs, err := selectViewDefs(ctx, s, newFolders, openFiles) |
| 393 |
if err != nil { |
| 394 |
return err |
| 395 |
} |
| 396 |
var newViews []*View |
| 397 |
for _, def := range defs { |
| 398 |
v, _, release := s.createView(ctx, def) |
| 399 |
release() |
| 400 |
newViews = append(newViews, v) |
| 401 |
} |
| 402 |
for _, v := range s.views { |
| 403 |
v.shutdown() |
| 404 |
} |
| 405 |
s.views = newViews |
| 406 |
return nil |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
func (s *Snapshot) RunProcessEnvFunc(ctx context.Context, fn func(context.Context, *imports.Options) error) error { |
| 412 |
return s.view.importsState.runProcessEnvFunc(ctx, s, fn) |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
func fileHasExtension(path string, suffixes []string) bool { |
| 417 |
ext := filepath.Ext(path) |
| 418 |
if ext != "" && ext[0] == '.' { |
| 419 |
ext = ext[1:] |
| 420 |
} |
| 421 |
for _, s := range suffixes { |
| 422 |
if s != "" && ext == s { |
| 423 |
return true |
| 424 |
} |
| 425 |
} |
| 426 |
return false |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
func (s *Snapshot) locateTemplateFiles(ctx context.Context) { |
| 432 |
suffixes := s.Options().TemplateExtensions |
| 433 |
if len(suffixes) == 0 { |
| 434 |
return |
| 435 |
} |
| 436 |
|
| 437 |
searched := 0 |
| 438 |
filterFunc := s.view.filterFunc() |
| 439 |
err := filepath.WalkDir(s.view.folder.Dir.Path(), func(path string, entry os.DirEntry, err error) error { |
| 440 |
if err != nil { |
| 441 |
return err |
| 442 |
} |
| 443 |
if entry.IsDir() { |
| 444 |
return nil |
| 445 |
} |
| 446 |
if fileLimit > 0 && searched > fileLimit { |
| 447 |
return errExhausted |
| 448 |
} |
| 449 |
searched++ |
| 450 |
if !fileHasExtension(path, suffixes) { |
| 451 |
return nil |
| 452 |
} |
| 453 |
uri := protocol.URIFromPath(path) |
| 454 |
if filterFunc(uri) { |
| 455 |
return nil |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
s.ReadFile(ctx, uri) |
| 465 |
return nil |
| 466 |
}) |
| 467 |
if err != nil { |
| 468 |
event.Error(ctx, "searching for template files failed", err) |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
func (v *View) filterFunc() func(protocol.DocumentURI) bool { |
| 475 |
v.filterFuncOnce.Do(func() { |
| 476 |
folderDir := v.folder.Dir.Path() |
| 477 |
gomodcache := v.folder.Env.GOMODCACHE |
| 478 |
var filters []string |
| 479 |
filters = append(filters, v.folder.Options.DirectoryFilters...) |
| 480 |
if pref, ok := strings.CutPrefix(gomodcache, folderDir); ok { |
| 481 |
modcacheFilter := "-" + strings.TrimPrefix(filepath.ToSlash(pref), "/") |
| 482 |
filters = append(filters, modcacheFilter) |
| 483 |
} |
| 484 |
pathIncluded := PathIncludeFunc(filters) |
| 485 |
v._filterFunc = func(uri protocol.DocumentURI) bool { |
| 486 |
|
| 487 |
if pathutil.InDir(folderDir, uri.Path()) { |
| 488 |
return relPathExcludedByFilter(strings.TrimPrefix(uri.Path(), folderDir), pathIncluded) |
| 489 |
} |
| 490 |
return false |
| 491 |
} |
| 492 |
}) |
| 493 |
return v._filterFunc |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
func (v *View) shutdown() { |
| 498 |
|
| 499 |
v.cancelInitialWorkspaceLoad() |
| 500 |
v.importsState.stopTimer() |
| 501 |
if v.modcacheState != nil { |
| 502 |
v.modcacheState.stopTimer() |
| 503 |
} |
| 504 |
|
| 505 |
v.snapshotMu.Lock() |
| 506 |
if v.snapshot != nil { |
| 507 |
v.snapshot.cancel() |
| 508 |
v.snapshot.decref() |
| 509 |
v.snapshot = nil |
| 510 |
} |
| 511 |
v.snapshotMu.Unlock() |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
func (v *View) ScanImports() { |
| 517 |
gomodcache := v.folder.Env.GOMODCACHE |
| 518 |
dirCache := v.importsState.modCache.dirCache(gomodcache) |
| 519 |
imports.ScanModuleCache(gomodcache, dirCache, log.Printf) |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
func (s *Snapshot) IgnoredFile(uri protocol.DocumentURI) bool { |
| 528 |
|
| 529 |
|
| 530 |
{ |
| 531 |
uriStr := string(uri) |
| 532 |
if !strings.Contains(uriStr, ".") && !strings.Contains(uriStr, "_") && !strings.Contains(uriStr, "testdata") { |
| 533 |
return false |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
return s.view.ignoreFilter.ignored(uri.Path()) |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
type ignoreFilter struct { |
| 542 |
prefixes []string |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
func newIgnoreFilter(dirs []string) *ignoreFilter { |
| 548 |
f := new(ignoreFilter) |
| 549 |
for _, d := range dirs { |
| 550 |
f.prefixes = append(f.prefixes, filepath.Clean(d)+string(filepath.Separator)) |
| 551 |
} |
| 552 |
return f |
| 553 |
} |
| 554 |
|
| 555 |
func (f *ignoreFilter) ignored(filename string) bool { |
| 556 |
for _, prefix := range f.prefixes { |
| 557 |
if suffix, ok := strings.CutPrefix(filename, prefix); ok { |
| 558 |
if checkIgnored(suffix) { |
| 559 |
return true |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
return false |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
”: |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
func checkIgnored(suffix string) bool { |
| 572 |
|
| 573 |
|
| 574 |
for component := range strings.SplitSeq(suffix, string(filepath.Separator)) { |
| 575 |
if len(component) == 0 { |
| 576 |
continue |
| 577 |
} |
| 578 |
if component[0] == '.' || component[0] == '_' || component == "testdata" { |
| 579 |
return true |
| 580 |
} |
| 581 |
} |
| 582 |
return false |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
func (v *View) Snapshot() (*Snapshot, func(), error) { |
| 592 |
v.snapshotMu.Lock() |
| 593 |
defer v.snapshotMu.Unlock() |
| 594 |
if v.snapshot == nil { |
| 595 |
return nil, nil, errors.New("view is shutdown") |
| 596 |
} |
| 597 |
return v.snapshot, v.snapshot.Acquire(), nil |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
func (s *Snapshot) initialize(ctx context.Context, firstAttempt bool) { |
| 616 |
|
| 617 |
|
| 618 |
select { |
| 619 |
case <-ctx.Done(): |
| 620 |
return |
| 621 |
case s.view.initializationSema <- struct{}{}: |
| 622 |
} |
| 623 |
|
| 624 |
defer func() { |
| 625 |
<-s.view.initializationSema |
| 626 |
}() |
| 627 |
|
| 628 |
s.mu.Lock() |
| 629 |
initialized := s.initialized |
| 630 |
s.mu.Unlock() |
| 631 |
|
| 632 |
if initialized { |
| 633 |
return |
| 634 |
} |
| 635 |
|
| 636 |
defer func() { |
| 637 |
if firstAttempt { |
| 638 |
close(s.view.initialWorkspaceLoad) |
| 639 |
} |
| 640 |
}() |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
s.locateTemplateFiles(ctx) |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
var scopes []loadScope |
| 649 |
var modDiagnostics []*Diagnostic |
| 650 |
addError := func(uri protocol.DocumentURI, err error) { |
| 651 |
modDiagnostics = append(modDiagnostics, &Diagnostic{ |
| 652 |
URI: uri, |
| 653 |
Severity: protocol.SeverityError, |
| 654 |
Source: ListError, |
| 655 |
Message: err.Error(), |
| 656 |
}) |
| 657 |
} |
| 658 |
|
| 659 |
if len(s.view.workspaceModFiles) > 0 { |
| 660 |
for modURI := range s.view.workspaceModFiles { |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
fh, err := s.ReadFile(ctx, modURI) |
| 670 |
if err != nil { |
| 671 |
if ctx.Err() != nil { |
| 672 |
return |
| 673 |
} |
| 674 |
addError(modURI, err) |
| 675 |
continue |
| 676 |
} |
| 677 |
parsed, err := s.ParseMod(ctx, fh) |
| 678 |
if err != nil { |
| 679 |
if ctx.Err() != nil { |
| 680 |
return |
| 681 |
} |
| 682 |
addError(modURI, err) |
| 683 |
continue |
| 684 |
} |
| 685 |
if parsed.File == nil || parsed.File.Module == nil { |
| 686 |
addError(modURI, fmt.Errorf("no module path for %s", modURI)) |
| 687 |
continue |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
scopes = append(scopes, moduleLoadScope{dir: modURI.DirPath(), modulePath: parsed.File.Module.Mod.Path}) |
| 693 |
} |
| 694 |
} else { |
| 695 |
scopes = append(scopes, viewLoadScope{}) |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
if len(scopes) > 0 { |
| 702 |
scopes = append(scopes, packageLoadScope("builtin")) |
| 703 |
} |
| 704 |
loadErr := s.load(ctx, NetworkOK, scopes...) |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
if loadErr != nil && ctx.Err() != nil && !firstAttempt { |
| 713 |
return |
| 714 |
} |
| 715 |
|
| 716 |
var initialErr *InitializationError |
| 717 |
switch { |
| 718 |
case loadErr != nil && ctx.Err() != nil: |
| 719 |
event.Error(ctx, fmt.Sprintf("initial workspace load: %v", loadErr), loadErr) |
| 720 |
initialErr = &InitializationError{ |
| 721 |
MainError: loadErr, |
| 722 |
} |
| 723 |
case loadErr != nil: |
| 724 |
event.Error(ctx, "initial workspace load failed", loadErr) |
| 725 |
extractedDiags := s.extractGoCommandErrors(ctx, loadErr) |
| 726 |
initialErr = &InitializationError{ |
| 727 |
MainError: loadErr, |
| 728 |
Diagnostics: moremaps.Group(extractedDiags, byURI), |
| 729 |
} |
| 730 |
case s.view.workspaceModFilesErr != nil: |
| 731 |
initialErr = &InitializationError{ |
| 732 |
MainError: s.view.workspaceModFilesErr, |
| 733 |
} |
| 734 |
case len(modDiagnostics) > 0: |
| 735 |
initialErr = &InitializationError{ |
| 736 |
MainError: errors.New(modDiagnostics[0].Message), |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
s.mu.Lock() |
| 741 |
defer s.mu.Unlock() |
| 742 |
|
| 743 |
s.initialized = true |
| 744 |
s.initialErr = initialErr |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
type StateChange struct { |
| 752 |
Modifications []file.Modification |
| 753 |
Files map[protocol.DocumentURI]file.Handle |
| 754 |
ModuleUpgrades map[protocol.DocumentURI]map[string]string |
| 755 |
Vulns map[protocol.DocumentURI]*vulncheck.Result |
| 756 |
CompilerOptDetails map[protocol.DocumentURI]bool |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
func (s *Session) InvalidateView(ctx context.Context, view *View, changed StateChange) (*Snapshot, func(), error) { |
| 768 |
s.viewMu.Lock() |
| 769 |
defer s.viewMu.Unlock() |
| 770 |
|
| 771 |
if !slices.Contains(s.views, view) { |
| 772 |
return nil, nil, fmt.Errorf("view is no longer active") |
| 773 |
} |
| 774 |
snapshot, release, _ := s.invalidateViewLocked(ctx, view, changed) |
| 775 |
return snapshot, release, nil |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
func (s *Session) invalidateViewLocked(ctx context.Context, v *View, changed StateChange) (*Snapshot, func(), bool) { |
| 786 |
|
| 787 |
ctx = context.WithoutCancel(ctx) |
| 788 |
|
| 789 |
|
| 790 |
v.snapshotMu.Lock() |
| 791 |
defer v.snapshotMu.Unlock() |
| 792 |
|
| 793 |
prevSnapshot := v.snapshot |
| 794 |
|
| 795 |
if prevSnapshot == nil { |
| 796 |
panic("invalidateContent called after shutdown") |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
prevSnapshot.cancel() |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
prevSnapshot.AwaitInitialized(ctx) |
| 807 |
|
| 808 |
var needsDiagnosis bool |
| 809 |
s.snapshotWG.Add(1) |
| 810 |
v.snapshot, needsDiagnosis = prevSnapshot.clone(ctx, v.baseCtx, changed, s.snapshotWG.Done) |
| 811 |
|
| 812 |
|
| 813 |
prevSnapshot.decref() |
| 814 |
|
| 815 |
|
| 816 |
return v.snapshot, v.snapshot.Acquire(), needsDiagnosis |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
func defineView(ctx context.Context, fs file.Source, folder *Folder, forFile file.Handle) (*viewDefinition, error) { |
| 837 |
if err := checkPathValid(folder.Dir.Path()); err != nil { |
| 838 |
return nil, fmt.Errorf("invalid workspace folder path: %w; check that the spelling of the configured workspace folder path agrees with the spelling reported by the operating system", err) |
| 839 |
} |
| 840 |
dir := folder.Dir.Path() |
| 841 |
if forFile != nil { |
| 842 |
dir = forFile.URI().DirPath() |
| 843 |
} |
| 844 |
|
| 845 |
def := new(viewDefinition) |
| 846 |
def.folder = folder |
| 847 |
|
| 848 |
if forFile != nil && fileKind(forFile) == file.Go { |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
path := forFile.URI().Path() |
| 857 |
if content, err := forFile.Content(); err == nil { |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
content = trimContentForPortMatch(content) |
| 863 |
viewPort := port{def.folder.Env.GOOS, def.folder.Env.GOARCH} |
| 864 |
if !viewPort.matches(path, content) { |
| 865 |
for _, p := range preferredPorts { |
| 866 |
if p.matches(path, content) { |
| 867 |
if def.envOverlay == nil { |
| 868 |
def.envOverlay = make(map[string]string) |
| 869 |
} |
| 870 |
def.envOverlay["GOOS"] = p.GOOS |
| 871 |
def.envOverlay["GOARCH"] = p.GOARCH |
| 872 |
break |
| 873 |
} |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
var err error |
| 880 |
dirURI := protocol.URIFromPath(dir) |
| 881 |
goworkFromEnv := false |
| 882 |
if folder.Env.ExplicitGOWORK != "off" && folder.Env.ExplicitGOWORK != "" { |
| 883 |
goworkFromEnv = true |
| 884 |
def.gowork = protocol.URIFromPath(folder.Env.ExplicitGOWORK) |
| 885 |
} else { |
| 886 |
def.gowork, err = findRootPattern(ctx, dirURI, "go.work", fs) |
| 887 |
if err != nil { |
| 888 |
return nil, err |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
def.gomod, err = findRootPattern(ctx, dirURI, "go.mod", fs) |
| 895 |
if err != nil { |
| 896 |
return nil, err |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
if def.folder.Env.EffectiveGOPACKAGESDRIVER != "" { |
| 909 |
def.typ = GoPackagesDriverView |
| 910 |
def.root = dirURI |
| 911 |
return def, nil |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
gomodWorkspace := func() map[protocol.DocumentURI]unit { |
| 922 |
modFiles := map[protocol.DocumentURI]struct{}{def.gomod: {}} |
| 923 |
if folder.Options.IncludeReplaceInWorkspace { |
| 924 |
includingReplace, err := goModModules(ctx, def.gomod, fs) |
| 925 |
if err == nil { |
| 926 |
modFiles = includingReplace |
| 927 |
} else { |
| 928 |
|
| 929 |
|
| 930 |
} |
| 931 |
} |
| 932 |
return modFiles |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
if def.adjustedGO111MODULE() != "off" && folder.Env.ExplicitGOWORK != "off" && def.gowork != "" { |
| 938 |
def.typ = GoWorkView |
| 939 |
if goworkFromEnv { |
| 940 |
|
| 941 |
|
| 942 |
def.root = dirURI |
| 943 |
} else { |
| 944 |
|
| 945 |
def.root = def.gowork.Dir() |
| 946 |
} |
| 947 |
def.workspaceModFiles, def.workspaceModFilesErr = goWorkModules(ctx, def.gowork, fs) |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
if forFile != nil && def.workspaceModFilesErr == nil && def.gomod != "" { |
| 952 |
if _, ok := def.workspaceModFiles[def.gomod]; !ok { |
| 953 |
def.typ = GoModView |
| 954 |
def.root = def.gomod.Dir() |
| 955 |
def.workspaceModFiles = gomodWorkspace() |
| 956 |
if def.envOverlay == nil { |
| 957 |
def.envOverlay = make(map[string]string) |
| 958 |
} |
| 959 |
def.envOverlay["GOWORK"] = "off" |
| 960 |
} |
| 961 |
} |
| 962 |
return def, nil |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
if def.adjustedGO111MODULE() != "off" && def.gomod != "" { |
| 972 |
def.typ = GoModView |
| 973 |
def.root = def.gomod.Dir() |
| 974 |
def.workspaceModFiles = gomodWorkspace() |
| 975 |
return def, nil |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
inGOPATH := false |
| 980 |
for _, gp := range filepath.SplitList(folder.Env.GOPATH) { |
| 981 |
if pathutil.InDir(filepath.Join(gp, "src"), dir) { |
| 982 |
inGOPATH = true |
| 983 |
break |
| 984 |
} |
| 985 |
} |
| 986 |
if def.adjustedGO111MODULE() != "on" && inGOPATH { |
| 987 |
def.typ = GOPATHView |
| 988 |
def.root = dirURI |
| 989 |
return def, nil |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
def.typ = AdHocView |
| 995 |
def.root = dirURI |
| 996 |
return def, nil |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
func FetchGoEnv(ctx context.Context, folder protocol.DocumentURI, opts *settings.Options) (*GoEnv, error) { |
| 1002 |
dir := folder.Path() |
| 1003 |
|
| 1004 |
|
| 1005 |
runner := new(gocommand.Runner) |
| 1006 |
inv := gocommand.Invocation{ |
| 1007 |
WorkingDir: dir, |
| 1008 |
Env: opts.EnvSlice(), |
| 1009 |
} |
| 1010 |
|
| 1011 |
var ( |
| 1012 |
env = new(GoEnv) |
| 1013 |
err error |
| 1014 |
) |
| 1015 |
envvars := map[string]*string{ |
| 1016 |
"GOOS": &env.GOOS, |
| 1017 |
"GOARCH": &env.GOARCH, |
| 1018 |
"GOCACHE": &env.GOCACHE, |
| 1019 |
"GOPATH": &env.GOPATH, |
| 1020 |
"GOPRIVATE": &env.GOPRIVATE, |
| 1021 |
"GOMODCACHE": &env.GOMODCACHE, |
| 1022 |
"GOFLAGS": &env.GOFLAGS, |
| 1023 |
"GO111MODULE": &env.GO111MODULE, |
| 1024 |
"GOTOOLCHAIN": &env.GOTOOLCHAIN, |
| 1025 |
"GOROOT": &env.GOROOT, |
| 1026 |
} |
| 1027 |
if err := loadGoEnv(ctx, dir, opts.EnvSlice(), runner, envvars); err != nil { |
| 1028 |
return nil, err |
| 1029 |
} |
| 1030 |
|
| 1031 |
env.GoVersion, err = gocommand.GoVersion(ctx, inv, runner) |
| 1032 |
if err != nil { |
| 1033 |
return nil, err |
| 1034 |
} |
| 1035 |
env.GoVersionOutput, err = gocommand.GoVersionOutput(ctx, inv, runner) |
| 1036 |
if err != nil { |
| 1037 |
return nil, err |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
if driver, ok := opts.Env["GOPACKAGESDRIVER"]; ok { |
| 1042 |
if driver != "off" { |
| 1043 |
env.EffectiveGOPACKAGESDRIVER = driver |
| 1044 |
} |
| 1045 |
} else if driver := os.Getenv("GOPACKAGESDRIVER"); driver != "off" { |
| 1046 |
env.EffectiveGOPACKAGESDRIVER = driver |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
if env.EffectiveGOPACKAGESDRIVER == "" { |
| 1053 |
tool, err := exec.LookPath("gopackagesdriver") |
| 1054 |
if err == nil && tool != "" { |
| 1055 |
env.EffectiveGOPACKAGESDRIVER = tool |
| 1056 |
} |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
if gowork, ok := opts.Env["GOWORK"]; ok { |
| 1064 |
env.ExplicitGOWORK = gowork |
| 1065 |
} else { |
| 1066 |
env.ExplicitGOWORK = os.Getenv("GOWORK") |
| 1067 |
} |
| 1068 |
return env, nil |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
func loadGoEnv(ctx context.Context, dir string, configEnv []string, runner *gocommand.Runner, vars map[string]*string) error { |
| 1074 |
|
| 1075 |
args := []string{"-json"} |
| 1076 |
for k := range vars { |
| 1077 |
args = append(args, k) |
| 1078 |
} |
| 1079 |
|
| 1080 |
inv := gocommand.Invocation{ |
| 1081 |
Verb: "env", |
| 1082 |
Args: args, |
| 1083 |
Env: configEnv, |
| 1084 |
WorkingDir: dir, |
| 1085 |
} |
| 1086 |
stdout, err := runner.Run(ctx, inv) |
| 1087 |
if err != nil { |
| 1088 |
return err |
| 1089 |
} |
| 1090 |
envMap := make(map[string]string) |
| 1091 |
if err := json.Unmarshal(stdout.Bytes(), &envMap); err != nil { |
| 1092 |
return fmt.Errorf("internal error unmarshaling JSON from 'go env': %w", err) |
| 1093 |
} |
| 1094 |
for key, ptr := range vars { |
| 1095 |
*ptr = envMap[key] |
| 1096 |
} |
| 1097 |
|
| 1098 |
return nil |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
func findRootPattern(ctx context.Context, dirURI protocol.DocumentURI, basename string, fs file.Source) (protocol.DocumentURI, error) { |
| 1110 |
dir := dirURI.Path() |
| 1111 |
for dir != "" { |
| 1112 |
target := filepath.Join(dir, basename) |
| 1113 |
uri := protocol.URIFromPath(target) |
| 1114 |
fh, err := fs.ReadFile(ctx, uri) |
| 1115 |
if err != nil { |
| 1116 |
return "", err |
| 1117 |
} |
| 1118 |
if fileExists(fh) { |
| 1119 |
return uri, nil |
| 1120 |
} |
| 1121 |
|
| 1122 |
next, _ := filepath.Split(strings.TrimRight(dir, string(filepath.Separator))) |
| 1123 |
if next == dir { |
| 1124 |
break |
| 1125 |
} |
| 1126 |
dir = next |
| 1127 |
} |
| 1128 |
return "", nil |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
var checkPathValid = defaultCheckPathValid |
| 1138 |
|
| 1139 |
|
| 1140 |
func CheckPathValid(dir string) error { return checkPathValid(dir) } |
| 1141 |
|
| 1142 |
func defaultCheckPathValid(path string) error { |
| 1143 |
return nil |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
func (s *Snapshot) IsGoPrivatePath(target string) bool { |
| 1149 |
return globsMatchPath(s.view.folder.Env.GOPRIVATE, target) |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
func (s *Snapshot) ModuleUpgrades(modfile protocol.DocumentURI) map[string]string { |
| 1155 |
s.mu.Lock() |
| 1156 |
defer s.mu.Unlock() |
| 1157 |
upgrades := map[string]string{} |
| 1158 |
orig, _ := s.moduleUpgrades.Get(modfile) |
| 1159 |
maps.Copy(upgrades, orig) |
| 1160 |
return upgrades |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
var MaxGovulncheckResultAge = 1 * time.Hour |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
func (s *Snapshot) Vulnerabilities(modfiles ...protocol.DocumentURI) map[protocol.DocumentURI]*vulncheck.Result { |
| 1178 |
m := make(map[protocol.DocumentURI]*vulncheck.Result) |
| 1179 |
now := time.Now() |
| 1180 |
|
| 1181 |
s.mu.Lock() |
| 1182 |
defer s.mu.Unlock() |
| 1183 |
|
| 1184 |
if len(modfiles) == 0 { |
| 1185 |
modfiles = slices.Collect(s.vulns.Keys()) |
| 1186 |
} |
| 1187 |
for _, modfile := range modfiles { |
| 1188 |
vuln, _ := s.vulns.Get(modfile) |
| 1189 |
if vuln != nil && now.Sub(vuln.AsOf) > MaxGovulncheckResultAge { |
| 1190 |
vuln = nil |
| 1191 |
} |
| 1192 |
m[modfile] = vuln |
| 1193 |
} |
| 1194 |
return m |
| 1195 |
} |
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
func (v *View) GoVersion() int { |
| 1200 |
return v.folder.Env.GoVersion |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
func (v *View) GoVersionString() string { |
| 1207 |
return gocommand.ParseGoVersionOutput(v.folder.Env.GoVersionOutput) |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
func (s *Snapshot) GoVersionString() string { |
| 1214 |
return s.view.GoVersionString() |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
func globsMatchPath(globs, target string) bool { |
| 1220 |
for globs != "" { |
| 1221 |
|
| 1222 |
var glob string |
| 1223 |
if i := strings.Index(globs, ","); i >= 0 { |
| 1224 |
glob, globs = globs[:i], globs[i+1:] |
| 1225 |
} else { |
| 1226 |
glob, globs = globs, "" |
| 1227 |
} |
| 1228 |
if glob == "" { |
| 1229 |
continue |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
n := strings.Count(glob, "/") |
| 1236 |
prefix := target |
| 1237 |
|
| 1238 |
for i := range len(target) { |
| 1239 |
if target[i] == '/' { |
| 1240 |
if n == 0 { |
| 1241 |
prefix = target[:i] |
| 1242 |
break |
| 1243 |
} |
| 1244 |
n-- |
| 1245 |
} |
| 1246 |
} |
| 1247 |
if n > 0 { |
| 1248 |
|
| 1249 |
continue |
| 1250 |
} |
| 1251 |
matched, _ := path.Match(glob, prefix) |
| 1252 |
if matched { |
| 1253 |
return true |
| 1254 |
} |
| 1255 |
} |
| 1256 |
return false |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
func allFilesExcluded(files []string, filterFunc func(protocol.DocumentURI) bool) bool { |
| 1262 |
for _, f := range files { |
| 1263 |
uri := protocol.URIFromPath(f) |
| 1264 |
if !filterFunc(uri) { |
| 1265 |
return false |
| 1266 |
} |
| 1267 |
} |
| 1268 |
return true |
| 1269 |
} |
| 1270 |
|
| 1271 |
func relPathExcludedByFilter(path string, pathIncluded func(string) bool) bool { |
| 1272 |
path = strings.TrimPrefix(filepath.ToSlash(path), "/") |
| 1273 |
return !pathIncluded(path) |
| 1274 |
} |