@@ -48,8 +48,8 @@ const (
48
48
// "placeholder" Packages with only the ID set.
49
49
NeedImports
50
50
51
- // NeedDeps adds the fields requested by the LoadMode in the packages in Imports. If NeedImports
52
- // is not set NeedDeps has no effect .
51
+ // NeedDeps adds the fields requested by the LoadMode in the packages in Imports.
52
+ // If NeedImports is not set, it will be added automatically .
53
53
NeedDeps
54
54
55
55
// NeedExportsFile adds ExportsFile.
@@ -61,7 +61,7 @@ const (
61
61
// NeedSyntax adds Syntax.
62
62
NeedSyntax
63
63
64
- // NeedTypesInfo adds TypesInfo.
64
+ // NeedTypesInfo adds TypesInfo. If NeedImports is not set, it will be added automatically.
65
65
NeedTypesInfo
66
66
67
67
// NeedTypesSizes adds TypesSizes.
@@ -75,7 +75,7 @@ const (
75
75
76
76
// Deprecated: LoadImports exists for historical compatibility
77
77
// and should not be used. Please directly specify the needed fields using the Need values.
78
- LoadImports = LoadFiles | NeedImports | NeedDeps
78
+ LoadImports = LoadFiles | NeedImports
79
79
80
80
// Deprecated: LoadTypes exists for historical compatibility
81
81
// and should not be used. Please directly specify the needed fields using the Need values.
@@ -87,7 +87,7 @@ const (
87
87
88
88
// Deprecated: LoadAllSyntax exists for historical compatibility
89
89
// and should not be used. Please directly specify the needed fields using the Need values.
90
- LoadAllSyntax = LoadSyntax
90
+ LoadAllSyntax = LoadSyntax | NeedDeps
91
91
)
92
92
93
93
// A Config specifies details about how packages should be loaded.
@@ -476,6 +476,8 @@ func newLoader(cfg *Config) *loader {
476
476
}
477
477
}
478
478
}
479
+
480
+ ld .addDependingLoadModes ()
479
481
return ld
480
482
}
481
483
@@ -496,7 +498,7 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
496
498
}
497
499
lpkg := & loaderPackage {
498
500
Package : pkg ,
499
- needtypes : (ld .Mode & (NeedTypes | NeedTypesInfo ) != 0 && rootIndex < 0 ) || rootIndex >= 0 ,
501
+ needtypes : (ld .Mode & (NeedTypes | NeedTypesInfo ) != 0 && ld . Mode & NeedDeps != 0 && rootIndex < 0 ) || rootIndex >= 0 ,
500
502
needsrc : (ld .Mode & (NeedSyntax | NeedTypesInfo ) != 0 && ld .Mode & NeedDeps != 0 && rootIndex < 0 ) || rootIndex >= 0 ||
501
503
len (ld .Overlay ) > 0 || // Overlays can invalidate export data. TODO(matloob): make this check fine-grained based on dependencies on overlaid files
502
504
pkg .ExportFile == "" && pkg .PkgPath != "unsafe" ,
@@ -544,10 +546,8 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
544
546
lpkg .color = grey
545
547
stack = append (stack , lpkg ) // push
546
548
stubs := lpkg .Imports // the structure form has only stubs with the ID in the Imports
547
- // If NeedTypesInfo we need dependencies (at least for the roots) to typecheck the package.
548
549
// If NeedImports isn't set, the imports fields will all be zeroed out.
549
- // If NeedDeps isn't also set we want to keep the stubs.
550
- if ld .Mode & NeedTypesInfo != 0 || (ld .Mode & NeedImports != 0 && ld .Mode & NeedDeps != 0 ) {
550
+ if ld .Mode & NeedImports != 0 {
551
551
lpkg .Imports = make (map [string ]* Package , len (stubs ))
552
552
for importPath , ipkg := range stubs {
553
553
var importErr error
@@ -566,11 +566,8 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
566
566
continue
567
567
}
568
568
569
- // If !NeedDeps, just fill Imports for the root. No need to recurse further.
570
- if ld .Mode & NeedDeps != 0 {
571
- if visit (imp ) {
572
- lpkg .needsrc = true
573
- }
569
+ if visit (imp ) {
570
+ lpkg .needsrc = true
574
571
}
575
572
lpkg .Imports [importPath ] = imp .Package
576
573
}
@@ -587,7 +584,7 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
587
584
return lpkg .needsrc
588
585
}
589
586
590
- if ld .Mode & ( NeedImports | NeedDeps | NeedTypesInfo ) == 0 {
587
+ if ld .Mode & NeedImports == 0 {
591
588
// We do this to drop the stub import packages that we are not even going to try to resolve.
592
589
for _ , lpkg := range initial {
593
590
lpkg .Imports = nil
@@ -598,7 +595,7 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
598
595
visit (lpkg )
599
596
}
600
597
}
601
- if ld .Mode & NeedDeps != 0 { // TODO(matloob): This is only the case if NeedTypes is also set, right?
598
+ if ld .Mode & NeedImports != 0 && ld . Mode & NeedTypes != 0 {
602
599
for _ , lpkg := range srcPkgs {
603
600
// Complete type information is required for the
604
601
// immediate dependencies of each source package.
@@ -623,7 +620,6 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
623
620
}
624
621
625
622
result := make ([]* Package , len (initial ))
626
- importPlaceholders := make (map [string ]* Package )
627
623
for i , lpkg := range initial {
628
624
result [i ] = lpkg .Package
629
625
}
@@ -660,17 +656,8 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
660
656
if ld .Mode & NeedTypesSizes == 0 {
661
657
ld .pkgs [i ].TypesSizes = nil
662
658
}
663
- if ld .Mode & NeedDeps == 0 {
664
- for j , pkg := range ld .pkgs [i ].Imports {
665
- ph , ok := importPlaceholders [pkg .ID ]
666
- if ! ok {
667
- ph = & Package {ID : pkg .ID }
668
- importPlaceholders [pkg .ID ] = ph
669
- }
670
- ld .pkgs [i ].Imports [j ] = ph
671
- }
672
- }
673
659
}
660
+
674
661
return result , nil
675
662
}
676
663
@@ -691,7 +678,6 @@ func (ld *loader) loadRecursive(lpkg *loaderPackage) {
691
678
}(imp )
692
679
}
693
680
wg .Wait ()
694
-
695
681
ld .loadPackage (lpkg )
696
682
})
697
683
}
@@ -818,7 +804,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
818
804
if ipkg .Types != nil && ipkg .Types .Complete () {
819
805
return ipkg .Types , nil
820
806
}
821
- log .Fatalf ("internal error: nil Pkg importing %q from %q" , path , lpkg )
807
+ log .Fatalf ("internal error: package %q without types was imported from %q" , path , lpkg )
822
808
panic ("unreachable" )
823
809
})
824
810
@@ -829,7 +815,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
829
815
// Type-check bodies of functions only in non-initial packages.
830
816
// Example: for import graph A->B->C and initial packages {A,C},
831
817
// we can ignore function bodies in B.
832
- IgnoreFuncBodies : ( ld .Mode & ( NeedDeps | NeedTypesInfo ) == 0 ) && ! lpkg .initial ,
818
+ IgnoreFuncBodies : ld .Mode & NeedDeps == 0 && ! lpkg .initial ,
833
819
834
820
Error : appendError ,
835
821
Sizes : ld .sizes ,
@@ -1091,10 +1077,25 @@ func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error
1091
1077
return tpkg , nil
1092
1078
}
1093
1079
1080
+ // addDependingLoadModes adds dependencies for choosed LoadMode in ld.Mode
1081
+ func (ld * loader ) addDependingLoadModes () {
1082
+ if ld .Mode & NeedTypesInfo != 0 && ld .Mode & NeedImports == 0 {
1083
+ // If NeedTypesInfo, go/packages needs to do typechecking itself so it can
1084
+ // associate type info with the AST. To do so, we need the export data
1085
+ // for dependencies, which means we need to ask for the direct dependencies.
1086
+ // NeedImports is used to ask for the direct dependencies.
1087
+ ld .Mode |= NeedImports
1088
+ ld .Logf ("Added load mode dependency of NeedTypesInfo: NeedImports" )
1089
+ }
1090
+
1091
+ if ld .Mode & NeedDeps != 0 && ld .Mode & NeedImports == 0 {
1092
+ // With NeedDeps we need to load at least direct dependencies.
1093
+ // NeedImports is used to ask for the direct dependencies.
1094
+ ld .Mode |= NeedImports
1095
+ ld .Logf ("Added load mode dependency of NeedDeps: NeedImports" )
1096
+ }
1097
+ }
1098
+
1094
1099
func usesExportData (cfg * Config ) bool {
1095
- return cfg .Mode & NeedExportsFile != 0 ||
1096
- // If NeedTypes but not NeedTypesInfo we won't typecheck using sources, so we need export data.
1097
- (cfg .Mode & NeedTypes != 0 && cfg .Mode & NeedTypesInfo == 0 ) ||
1098
- // If NeedTypesInfo but not NeedDeps, we're typechecking a package using its sources plus its dependencies' export data
1099
- (cfg .Mode & NeedTypesInfo != 0 && cfg .Mode & NeedDeps == 0 )
1100
+ return cfg .Mode & NeedExportsFile != 0 || cfg .Mode & NeedTypes != 0 && cfg .Mode & NeedDeps == 0
1100
1101
}
0 commit comments