Skip to content

Commit e3a9838

Browse files
committed
refactored build package, fixed caching related to vendoring
1 parent 0e09744 commit e3a9838

File tree

2 files changed

+87
-66
lines changed

2 files changed

+87
-66
lines changed

build/build.go

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,11 @@ type PackageData struct {
295295
IsTest bool // IsTest is true if the package is being built for running tests.
296296
SrcModTime time.Time
297297
UpToDate bool
298-
Archive *compiler.Archive
299298
}
300299

301300
type Session struct {
302301
options *Options
303-
Packages map[string]*PackageData
302+
Archives map[string]*compiler.Archive
304303
Types map[string]*types.Package
305304
Watcher *fsnotify.Watcher
306305
}
@@ -316,7 +315,7 @@ func NewSession(options *Options) *Session {
316315

317316
s := &Session{
318317
options: options,
319-
Packages: make(map[string]*PackageData),
318+
Archives: make(map[string]*compiler.Archive),
320319
}
321320
s.Types = make(map[string]*types.Package)
322321
if options.Watch {
@@ -356,13 +355,14 @@ func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string)
356355
return err
357356
}
358357
pkg.JSFiles = jsFiles
359-
if err := s.BuildPackage(pkg); err != nil {
358+
archive, err := s.BuildPackage(pkg)
359+
if err != nil {
360360
return err
361361
}
362362
if pkgObj == "" {
363363
pkgObj = filepath.Base(packagePath) + ".js"
364364
}
365-
if err := s.WriteCommandPackage(pkg.Archive, pkgObj); err != nil {
365+
if err := s.WriteCommandPackage(archive, pkgObj); err != nil {
366366
return err
367367
}
368368
return nil
@@ -385,42 +385,41 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
385385
pkg.GoFiles = append(pkg.GoFiles, file)
386386
}
387387

388-
if err := s.BuildPackage(pkg); err != nil {
388+
archive, err := s.BuildPackage(pkg)
389+
if err != nil {
389390
return err
390391
}
391392
if s.Types["main"].Name() != "main" {
392393
return fmt.Errorf("cannot build/run non-main package")
393394
}
394-
return s.WriteCommandPackage(pkg.Archive, pkgObj)
395+
return s.WriteCommandPackage(archive, pkgObj)
395396
}
396397

397398
func (s *Session) BuildImportPath(path string) (*compiler.Archive, error) {
398-
return s.buildImportPathWithSrcDir(path, "")
399+
_, archive, err := s.buildImportPathWithSrcDir(path, "")
400+
return archive, err
399401
}
400402

401-
func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*compiler.Archive, error) {
402-
if pkg, found := s.Packages[path]; found {
403-
return pkg.Archive, nil
404-
}
405-
403+
func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*PackageData, *compiler.Archive, error) {
406404
pkg, err := importWithSrcDir(path, srcDir, 0, s.InstallSuffix(), s.options.BuildTags)
407405
if s.Watcher != nil && pkg != nil { // add watch even on error
408406
s.Watcher.Add(pkg.Dir)
409407
}
410408
if err != nil {
411-
return nil, err
409+
return nil, nil, err
412410
}
413411

414-
if err := s.BuildPackage(pkg); err != nil {
415-
return nil, err
412+
archive, err := s.BuildPackage(pkg)
413+
if err != nil {
414+
return nil, nil, err
416415
}
417-
return pkg.Archive, nil
416+
417+
return pkg, archive, nil
418418
}
419419

420-
func (s *Session) BuildPackage(pkg *PackageData) error {
421-
s.Packages[pkg.ImportPath] = pkg
422-
if pkg.ImportPath == "unsafe" {
423-
return nil
420+
func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
421+
if archive, ok := s.Archives[pkg.ImportPath]; ok {
422+
return archive, nil
424423
}
425424

426425
if pkg.PkgObj != "" {
@@ -454,11 +453,11 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
454453
if importedPkgPath == "unsafe" || ignored {
455454
continue
456455
}
457-
_, err := s.BuildImportPath(importedPkgPath)
456+
pkg, _, err := s.buildImportPathWithSrcDir(importedPkgPath, "")
458457
if err != nil {
459-
return err
458+
return nil, err
460459
}
461-
impModeTime := s.Packages[importedPkgPath].SrcModTime
460+
impModeTime := pkg.SrcModTime
462461
if impModeTime.After(pkg.SrcModTime) {
463462
pkg.SrcModTime = impModeTime
464463
}
@@ -467,7 +466,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
467466
for _, name := range append(pkg.GoFiles, pkg.JSFiles...) {
468467
fileInfo, err := os.Stat(filepath.Join(pkg.Dir, name))
469468
if err != nil {
470-
return err
469+
return nil, err
471470
}
472471
if fileInfo.ModTime().After(pkg.SrcModTime) {
473472
pkg.SrcModTime = fileInfo.ModTime()
@@ -479,75 +478,79 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
479478
// package object is up to date, load from disk if library
480479
pkg.UpToDate = true
481480
if pkg.IsCommand() {
482-
return nil
481+
return nil, nil
483482
}
484483

485484
objFile, err := os.Open(pkg.PkgObj)
486485
if err != nil {
487-
return err
486+
return nil, err
488487
}
489488
defer objFile.Close()
490489

491-
pkg.Archive, err = compiler.ReadArchive(pkg.PkgObj, pkg.ImportPath, objFile, s.Types)
490+
archive, err := compiler.ReadArchive(pkg.PkgObj, pkg.ImportPath, objFile, s.Types)
492491
if err != nil {
493-
return err
492+
return nil, err
494493
}
495494

496-
return nil
495+
s.Archives[pkg.ImportPath] = archive
496+
return archive, err
497497
}
498498
}
499499

500500
fileSet := token.NewFileSet()
501501
files, err := parse(pkg.Package, pkg.IsTest, fileSet)
502502
if err != nil {
503-
return err
503+
return nil, err
504504
}
505505

506506
importContext := &compiler.ImportContext{
507507
Packages: s.Types,
508508
Import: func(path string) (*compiler.Archive, error) {
509-
return s.buildImportPathWithSrcDir(path, pkg.Dir)
509+
_, archive, err := s.buildImportPathWithSrcDir(path, pkg.Dir)
510+
return archive, err
510511
},
511512
}
512-
pkg.Archive, err = compiler.Compile(pkg.ImportPath, files, fileSet, importContext, s.options.Minify)
513+
archive, err := compiler.Compile(pkg.ImportPath, files, fileSet, importContext, s.options.Minify)
513514
if err != nil {
514-
return err
515+
return nil, err
515516
}
516517

517518
for _, jsFile := range pkg.JSFiles {
518519
code, err := ioutil.ReadFile(filepath.Join(pkg.Dir, jsFile))
519520
if err != nil {
520-
return err
521+
return nil, err
521522
}
522-
pkg.Archive.IncJSCode = append(pkg.Archive.IncJSCode, []byte("\t(function() {\n")...)
523-
pkg.Archive.IncJSCode = append(pkg.Archive.IncJSCode, code...)
524-
pkg.Archive.IncJSCode = append(pkg.Archive.IncJSCode, []byte("\n\t}).call($global);\n")...)
523+
archive.IncJSCode = append(archive.IncJSCode, []byte("\t(function() {\n")...)
524+
archive.IncJSCode = append(archive.IncJSCode, code...)
525+
archive.IncJSCode = append(archive.IncJSCode, []byte("\n\t}).call($global);\n")...)
525526
}
526527

527528
if s.options.Verbose {
528529
fmt.Println(pkg.ImportPath)
529530
}
530531

532+
s.Archives[pkg.ImportPath] = archive
533+
531534
if pkg.PkgObj == "" || pkg.IsCommand() {
532-
return nil
535+
return archive, nil
533536
}
534537

535-
if err := s.writeLibraryPackage(pkg, pkg.PkgObj); err != nil {
538+
if err := s.writeLibraryPackage(archive, pkg.PkgObj); err != nil {
536539
if strings.HasPrefix(pkg.PkgObj, s.options.GOROOT) {
537540
// fall back to first GOPATH workspace
538541
firstGopathWorkspace := filepath.SplitList(s.options.GOPATH)[0]
539-
if err := s.writeLibraryPackage(pkg, filepath.Join(firstGopathWorkspace, pkg.PkgObj[len(s.options.GOROOT):])); err != nil {
540-
return err
542+
if err := s.writeLibraryPackage(archive, filepath.Join(firstGopathWorkspace, pkg.PkgObj[len(s.options.GOROOT):])); err != nil {
543+
return nil, err
541544
}
542-
return nil
545+
return nil, nil
543546
}
544-
return err
547+
return nil, err
545548
}
546549

547-
return nil
550+
return archive, nil
548551
}
549552

550-
func (s *Session) writeLibraryPackage(pkg *PackageData, pkgObj string) error {
553+
func (s *Session) writeLibraryPackage(archive *compiler.Archive, pkgObj string) error {
551554
if err := os.MkdirAll(filepath.Dir(pkgObj), 0777); err != nil {
552555
return err
553556
}
@@ -558,7 +561,7 @@ func (s *Session) writeLibraryPackage(pkg *PackageData, pkgObj string) error {
558561
}
559562
defer objFile.Close()
560563

561-
return compiler.WriteArchive(pkg.Archive, objFile)
564+
return compiler.WriteArchive(archive, objFile)
562565
}
563566

564567
func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string) error {
@@ -588,7 +591,13 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
588591
sourceMapFilter.MappingCallback = NewMappingCallback(m, s.options.GOROOT, s.options.GOPATH)
589592
}
590593

591-
deps, err := compiler.ImportDependencies(archive, s.BuildImportPath)
594+
deps, err := compiler.ImportDependencies(archive, func(path string) (*compiler.Archive, error) {
595+
if archive, ok := s.Archives[path]; ok {
596+
return archive, nil
597+
}
598+
_, archive, err := s.buildImportPathWithSrcDir(path, "")
599+
return archive, err
600+
})
592601
if err != nil {
593602
return err
594603
}

tool.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ func main() {
124124
if err != nil {
125125
return err
126126
}
127-
if err := s.BuildPackage(pkg); err != nil {
127+
archive, err := s.BuildPackage(pkg)
128+
if err != nil {
128129
return err
129130
}
130131
if pkgObj == "" {
131132
pkgObj = filepath.Base(args[0]) + ".js"
132133
}
133-
if err := s.WriteCommandPackage(pkg.Archive, pkgObj); err != nil {
134+
if err := s.WriteCommandPackage(archive, pkgObj); err != nil {
134135
return err
135136
}
136137
}
@@ -186,12 +187,22 @@ func main() {
186187
}
187188
for _, pkgPath := range pkgs {
188189
pkgPath = filepath.ToSlash(pkgPath)
189-
if _, err := s.BuildImportPath(pkgPath); err != nil {
190+
191+
pkg, err := gbuild.Import(pkgPath, 0, s.InstallSuffix(), options.BuildTags)
192+
if s.Watcher != nil && pkg != nil { // add watch even on error
193+
s.Watcher.Add(pkg.Dir)
194+
}
195+
if err != nil {
196+
return err
197+
}
198+
199+
archive, err := s.BuildPackage(pkg)
200+
if err != nil {
190201
return err
191202
}
192-
pkg := s.Packages[pkgPath]
203+
193204
if pkg.IsCommand() && !pkg.UpToDate {
194-
if err := s.WriteCommandPackage(pkg.Archive, pkg.PkgObj); err != nil {
205+
if err := s.WriteCommandPackage(archive, pkg.PkgObj); err != nil {
195206
return err
196207
}
197208
}
@@ -316,11 +327,12 @@ func main() {
316327
s := gbuild.NewSession(options)
317328
tests := &testFuncs{Package: pkg.Package}
318329
collectTests := func(testPkg *gbuild.PackageData, testPkgName string, needVar *bool) error {
319-
if err := s.BuildPackage(testPkg); err != nil {
330+
archive, err := s.BuildPackage(testPkg)
331+
if err != nil {
320332
return err
321333
}
322334

323-
for _, decl := range testPkg.Archive.Declarations {
335+
for _, decl := range archive.Declarations {
324336
if strings.HasPrefix(decl.FullName, testPkg.ImportPath+".Test") {
325337
tests.Tests = append(tests.Tests, testFunc{Package: testPkgName, Name: decl.FullName[len(testPkg.ImportPath)+1:]})
326338
*needVar = true
@@ -369,17 +381,16 @@ func main() {
369381
return err
370382
}
371383

372-
mainPkg := &gbuild.PackageData{
373-
Package: &build.Package{
374-
Name: "main",
375-
ImportPath: "main",
376-
},
377-
}
378384
importContext := &compiler.ImportContext{
379385
Packages: s.Types,
380-
Import: s.BuildImportPath,
386+
Import: func(path string) (*compiler.Archive, error) {
387+
if path == pkg.ImportPath || path == pkg.ImportPath+"_test" {
388+
return s.Archives[path], nil
389+
}
390+
return s.BuildImportPath(path)
391+
},
381392
}
382-
mainPkg.Archive, err = compiler.Compile("main", []*ast.File{mainFile}, fset, importContext, options.Minify)
393+
mainPkgArchive, err := compiler.Compile("main", []*ast.File{mainFile}, fset, importContext, options.Minify)
383394
if err != nil {
384395
return err
385396
}
@@ -408,7 +419,7 @@ func main() {
408419
}
409420
}()
410421

411-
if err := s.WriteCommandPackage(mainPkg.Archive, outfile.Name()); err != nil {
422+
if err := s.WriteCommandPackage(mainPkgArchive, outfile.Name()); err != nil {
412423
return err
413424
}
414425

@@ -554,15 +565,16 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
554565
buf := bytes.NewBuffer(nil)
555566
browserErrors := bytes.NewBuffer(nil)
556567
exitCode := handleError(func() error {
557-
if err := s.BuildPackage(pkg); err != nil {
568+
archive, err := s.BuildPackage(pkg)
569+
if err != nil {
558570
return err
559571
}
560572

561573
sourceMapFilter := &compiler.SourceMapFilter{Writer: buf}
562574
m := &sourcemap.Map{File: base + ".js"}
563575
sourceMapFilter.MappingCallback = gbuild.NewMappingCallback(m, fs.options.GOROOT, fs.options.GOPATH)
564576

565-
deps, err := compiler.ImportDependencies(pkg.Archive, s.BuildImportPath)
577+
deps, err := compiler.ImportDependencies(archive, s.BuildImportPath)
566578
if err != nil {
567579
return err
568580
}

0 commit comments

Comments
 (0)