@@ -299,6 +299,7 @@ func (s *snapshot) ValidBuildConfiguration() bool {
299
299
}
300
300
// The user may have a multiple directories in their GOPATH.
301
301
// Check if the workspace is within any of them.
302
+ // TODO(rfindley): this should probably be subject to "if GO111MODULES = off {...}".
302
303
for _ , gp := range filepath .SplitList (s .view .gopath ) {
303
304
if source .InDir (filepath .Join (gp , "src" ), s .view .rootURI .Filename ()) {
304
305
return true
@@ -653,7 +654,7 @@ func (s *snapshot) PackagesForFile(ctx context.Context, uri span.URI, mode sourc
653
654
ids = append (ids , m .ID )
654
655
}
655
656
656
- return s .loadPackages (ctx , mode , ids ... )
657
+ return s .TypeCheck (ctx , mode , ids ... )
657
658
}
658
659
659
660
func (s * snapshot ) PackageForFile (ctx context.Context , uri span.URI , mode source.TypecheckMode , pkgPolicy source.PackageFilter ) (source.Package , error ) {
@@ -671,7 +672,7 @@ func (s *snapshot) PackageForFile(ctx context.Context, uri span.URI, mode source
671
672
case source .WidestPackage :
672
673
metas = metas [len (metas )- 1 :]
673
674
}
674
- pkgs , err := s .loadPackages (ctx , mode , metas [0 ].ID )
675
+ pkgs , err := s .TypeCheck (ctx , mode , metas [0 ].ID )
675
676
if err != nil {
676
677
return nil , err
677
678
}
@@ -713,8 +714,8 @@ func (s *snapshot) containingPackages(ctx context.Context, uri span.URI) ([]*sou
713
714
return metas , err
714
715
}
715
716
716
- // loadPackages type-checks the specified packages in the given mode.
717
- func (s * snapshot ) loadPackages (ctx context.Context , mode source.TypecheckMode , ids ... PackageID ) ([]source.Package , error ) {
717
+ // TypeCheck type-checks the specified packages in the given mode.
718
+ func (s * snapshot ) TypeCheck (ctx context.Context , mode source.TypecheckMode , ids ... PackageID ) ([]source.Package , error ) {
718
719
// Build all the handles...
719
720
var phs []* packageHandle
720
721
for _ , id := range ids {
@@ -854,30 +855,14 @@ func (s *snapshot) getImportedBy(id PackageID) []PackageID {
854
855
return s .meta .importedBy [id ]
855
856
}
856
857
857
- func (s * snapshot ) workspacePackageIDs () (ids []PackageID ) {
858
+ func (s * snapshot ) workspaceMetadata () (meta []* source. Metadata ) {
858
859
s .mu .Lock ()
859
860
defer s .mu .Unlock ()
860
861
861
862
for id := range s .workspacePackages {
862
- ids = append (ids , id )
863
+ meta = append (meta , s . meta . metadata [ id ] )
863
864
}
864
- return ids
865
- }
866
-
867
- func (s * snapshot ) activePackageIDs () (ids []PackageID ) {
868
- if s .view .Options ().MemoryMode == source .ModeNormal {
869
- return s .workspacePackageIDs ()
870
- }
871
-
872
- s .mu .Lock ()
873
- defer s .mu .Unlock ()
874
-
875
- for id := range s .workspacePackages {
876
- if s .isActiveLocked (id ) {
877
- ids = append (ids , id )
878
- }
879
- }
880
- return ids
865
+ return meta
881
866
}
882
867
883
868
func (s * snapshot ) isActiveLocked (id PackageID ) (active bool ) {
@@ -1089,35 +1074,25 @@ func (s *snapshot) knownFilesInDir(ctx context.Context, dir span.URI) []span.URI
1089
1074
return files
1090
1075
}
1091
1076
1092
- func (s * snapshot ) ActivePackages (ctx context.Context ) ([]source.Package , error ) {
1093
- phs , err := s .activePackageHandles (ctx )
1094
- if err != nil {
1077
+ func (s * snapshot ) ActiveMetadata (ctx context.Context ) ([]* source.Metadata , error ) {
1078
+ if err := s .awaitLoaded (ctx ); err != nil {
1095
1079
return nil , err
1096
1080
}
1097
- var pkgs []source.Package
1098
- for _ , ph := range phs {
1099
- pkg , err := ph .await (ctx , s )
1100
- if err != nil {
1101
- return nil , err
1102
- }
1103
- pkgs = append (pkgs , pkg )
1104
- }
1105
- return pkgs , nil
1106
- }
1107
1081
1108
- func (s * snapshot ) activePackageHandles (ctx context.Context ) ([]* packageHandle , error ) {
1109
- if err := s .awaitLoaded (ctx ); err != nil {
1110
- return nil , err
1082
+ if s .view .Options ().MemoryMode == source .ModeNormal {
1083
+ return s .workspaceMetadata (), nil
1111
1084
}
1112
- var phs []* packageHandle
1113
- for _ , pkgID := range s .activePackageIDs () {
1114
- ph , err := s .buildPackageHandle (ctx , pkgID , s .workspaceParseMode (pkgID ))
1115
- if err != nil {
1116
- return nil , err
1085
+
1086
+ // ModeDegradeClosed
1087
+ s .mu .Lock ()
1088
+ defer s .mu .Unlock ()
1089
+ var active []* source.Metadata
1090
+ for id := range s .workspacePackages {
1091
+ if s .isActiveLocked (id ) {
1092
+ active = append (active , s .Metadata (id ))
1117
1093
}
1118
- phs = append (phs , ph )
1119
1094
}
1120
- return phs , nil
1095
+ return active , nil
1121
1096
}
1122
1097
1123
1098
// Symbols extracts and returns the symbols for each file in all the snapshot's views.
@@ -1394,8 +1369,8 @@ func (s *snapshot) GetCriticalError(ctx context.Context) *source.CriticalError {
1394
1369
// Even if packages didn't fail to load, we still may want to show
1395
1370
// additional warnings.
1396
1371
if loadErr == nil {
1397
- wsPkgs , _ := s .ActivePackages (ctx )
1398
- if msg := shouldShowAdHocPackagesWarning (s , wsPkgs ); msg != "" {
1372
+ active , _ := s .ActiveMetadata (ctx )
1373
+ if msg := shouldShowAdHocPackagesWarning (s , active ); msg != "" {
1399
1374
return & source.CriticalError {
1400
1375
MainError : errors .New (msg ),
1401
1376
}
@@ -1410,7 +1385,7 @@ func (s *snapshot) GetCriticalError(ctx context.Context) *source.CriticalError {
1410
1385
// on-demand or via orphaned file reloading.
1411
1386
//
1412
1387
// TODO(rfindley): re-evaluate this heuristic.
1413
- if containsCommandLineArguments (wsPkgs ) {
1388
+ if containsCommandLineArguments (active ) {
1414
1389
err , diags := s .workspaceLayoutError (ctx )
1415
1390
if err != nil {
1416
1391
if ctx .Err () != nil {
@@ -1445,15 +1420,9 @@ const adHocPackagesWarning = `You are outside of a module and outside of $GOPATH
1445
1420
If you are using modules, please open your editor to a directory in your module.
1446
1421
If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new.`
1447
1422
1448
- func shouldShowAdHocPackagesWarning (snapshot source.Snapshot , pkgs []source.Package ) string {
1423
+ func shouldShowAdHocPackagesWarning (snapshot source.Snapshot , active []* source.Metadata ) string {
1449
1424
if ! snapshot .ValidBuildConfiguration () {
1450
- for _ , pkg := range pkgs {
1451
- // TODO(adonovan): strength-reduce the parameter to []Metadata.
1452
- m := snapshot .Metadata (pkg .ID ())
1453
- if m == nil {
1454
- continue
1455
- }
1456
-
1425
+ for _ , m := range active {
1457
1426
// A blank entry in DepsByImpPath
1458
1427
// indicates a missing dependency.
1459
1428
for _ , importID := range m .DepsByImpPath {
@@ -1466,9 +1435,9 @@ func shouldShowAdHocPackagesWarning(snapshot source.Snapshot, pkgs []source.Pack
1466
1435
return ""
1467
1436
}
1468
1437
1469
- func containsCommandLineArguments (pkgs []source.Package ) bool {
1470
- for _ , pkg := range pkgs {
1471
- if source .IsCommandLineArguments (pkg .ID () ) {
1438
+ func containsCommandLineArguments (metas []* source.Metadata ) bool {
1439
+ for _ , m := range metas {
1440
+ if source .IsCommandLineArguments (m .ID ) {
1472
1441
return true
1473
1442
}
1474
1443
}
0 commit comments