| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"go/types" |
| 13 |
"path/filepath" |
| 14 |
"slices" |
| 15 |
"sort" |
| 16 |
"strings" |
| 17 |
"sync/atomic" |
| 18 |
"time" |
| 19 |
|
| 20 |
"golang.org/x/tools/go/packages" |
| 21 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 22 |
"golang.org/x/tools/gopls/internal/file" |
| 23 |
"golang.org/x/tools/gopls/internal/label" |
| 24 |
"golang.org/x/tools/gopls/internal/protocol" |
| 25 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 26 |
"golang.org/x/tools/gopls/internal/util/immutable" |
| 27 |
"golang.org/x/tools/gopls/internal/util/pathutil" |
| 28 |
"golang.org/x/tools/internal/event" |
| 29 |
"golang.org/x/tools/internal/packagesinternal" |
| 30 |
"golang.org/x/tools/internal/typesinternal" |
| 31 |
) |
| 32 |
|
| 33 |
var loadID uint64 |
| 34 |
|
| 35 |
|
| 36 |
var errNoPackages = errors.New("no packages returned") |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func (s *Snapshot) load(ctx context.Context, allowNetwork AllowNetwork, scopes ...loadScope) (err error) { |
| 46 |
if ctx.Err() != nil { |
| 47 |
|
| 48 |
|
| 49 |
return ctx.Err() |
| 50 |
} |
| 51 |
id := atomic.AddUint64(&loadID, 1) |
| 52 |
eventName := fmt.Sprintf("go/packages.Load #%d", id) |
| 53 |
|
| 54 |
var query []string |
| 55 |
var standalone bool |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
moduleQueries := make(map[string]string) |
| 60 |
|
| 61 |
for _, scope := range scopes { |
| 62 |
switch scope := scope.(type) { |
| 63 |
case packageLoadScope: |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
query = append(query, string(scope)) |
| 68 |
|
| 69 |
case fileLoadScope: |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
uri := protocol.DocumentURI(scope) |
| 75 |
if len(scopes) > 1 { |
| 76 |
panic(fmt.Sprintf("internal error: load called with multiple scopes when a file scope is present (file: %s)", uri)) |
| 77 |
} |
| 78 |
fh, err := s.ReadFile(ctx, uri) |
| 79 |
if err != nil || fh == nil || s.FileKind(fh) != file.Go { |
| 80 |
|
| 81 |
continue |
| 82 |
} |
| 83 |
contents, err := fh.Content() |
| 84 |
if err != nil { |
| 85 |
continue |
| 86 |
} |
| 87 |
if isStandaloneFile(contents, s.Options().StandaloneTags) { |
| 88 |
standalone = true |
| 89 |
query = append(query, uri.Path()) |
| 90 |
} else { |
| 91 |
query = append(query, fmt.Sprintf("file=%s", uri.Path())) |
| 92 |
} |
| 93 |
|
| 94 |
case moduleLoadScope: |
| 95 |
modQuery := fmt.Sprintf("%s%c...", scope.dir, filepath.Separator) |
| 96 |
query = append(query, modQuery) |
| 97 |
moduleQueries[modQuery] = scope.modulePath |
| 98 |
|
| 99 |
case viewLoadScope: |
| 100 |
|
| 101 |
|
| 102 |
if s.view.typ == AdHocView { |
| 103 |
query = append(query, "./") |
| 104 |
} else { |
| 105 |
query = append(query, "./...") |
| 106 |
} |
| 107 |
|
| 108 |
default: |
| 109 |
panic(fmt.Sprintf("unknown scope type %T", scope)) |
| 110 |
} |
| 111 |
} |
| 112 |
if len(query) == 0 { |
| 113 |
return nil |
| 114 |
} |
| 115 |
sort.Strings(query) |
| 116 |
|
| 117 |
ctx, done := event.Start(ctx, "cache.snapshot.load", label.Query.Of(query)) |
| 118 |
defer done() |
| 119 |
|
| 120 |
startTime := time.Now() |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute) |
| 126 |
defer cancel() |
| 127 |
|
| 128 |
cfg := s.config(ctx, allowNetwork) |
| 129 |
pkgs, err := packages.Load(cfg, query...) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
if ctx.Err() != nil { |
| 135 |
return ctx.Err() |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
{ |
| 140 |
lbls := append(s.Labels(), |
| 141 |
label.Query.Of(query), |
| 142 |
label.PackageCount.Of(len(pkgs)), |
| 143 |
label.Duration.Of(time.Since(startTime)), |
| 144 |
) |
| 145 |
if err != nil { |
| 146 |
event.Error(ctx, eventName, err, lbls...) |
| 147 |
} else { |
| 148 |
event.Log(ctx, eventName, lbls...) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if err != nil { |
| 153 |
return fmt.Errorf("packages.Load error: %w", err) |
| 154 |
} |
| 155 |
|
| 156 |
if standalone { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
var standalonePkg *packages.Package |
| 168 |
for _, pkg := range pkgs { |
| 169 |
if pkg.ID == "command-line-arguments" { |
| 170 |
if standalonePkg != nil { |
| 171 |
return fmt.Errorf("go/packages returned multiple standalone packages") |
| 172 |
} |
| 173 |
standalonePkg = pkg |
| 174 |
} else if pkg.ForTest == "" && !strings.HasSuffix(pkg.ID, ".test") { |
| 175 |
return fmt.Errorf("go/packages returned unexpected package %q for standalone file", pkg.ID) |
| 176 |
} |
| 177 |
} |
| 178 |
if standalonePkg == nil { |
| 179 |
return fmt.Errorf("go/packages failed to return non-test standalone package") |
| 180 |
} |
| 181 |
if len(standalonePkg.CompiledGoFiles) > 0 { |
| 182 |
pkgs = []*packages.Package{standalonePkg} |
| 183 |
} else { |
| 184 |
pkgs = nil |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if len(pkgs) == 0 { |
| 189 |
return fmt.Errorf("packages.Load error: %w", errNoPackages) |
| 190 |
} |
| 191 |
|
| 192 |
moduleErrs := make(map[string][]packages.Error) |
| 193 |
filterFunc := s.view.filterFunc() |
| 194 |
newMetadata := make(map[PackageID]*metadata.Package) |
| 195 |
for _, pkg := range pkgs { |
| 196 |
if pkg.Module != nil && strings.Contains(pkg.Module.Path, "command-line-arguments") { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
return fmt.Errorf(`load failed: module name in %s contains "command-line-arguments", which is disallowed`, pkg.Module.GoMod) |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if mod := moduleQueries[pkg.PkgPath]; mod != "" { |
| 213 |
if len(pkg.Errors) > 0 { |
| 214 |
moduleErrs[mod] = pkg.Errors |
| 215 |
} |
| 216 |
continue |
| 217 |
} |
| 218 |
|
| 219 |
if s.Options().VerboseOutput { |
| 220 |
event.Log(ctx, eventName, append( |
| 221 |
s.Labels(), |
| 222 |
label.Package.Of(pkg.ID), |
| 223 |
label.Files.Of(pkg.CompiledGoFiles))...) |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if len(pkg.GoFiles) == 0 && len(pkg.CompiledGoFiles) == 0 { |
| 229 |
continue |
| 230 |
} |
| 231 |
|
| 232 |
if pkg.PkgPath == "builtin" { |
| 233 |
if len(pkg.GoFiles) != 1 { |
| 234 |
return fmt.Errorf("only expected 1 file for builtin, got %v", len(pkg.GoFiles)) |
| 235 |
} |
| 236 |
s.setBuiltin(pkg.GoFiles[0]) |
| 237 |
continue |
| 238 |
} |
| 239 |
if pkg.ForTest == "builtin" { |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
continue |
| 244 |
} |
| 245 |
|
| 246 |
if isTestMain(pkg, s.view.folder.Env.GOCACHE) { |
| 247 |
continue |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if allFilesExcluded(pkg.GoFiles, filterFunc) { |
| 256 |
continue |
| 257 |
} |
| 258 |
buildMetadata(newMetadata, cfg.Dir, standalone, pkg) |
| 259 |
} |
| 260 |
|
| 261 |
s.mu.Lock() |
| 262 |
|
| 263 |
|
| 264 |
for id, ph := range s.packages.All() { |
| 265 |
if s.meta.Packages[id] != ph.mp { |
| 266 |
panic("inconsistent metadata") |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
var files []protocol.DocumentURI |
| 273 |
seenFiles := make(map[protocol.DocumentURI]bool) |
| 274 |
updates := make(map[PackageID]*metadata.Package) |
| 275 |
for _, mp := range newMetadata { |
| 276 |
if existing := s.meta.Packages[mp.ID]; existing == nil { |
| 277 |
|
| 278 |
for _, uri := range mp.CompiledGoFiles { |
| 279 |
if !seenFiles[uri] { |
| 280 |
seenFiles[uri] = true |
| 281 |
files = append(files, uri) |
| 282 |
} |
| 283 |
} |
| 284 |
updates[mp.ID] = mp |
| 285 |
s.shouldLoad.Delete(mp.ID) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if s.Options().VerboseOutput { |
| 290 |
event.Log(ctx, fmt.Sprintf("%s: updating metadata for %d packages", eventName, len(updates))) |
| 291 |
} |
| 292 |
|
| 293 |
meta := s.meta.Update(updates) |
| 294 |
workspacePackages := computeWorkspacePackagesLocked(ctx, s, meta) |
| 295 |
s.meta = meta |
| 296 |
s.workspacePackages = workspacePackages |
| 297 |
|
| 298 |
s.mu.Unlock() |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
s.preloadFiles(ctx, files) |
| 312 |
|
| 313 |
if len(moduleErrs) > 0 { |
| 314 |
return &moduleErrorMap{moduleErrs} |
| 315 |
} |
| 316 |
|
| 317 |
return nil |
| 318 |
} |
| 319 |
|
| 320 |
type moduleErrorMap struct { |
| 321 |
errs map[string][]packages.Error |
| 322 |
} |
| 323 |
|
| 324 |
func (m *moduleErrorMap) Error() string { |
| 325 |
var paths []string |
| 326 |
for path, errs := range m.errs { |
| 327 |
if len(errs) > 0 { |
| 328 |
paths = append(paths, path) |
| 329 |
} |
| 330 |
} |
| 331 |
sort.Strings(paths) |
| 332 |
|
| 333 |
var buf bytes.Buffer |
| 334 |
fmt.Fprintf(&buf, "%d modules have errors:\n", len(paths)) |
| 335 |
for _, path := range paths { |
| 336 |
fmt.Fprintf(&buf, "\t%s:%s\n", path, m.errs[path][0].Msg) |
| 337 |
} |
| 338 |
|
| 339 |
return buf.String() |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
func (s *Snapshot) config(ctx context.Context, allowNetwork AllowNetwork) *packages.Config { |
| 350 |
cfg := &packages.Config{ |
| 351 |
Context: ctx, |
| 352 |
Dir: s.view.root.Path(), |
| 353 |
Env: s.view.Env(), |
| 354 |
BuildFlags: slices.Clone(s.view.folder.Options.BuildFlags), |
| 355 |
Mode: packages.NeedName | |
| 356 |
packages.NeedFiles | |
| 357 |
packages.NeedCompiledGoFiles | |
| 358 |
packages.NeedImports | |
| 359 |
packages.NeedDeps | |
| 360 |
packages.NeedTypesSizes | |
| 361 |
packages.NeedModule | |
| 362 |
packages.NeedEmbedFiles | |
| 363 |
packages.LoadMode(packagesinternal.DepsErrors) | |
| 364 |
packages.NeedForTest, |
| 365 |
Fset: nil, |
| 366 |
Overlay: s.buildOverlays(), |
| 367 |
Logf: func(format string, args ...any) { |
| 368 |
if s.view.folder.Options.VerboseOutput { |
| 369 |
event.Log(ctx, fmt.Sprintf(format, args...)) |
| 370 |
} |
| 371 |
}, |
| 372 |
Tests: true, |
| 373 |
} |
| 374 |
if !allowNetwork { |
| 375 |
cfg.Env = append(cfg.Env, "GOPROXY=off") |
| 376 |
} |
| 377 |
|
| 378 |
if typesinternal.SetUsesCgo(&types.Config{}) { |
| 379 |
cfg.Mode |= packages.LoadMode(packagesinternal.TypecheckCgo) |
| 380 |
} |
| 381 |
return cfg |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
func buildMetadata(updates map[PackageID]*metadata.Package, loadDir string, standalone bool, pkg *packages.Package) *metadata.Package { |
| 392 |
|
| 393 |
pkgPath := PackagePath(pkg.PkgPath) |
| 394 |
id := PackageID(pkg.ID) |
| 395 |
|
| 396 |
if metadata.IsCommandLineArguments(id) { |
| 397 |
var f string |
| 398 |
if len(pkg.GoFiles) > 0 { |
| 399 |
f = pkg.GoFiles[0] |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
if len(pkg.GoFiles) > 1 { |
| 407 |
bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.GoFiles) |
| 408 |
return nil |
| 409 |
} |
| 410 |
} else if len(pkg.IgnoredFiles) > 0 { |
| 411 |
|
| 412 |
f = pkg.IgnoredFiles[0] |
| 413 |
} else { |
| 414 |
bug.Reportf("command-line-arguments package has neither GoFiles nor IgnoredFiles") |
| 415 |
return nil |
| 416 |
} |
| 417 |
id = PackageID(pkg.ID + f) |
| 418 |
pkgPath = PackagePath(pkg.PkgPath + f) |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
if existing, ok := updates[id]; ok { |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
return existing |
| 429 |
} |
| 430 |
|
| 431 |
if pkg.TypesSizes == nil { |
| 432 |
panic(id + ".TypeSizes is nil") |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
mp := &metadata.Package{ |
| 437 |
ID: id, |
| 438 |
PkgPath: pkgPath, |
| 439 |
Name: PackageName(pkg.Name), |
| 440 |
ForTest: PackagePath(pkg.ForTest), |
| 441 |
TypesSizes: pkg.TypesSizes, |
| 442 |
LoadDir: loadDir, |
| 443 |
Module: pkg.Module, |
| 444 |
Errors: pkg.Errors, |
| 445 |
DepsErrors: packagesinternal.GetDepsErrors(pkg), |
| 446 |
Standalone: standalone, |
| 447 |
} |
| 448 |
|
| 449 |
updates[id] = mp |
| 450 |
|
| 451 |
copyURIs := func(dst *[]protocol.DocumentURI, src []string) { |
| 452 |
for _, filename := range src { |
| 453 |
*dst = append(*dst, protocol.URIFromPath(filename)) |
| 454 |
} |
| 455 |
} |
| 456 |
copyURIs(&mp.CompiledGoFiles, pkg.CompiledGoFiles) |
| 457 |
copyURIs(&mp.GoFiles, pkg.GoFiles) |
| 458 |
copyURIs(&mp.IgnoredFiles, pkg.IgnoredFiles) |
| 459 |
copyURIs(&mp.OtherFiles, pkg.OtherFiles) |
| 460 |
|
| 461 |
depsByImpPath := make(map[ImportPath]PackageID) |
| 462 |
depsByPkgPath := make(map[PackagePath]PackageID) |
| 463 |
for importPath, imported := range pkg.Imports { |
| 464 |
importPath := ImportPath(importPath) |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
if importPath != "unsafe" && len(imported.CompiledGoFiles) == 0 { |
| 533 |
depsByImpPath[importPath] = "" |
| 534 |
continue |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
if PackageID(imported.ID) == id { |
| 540 |
if len(pkg.Errors) == 0 { |
| 541 |
bug.Reportf("self-import without error in package %s", id) |
| 542 |
} |
| 543 |
continue |
| 544 |
} |
| 545 |
|
| 546 |
dep := buildMetadata(updates, loadDir, false, imported) |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
if dep == nil || dep.ID != PackageID(imported.ID) || imported.Name == "" { |
| 561 |
depsByImpPath[importPath] = "" |
| 562 |
continue |
| 563 |
} |
| 564 |
|
| 565 |
depsByImpPath[importPath] = PackageID(imported.ID) |
| 566 |
depsByPkgPath[PackagePath(imported.PkgPath)] = PackageID(imported.ID) |
| 567 |
} |
| 568 |
mp.DepsByImpPath = depsByImpPath |
| 569 |
mp.DepsByPkgPath = depsByPkgPath |
| 570 |
return mp |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
func computeLoadDiagnostics(ctx context.Context, snapshot *Snapshot, mp *metadata.Package) []*Diagnostic { |
| 580 |
var diags []*Diagnostic |
| 581 |
for _, packagesErr := range mp.Errors { |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
if strings.Contains(packagesErr.Msg, "expected '") { |
| 586 |
continue |
| 587 |
} |
| 588 |
pkgDiags, err := goPackagesErrorDiagnostics(ctx, packagesErr, mp, snapshot) |
| 589 |
if err != nil { |
| 590 |
|
| 591 |
|
| 592 |
event.Error(ctx, "unable to compute positions for list errors", err, label.Package.Of(string(mp.ID))) |
| 593 |
continue |
| 594 |
} |
| 595 |
diags = append(diags, pkgDiags...) |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
depsDiags, err := depsErrors(ctx, snapshot, mp) |
| 602 |
if err != nil { |
| 603 |
if ctx.Err() == nil { |
| 604 |
|
| 605 |
|
| 606 |
event.Error(ctx, "unable to compute deps errors", err, label.Package.Of(string(mp.ID))) |
| 607 |
} |
| 608 |
} else { |
| 609 |
diags = append(diags, depsDiags...) |
| 610 |
} |
| 611 |
return diags |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
func isWorkspacePackageLocked(ctx context.Context, s *Snapshot, meta *metadata.Graph, pkg *metadata.Package) bool { |
| 632 |
if metadata.IsCommandLineArguments(pkg.ID) { |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if !pkg.Standalone { |
| 641 |
return false |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if allFilesHaveRealPackages(meta, pkg) { |
| 647 |
return false |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
return containsOpenFileLocked(s, pkg) |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
if containsOpenFileLocked(s, pkg) { |
| 663 |
return true |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
filterFunc := s.view.filterFunc() |
| 670 |
uris := make(map[protocol.DocumentURI]unit) |
| 671 |
for _, uri := range slices.Concat(pkg.CompiledGoFiles, pkg.GoFiles) { |
| 672 |
if !strings.Contains(string(uri), "/vendor/") && !filterFunc(uri) { |
| 673 |
uris[uri] = struct{}{} |
| 674 |
} |
| 675 |
} |
| 676 |
if len(uris) == 0 { |
| 677 |
return false |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
if !s.view.typ.usesModules() || !s.Options().ExpandWorkspaceToModule { |
| 687 |
folder := s.view.folder.Dir.Path() |
| 688 |
inFolder := false |
| 689 |
for uri := range uris { |
| 690 |
if pathutil.InDir(folder, uri.Path()) { |
| 691 |
inFolder = true |
| 692 |
break |
| 693 |
} |
| 694 |
} |
| 695 |
if !inFolder { |
| 696 |
return false |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
if s.view.typ.usesModules() { |
| 703 |
var modURI protocol.DocumentURI |
| 704 |
if pkg.Module != nil { |
| 705 |
modURI = protocol.URIFromPath(pkg.Module.GoMod) |
| 706 |
} else { |
| 707 |
|
| 708 |
|
| 709 |
if len(pkg.CompiledGoFiles) == 0 { |
| 710 |
return false |
| 711 |
} |
| 712 |
dir := pkg.CompiledGoFiles[0].Dir() |
| 713 |
var err error |
| 714 |
modURI, err = findRootPattern(ctx, dir, "go.mod", lockedSnapshot{s}) |
| 715 |
if err != nil || modURI == "" { |
| 716 |
|
| 717 |
|
| 718 |
return false |
| 719 |
} |
| 720 |
} |
| 721 |
_, ok := s.view.workspaceModFiles[modURI] |
| 722 |
return ok |
| 723 |
} |
| 724 |
|
| 725 |
return true |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
func containsOpenFileLocked(s *Snapshot, mp *metadata.Package) bool { |
| 733 |
uris := map[protocol.DocumentURI]struct{}{} |
| 734 |
for _, uri := range mp.CompiledGoFiles { |
| 735 |
uris[uri] = struct{}{} |
| 736 |
} |
| 737 |
for _, uri := range mp.GoFiles { |
| 738 |
uris[uri] = struct{}{} |
| 739 |
} |
| 740 |
|
| 741 |
for uri := range uris { |
| 742 |
fh, _ := s.files.get(uri) |
| 743 |
if _, open := fh.(*overlay); open { |
| 744 |
return true |
| 745 |
} |
| 746 |
} |
| 747 |
return false |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
func computeWorkspacePackagesLocked(ctx context.Context, s *Snapshot, meta *metadata.Graph) immutable.Map[PackageID, PackagePath] { |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
ctx = context.WithoutCancel(ctx) |
| 760 |
workspacePackages := make(map[PackageID]PackagePath) |
| 761 |
for _, mp := range meta.Packages { |
| 762 |
if !isWorkspacePackageLocked(ctx, s, meta, mp) { |
| 763 |
continue |
| 764 |
} |
| 765 |
|
| 766 |
switch { |
| 767 |
case mp.ForTest == "": |
| 768 |
|
| 769 |
workspacePackages[mp.ID] = mp.PkgPath |
| 770 |
case mp.ForTest == mp.PkgPath, mp.ForTest+"_test" == mp.PkgPath: |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
assert(!mp.IsIntermediateTestVariant(), "unexpected ITV") |
| 777 |
workspacePackages[mp.ID] = mp.ForTest |
| 778 |
} |
| 779 |
} |
| 780 |
return immutable.MapOf(workspacePackages) |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
func allFilesHaveRealPackages(g *metadata.Graph, mp *metadata.Package) bool { |
| 791 |
n := len(mp.CompiledGoFiles) |
| 792 |
checkURIs: |
| 793 |
for _, uri := range slices.Concat(mp.CompiledGoFiles[0:n:n], mp.GoFiles) { |
| 794 |
for _, pkg := range g.ForFile[uri] { |
| 795 |
if !metadata.IsCommandLineArguments(pkg.ID) { |
| 796 |
continue checkURIs |
| 797 |
} |
| 798 |
} |
| 799 |
return false |
| 800 |
} |
| 801 |
return true |
| 802 |
} |
| 803 |
|
| 804 |
func isTestMain(pkg *packages.Package, gocache string) bool { |
| 805 |
|
| 806 |
if !strings.HasSuffix(pkg.PkgPath, ".test") { |
| 807 |
return false |
| 808 |
} |
| 809 |
|
| 810 |
if pkg.Name != "main" { |
| 811 |
return false |
| 812 |
} |
| 813 |
|
| 814 |
if len(pkg.GoFiles) > 1 { |
| 815 |
return false |
| 816 |
} |
| 817 |
if !pathutil.InDir(gocache, pkg.GoFiles[0]) { |
| 818 |
return false |
| 819 |
} |
| 820 |
return true |
| 821 |
} |