@@ -27,6 +27,7 @@ import (
2727 "github.com/fsnotify/fsnotify"
2828 "github.com/gopherjs/gopherjs/compiler"
2929 "github.com/gopherjs/gopherjs/compiler/astutil"
30+ "github.com/gopherjs/gopherjs/internal/testmain"
3031 log "github.com/sirupsen/logrus"
3132
3233 "github.com/neelance/sourcemap"
@@ -686,14 +687,6 @@ func (p PackageData) FileModTime() time.Time {
686687 return newest
687688}
688689
689- // InternalBuildContext returns the build context that produced the package.
690- //
691- // WARNING: This function is a part of internal API and will be removed in
692- // future.
693- func (p * PackageData ) InternalBuildContext () * build.Context {
694- return p .bctx
695- }
696-
697690// TestPackage returns a variant of the package with "internal" tests.
698691func (p * PackageData ) TestPackage () * PackageData {
699692 return & PackageData {
@@ -913,11 +906,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
913906 })
914907 }
915908
916- parsed , err := s .BuildPackage (pkg )
917- if err != nil {
918- return err
919- }
920- archive , err := s .CompilePackage (parsed )
909+ archive , err := s .BuildProject (pkg )
921910 if err != nil {
922911 return err
923912 }
@@ -927,7 +916,73 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
927916 return s .WriteCommandPackage (archive , pkgObj )
928917}
929918
930- // buildImportPathWithSrcDir builds the package specified by the import path.
919+ func (s * Session ) BuildProject (pkg * PackageData ) (* compiler.Archive , error ) {
920+ var parsed * ParsedPackage
921+ var err error
922+ if pkg .IsTest {
923+ parsed , err = s .buildTestProject (pkg )
924+ } else {
925+ parsed , err = s .buildNonTestProject (pkg )
926+ }
927+ if err != nil {
928+ return nil , err
929+ }
930+
931+ // TODO(grantnelson-wf): At this point we should have all the parsed packages we need to
932+ // compile the whole project. We can perform analysis on the whole project
933+ // at this point to get the propagate flatten, blocking, etc. information
934+ // and check types to get the type info with all the instances for all
935+ // generics in the whole project.
936+
937+ return s .compilePackages (parsed )
938+ }
939+
940+ func (s * Session ) buildTestProject (pkg * PackageData ) (* ParsedPackage , error ) {
941+ tpkg := pkg .TestPackage ()
942+ tpkg .Imports = append (tpkg .Imports ,
943+ "github.com/gopherjs/gopherjs/js" ,
944+ "github.com/gopherjs/gopherjs/nosync" ,
945+ "testing/internal/testdeps" ,
946+ "runtime" ,
947+ )
948+ _ , err := s .buildPackages (tpkg )
949+ if err != nil {
950+ return nil , err
951+ }
952+ _ , err = s .buildPackages (pkg .XTestPackage ())
953+ if err != nil {
954+ return nil , err
955+ }
956+
957+ fset := token .NewFileSet ()
958+ tests := testmain.TestMain {Package : pkg .Package , Context : pkg .bctx }
959+ tests .Scan (fset )
960+ mainPkg , mainFile , err := tests .Synthesize (fset )
961+ if err != nil {
962+ return nil , fmt .Errorf ("failed to generate testmain package for %s: %w" , pkg .ImportPath , err )
963+ }
964+
965+ parsed := & ParsedPackage {
966+ ImportPath : mainPkg .ImportPath ,
967+ Dir : mainPkg .Dir ,
968+ GoFiles : []* ast.File {mainFile },
969+ FileSet : fset ,
970+ }
971+
972+ return parsed , nil
973+ }
974+
975+ func (s * Session ) buildNonTestProject (pkg * PackageData ) (* ParsedPackage , error ) {
976+ // Add some packages required by GopherJS as imports.
977+ pkg .Imports = append (pkg .Imports ,
978+ "github.com/gopherjs/gopherjs/js" ,
979+ "github.com/gopherjs/gopherjs/nosync" ,
980+ "runtime" ,
981+ )
982+ return s .buildPackages (pkg )
983+ }
984+
985+ // buildImportPathWithSrcDir builds the parsed package specified by the import path.
931986//
932987// Relative import paths are interpreted relative to the passed srcDir. If
933988// srcDir is empty, current working directory is assumed.
@@ -940,7 +995,7 @@ func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*Packag
940995 return nil , nil , err
941996 }
942997
943- parsed , err := s .recursivelyBuildPackages (pkg )
998+ parsed , err := s .buildPackages (pkg )
944999 if err != nil {
9451000 return nil , nil , err
9461001 }
@@ -974,23 +1029,7 @@ var getExeModTime = func() func() time.Time {
9741029 }
9751030}()
9761031
977- func (s * Session ) BuildPackage (pkg * PackageData ) (* ParsedPackage , error ) {
978- // If testing add in the required test imports into the package.
979- if pkg .IsTest {
980- pkg = pkg .TestPackage ()
981- // Add some packages required by GopherJS for tests as imports.
982- pkg .Imports = append (pkg .Imports ,
983- "testing/internal/testdeps" ,
984- )
985- }
986- // Add some packages required by GopherJS as imports.
987- pkg .Imports = append (pkg .Imports ,
988- "github.com/gopherjs/gopherjs/js" ,
989- "github.com/gopherjs/gopherjs/nosync" )
990- return s .recursivelyBuildPackages (pkg )
991- }
992-
993- func (s * Session ) recursivelyBuildPackages (pkg * PackageData ) (* ParsedPackage , error ) {
1032+ func (s * Session ) buildPackages (pkg * PackageData ) (* ParsedPackage , error ) {
9941033 if parsed , ok := s .ParsedPackages [pkg .ImportPath ]; ok {
9951034 return parsed , nil
9961035 }
@@ -1042,7 +1081,7 @@ func (s *Session) recursivelyBuildPackages(pkg *PackageData) (*ParsedPackage, er
10421081 return parsed , nil
10431082}
10441083
1045- func (s * Session ) CompilePackage (pkg * ParsedPackage ) (* compiler.Archive , error ) {
1084+ func (s * Session ) compilePackages (pkg * ParsedPackage ) (* compiler.Archive , error ) {
10461085 if archive , ok := s .UpToDateArchives [pkg .ImportPath ]; ok {
10471086 return archive , nil
10481087 }
@@ -1082,7 +1121,7 @@ func (s *Session) ImportResolverFor(srcDir string) func(string) (*compiler.Archi
10821121
10831122 // The archive hasn't been compiled yet so compile it with the parsed package.
10841123 if parsed , ok := s .ParsedPackages [path ]; ok {
1085- return s .CompilePackage (parsed )
1124+ return s .compilePackages (parsed )
10861125 }
10871126
10881127 return nil , fmt .Errorf (`parsed package for %q not found` , path )
0 commit comments