Skip to content

Commit 82034ad

Browse files
using Sources instead of ParsedPackage
1 parent 2a9b9a4 commit 82034ad

File tree

12 files changed

+333
-277
lines changed

12 files changed

+333
-277
lines changed

build/build.go

Lines changed: 47 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import (
2727
"github.com/fsnotify/fsnotify"
2828
"github.com/gopherjs/gopherjs/compiler"
2929
"github.com/gopherjs/gopherjs/compiler/astutil"
30+
"github.com/gopherjs/gopherjs/compiler/errorList"
31+
"github.com/gopherjs/gopherjs/compiler/jsFile"
32+
"github.com/gopherjs/gopherjs/compiler/sources"
3033
"github.com/gopherjs/gopherjs/internal/testmain"
3134
log "github.com/sirupsen/logrus"
3235

@@ -163,7 +166,7 @@ type overrideInfo struct {
163166
// - Otherwise for identifiers that exist in the original and the overrides,
164167
// the original is removed.
165168
// - New identifiers that don't exist in original package get added.
166-
func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]*ast.File, []JSFile, error) {
169+
func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]*ast.File, []jsFile.JSFile, error) {
167170
jsFiles, overlayFiles := parseOverlayFiles(xctx, pkg, isTest, fileSet)
168171

169172
originalFiles, err := parserOriginalFiles(pkg, fileSet)
@@ -192,7 +195,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
192195

193196
// parseOverlayFiles loads and parses overlay files
194197
// to augment the original files with.
195-
func parseOverlayFiles(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]JSFile, []*ast.File) {
198+
func parseOverlayFiles(xctx XContext, pkg *PackageData, isTest bool, fileSet *token.FileSet) ([]jsFile.JSFile, []*ast.File) {
196199
isXTest := strings.HasSuffix(pkg.ImportPath, "_test")
197200
importPath := pkg.ImportPath
198201
if isXTest {
@@ -238,7 +241,7 @@ func parseOverlayFiles(xctx XContext, pkg *PackageData, isTest bool, fileSet *to
238241
// parserOriginalFiles loads and parses the original files to augment.
239242
func parserOriginalFiles(pkg *PackageData, fileSet *token.FileSet) ([]*ast.File, error) {
240243
var files []*ast.File
241-
var errList compiler.ErrorList
244+
var errList errorList.ErrorList
242245
for _, name := range pkg.GoFiles {
243246
if !filepath.IsAbs(name) { // name might be absolute if specified directly. E.g., `gopherjs build /abs/file.go`.
244247
name = filepath.Join(pkg.Dir, name)
@@ -622,18 +625,11 @@ func (o *Options) PrintSuccess(format string, a ...interface{}) {
622625
fmt.Fprintf(os.Stderr, format, a...)
623626
}
624627

625-
// JSFile represents a *.inc.js file metadata and content.
626-
type JSFile struct {
627-
Path string // Full file path for the build context the file came from.
628-
ModTime time.Time
629-
Content []byte
630-
}
631-
632628
// PackageData is an extension of go/build.Package with additional metadata
633629
// GopherJS requires.
634630
type PackageData struct {
635631
*build.Package
636-
JSFiles []JSFile
632+
JSFiles []jsFile.JSFile
637633
// IsTest is true if the package is being built for running tests.
638634
IsTest bool
639635
SrcModTime time.Time
@@ -743,43 +739,6 @@ func (p *PackageData) InstallPath() string {
743739
return p.PkgObj
744740
}
745741

746-
// ParsedPackage is the results from building a package before it is compiled.
747-
type ParsedPackage struct {
748-
ImportPath string // import path of package ("" if unknown)
749-
Dir string // directory containing package sources
750-
751-
// GoFiles is the parsed and augmented Go AST files for the package.
752-
GoFiles []*ast.File
753-
FileSet *token.FileSet
754-
JSFiles []JSFile
755-
}
756-
757-
// Imports calculates the import paths of the package's dependencies
758-
// based on all the imports in the augmented files.
759-
//
760-
// The given skip paths will not be returned in the results.
761-
// This will not return any `*_test` packages in the results.
762-
func (p *ParsedPackage) Imports(skip ...string) []string {
763-
seen := make(map[string]struct{})
764-
for _, s := range skip {
765-
seen[s] = struct{}{}
766-
}
767-
imports := []string{}
768-
for _, file := range p.GoFiles {
769-
for _, imp := range file.Imports {
770-
path := strings.Trim(imp.Path.Value, `"`)
771-
if _, ok := seen[path]; !ok {
772-
if !strings.HasSuffix(path, "_test") {
773-
imports = append(imports, path)
774-
}
775-
seen[path] = struct{}{}
776-
}
777-
}
778-
}
779-
sort.Strings(imports)
780-
return imports
781-
}
782-
783742
// Session manages internal state GopherJS requires to perform a build.
784743
//
785744
// This is the main interface to GopherJS build system. Session lifetime is
@@ -793,10 +752,11 @@ type Session struct {
793752
// compilation when we're looking up parsed packages.
794753
importPaths map[string]map[string]string
795754

796-
// parsePackage is a map of parsed packages that have been built and augmented.
755+
// sources is a map of parsed packages that have been built and augmented.
797756
// This is keyed using resolved import paths. This is used to avoid
798757
// rebuilding and augmenting packages that are imported by several packages.
799-
parsedPackages map[string]*ParsedPackage
758+
// These sources haven't been sorted nor simplified yet.
759+
sources map[string]*sources.Sources
800760

801761
// Binary archives produced during the current session and assumed to be
802762
// up to date with input sources and dependencies. In the -w ("watch") mode
@@ -813,7 +773,7 @@ func NewSession(options *Options) (*Session, error) {
813773
s := &Session{
814774
options: options,
815775
importPaths: make(map[string]map[string]string),
816-
parsedPackages: make(map[string]*ParsedPackage),
776+
sources: make(map[string]*sources.Sources),
817777
UpToDateArchives: make(map[string]*compiler.Archive),
818778
}
819779
s.xctx = NewBuildContext(s.InstallSuffix(), s.options.BuildTags)
@@ -931,7 +891,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
931891
if err != nil {
932892
return fmt.Errorf("failed to stat %s: %w", file, err)
933893
}
934-
pkg.JSFiles = append(pkg.JSFiles, JSFile{
894+
pkg.JSFiles = append(pkg.JSFiles, jsFile.JSFile{
935895
Path: filepath.Join(pkg.Dir, filepath.Base(file)),
936896
ModTime: info.ModTime(),
937897
Content: content,
@@ -954,13 +914,13 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
954914
// ensure that runtime for gopherjs is imported
955915
pkg.Imports = append(pkg.Imports, `runtime`)
956916

957-
// Build the project to get the parsed packages.
958-
var parsed *ParsedPackage
917+
// Build the project to get the sources for the parsed packages.
918+
var srcs *sources.Sources
959919
var err error
960920
if pkg.IsTest {
961-
parsed, err = s.buildTestPackage(pkg)
921+
srcs, err = s.buildTestPackage(pkg)
962922
} else {
963-
parsed, err = s.buildPackages(pkg)
923+
srcs, err = s.buildPackages(pkg)
964924
}
965925
if err != nil {
966926
return nil, err
@@ -972,10 +932,10 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
972932
// flatten, blocking, etc. information and check types to get the type info
973933
// with all the instances for all generics in the whole project.
974934

975-
return s.compilePackages(parsed)
935+
return s.compilePackages(srcs)
976936
}
977937

978-
func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) {
938+
func (s *Session) buildTestPackage(pkg *PackageData) (*sources.Sources, error) {
979939
_, err := s.buildPackages(pkg.TestPackage())
980940
if err != nil {
981941
return nil, err
@@ -994,30 +954,30 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) {
994954
return nil, fmt.Errorf("failed to generate testmain package for %s: %w", pkg.ImportPath, err)
995955
}
996956

997-
// Create a parsed package for the testmain package.
998-
parsed := &ParsedPackage{
957+
// Create the sources for parsed package for the testmain package.
958+
srcs := &sources.Sources{
999959
ImportPath: mainPkg.ImportPath,
1000960
Dir: mainPkg.Dir,
1001-
GoFiles: []*ast.File{mainFile},
961+
Files: []*ast.File{mainFile},
1002962
FileSet: fset,
1003963
}
1004964

1005965
// Import dependencies for the testmain package.
1006-
for _, importedPkgPath := range parsed.Imports() {
966+
for _, importedPkgPath := range srcs.Imports() {
1007967
_, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
1008968
if err != nil {
1009969
return nil, err
1010970
}
1011971
}
1012972

1013-
return parsed, nil
973+
return srcs, nil
1014974
}
1015975

1016-
// buildImportPathWithSrcDir builds the parsed package specified by the import path.
976+
// buildImportPathWithSrcDir builds the sources for a package specified by the import path.
1017977
//
1018978
// Relative import paths are interpreted relative to the passed srcDir. If
1019979
// srcDir is empty, current working directory is assumed.
1020-
func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, *ParsedPackage, error) {
980+
func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, *sources.Sources, error) {
1021981
pkg, err := s.xctx.Import(path, srcDir, 0)
1022982
if s.Watcher != nil && pkg != nil { // add watch even on error
1023983
s.Watcher.Add(pkg.Dir)
@@ -1026,13 +986,13 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
1026986
return nil, nil, err
1027987
}
1028988

1029-
parsed, err := s.buildPackages(pkg)
989+
srcs, err := s.buildPackages(pkg)
1030990
if err != nil {
1031991
return nil, nil, err
1032992
}
1033993

1034994
s.cacheImportPath(path, srcDir, pkg.ImportPath)
1035-
return pkg, parsed, nil
995+
return pkg, srcs, nil
1036996
}
1037997

1038998
// cacheImportPath stores the import path for the build package so we can look
@@ -1073,9 +1033,9 @@ var getExeModTime = func() func() time.Time {
10731033
}
10741034
}()
10751035

1076-
func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
1077-
if parsed, ok := s.parsedPackages[pkg.ImportPath]; ok {
1078-
return parsed, nil
1036+
func (s *Session) buildPackages(pkg *PackageData) (*sources.Sources, error) {
1037+
if srcs, ok := s.sources[pkg.ImportPath]; ok {
1038+
return srcs, nil
10791039
}
10801040

10811041
if exeModTime := getExeModTime(); exeModTime.After(pkg.SrcModTime) {
@@ -1114,52 +1074,52 @@ func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
11141074
files = append(files, embed)
11151075
}
11161076

1117-
parsed := &ParsedPackage{
1077+
srcs := &sources.Sources{
11181078
ImportPath: pkg.ImportPath,
11191079
Dir: pkg.Dir,
1120-
GoFiles: files,
1080+
Files: files,
11211081
FileSet: fileSet,
11221082
JSFiles: append(pkg.JSFiles, overlayJsFiles...),
11231083
}
1124-
s.parsedPackages[pkg.ImportPath] = parsed
1084+
s.sources[pkg.ImportPath] = srcs
11251085

11261086
// Import dependencies from the augmented files,
11271087
// whilst skipping any that have been already imported.
1128-
for _, importedPkgPath := range parsed.Imports(pkg.Imports...) {
1088+
for _, importedPkgPath := range srcs.Imports(pkg.Imports...) {
11291089
_, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
11301090
if err != nil {
11311091
return nil, err
11321092
}
11331093
}
11341094

1135-
return parsed, nil
1095+
return srcs, nil
11361096
}
11371097

1138-
func (s *Session) compilePackages(pkg *ParsedPackage) (*compiler.Archive, error) {
1139-
if archive, ok := s.UpToDateArchives[pkg.ImportPath]; ok {
1098+
func (s *Session) compilePackages(srcs *sources.Sources) (*compiler.Archive, error) {
1099+
if archive, ok := s.UpToDateArchives[srcs.ImportPath]; ok {
11401100
return archive, nil
11411101
}
11421102

11431103
importContext := &compiler.ImportContext{
1144-
Packages: s.Types,
1145-
Import: s.ImportResolverFor(pkg.Dir),
1104+
Packages: s.Types,
1105+
ImportArchive: s.ImportResolverFor(srcs.Dir),
11461106
}
1147-
archive, err := compiler.Compile(pkg.ImportPath, pkg.GoFiles, pkg.FileSet, importContext, s.options.Minify)
1107+
archive, err := compiler.Compile(*srcs, importContext, s.options.Minify)
11481108
if err != nil {
11491109
return nil, err
11501110
}
11511111

1152-
for _, jsFile := range pkg.JSFiles {
1112+
for _, jsFile := range srcs.JSFiles {
11531113
archive.IncJSCode = append(archive.IncJSCode, []byte("\t(function() {\n")...)
11541114
archive.IncJSCode = append(archive.IncJSCode, jsFile.Content...)
11551115
archive.IncJSCode = append(archive.IncJSCode, []byte("\n\t}).call($global);\n")...)
11561116
}
11571117

11581118
if s.options.Verbose {
1159-
fmt.Println(pkg.ImportPath)
1119+
fmt.Println(srcs.ImportPath)
11601120
}
11611121

1162-
s.UpToDateArchives[pkg.ImportPath] = archive
1122+
s.UpToDateArchives[srcs.ImportPath] = archive
11631123

11641124
return archive, nil
11651125
}
@@ -1197,12 +1157,12 @@ func (s *Session) ImportResolverFor(srcDir string) func(string) (*compiler.Archi
11971157
return archive, nil
11981158
}
11991159

1200-
// The archive hasn't been compiled yet so compile it with the parsed package.
1201-
if parsed, ok := s.parsedPackages[importPath]; ok {
1202-
return s.compilePackages(parsed)
1160+
// The archive hasn't been compiled yet so compile it with the sources.
1161+
if srcs, ok := s.sources[importPath]; ok {
1162+
return s.compilePackages(srcs)
12031163
}
12041164

1205-
return nil, fmt.Errorf(`parsed package for %q not found`, importPath)
1165+
return nil, fmt.Errorf(`sources for %q not found`, importPath)
12061166
}
12071167
}
12081168

build/context.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"go/build"
66
"go/token"
7-
"io"
87
"net/http"
98
"os"
109
"os/exec"
@@ -16,6 +15,7 @@ import (
1615
_ "github.com/gopherjs/gopherjs/build/versionhack" // go/build release tags hack.
1716
"github.com/gopherjs/gopherjs/compiler"
1817
"github.com/gopherjs/gopherjs/compiler/gopherjspkg"
18+
"github.com/gopherjs/gopherjs/compiler/jsFile"
1919
"github.com/gopherjs/gopherjs/compiler/natives"
2020
"golang.org/x/tools/go/buildutil"
2121
)
@@ -91,7 +91,7 @@ func (sc simpleCtx) Import(importPath string, srcDir string, mode build.ImportMo
9191
if err != nil {
9292
return nil, err
9393
}
94-
jsFiles, err := jsFilesFromDir(&sc.bctx, pkg.Dir)
94+
jsFiles, err := jsFile.JSFilesFromDir(&sc.bctx, pkg.Dir)
9595
if err != nil {
9696
return nil, fmt.Errorf("failed to enumerate .inc.js files in %s: %w", pkg.Dir, err)
9797
}
@@ -440,40 +440,3 @@ func updateImports(sources []string, importPos map[string][]token.Position) (new
440440
sort.Strings(newImports)
441441
return newImports, newImportPos
442442
}
443-
444-
// jsFilesFromDir finds and loads any *.inc.js packages in the build context
445-
// directory.
446-
func jsFilesFromDir(bctx *build.Context, dir string) ([]JSFile, error) {
447-
files, err := buildutil.ReadDir(bctx, dir)
448-
if err != nil {
449-
return nil, err
450-
}
451-
var jsFiles []JSFile
452-
for _, file := range files {
453-
if !strings.HasSuffix(file.Name(), ".inc.js") || file.IsDir() {
454-
continue
455-
}
456-
if file.Name()[0] == '_' || file.Name()[0] == '.' {
457-
continue // Skip "hidden" files that are typically ignored by the Go build system.
458-
}
459-
460-
path := buildutil.JoinPath(bctx, dir, file.Name())
461-
f, err := buildutil.OpenFile(bctx, path)
462-
if err != nil {
463-
return nil, fmt.Errorf("failed to open %s from %v: %w", path, bctx, err)
464-
}
465-
defer f.Close()
466-
467-
content, err := io.ReadAll(f)
468-
if err != nil {
469-
return nil, fmt.Errorf("failed to read %s from %v: %w", path, bctx, err)
470-
}
471-
472-
jsFiles = append(jsFiles, JSFile{
473-
Path: path,
474-
ModTime: file.ModTime(),
475-
Content: content,
476-
})
477-
}
478-
return jsFiles, nil
479-
}

0 commit comments

Comments
 (0)