@@ -295,12 +295,11 @@ type PackageData struct {
295
295
IsTest bool // IsTest is true if the package is being built for running tests.
296
296
SrcModTime time.Time
297
297
UpToDate bool
298
- Archive * compiler.Archive
299
298
}
300
299
301
300
type Session struct {
302
301
options * Options
303
- Packages map [string ]* PackageData
302
+ Archives map [string ]* compiler. Archive
304
303
Types map [string ]* types.Package
305
304
Watcher * fsnotify.Watcher
306
305
}
@@ -316,7 +315,7 @@ func NewSession(options *Options) *Session {
316
315
317
316
s := & Session {
318
317
options : options ,
319
- Packages : make (map [string ]* PackageData ),
318
+ Archives : make (map [string ]* compiler. Archive ),
320
319
}
321
320
s .Types = make (map [string ]* types.Package )
322
321
if options .Watch {
@@ -356,13 +355,14 @@ func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string)
356
355
return err
357
356
}
358
357
pkg .JSFiles = jsFiles
359
- if err := s .BuildPackage (pkg ); err != nil {
358
+ archive , err := s .BuildPackage (pkg )
359
+ if err != nil {
360
360
return err
361
361
}
362
362
if pkgObj == "" {
363
363
pkgObj = filepath .Base (packagePath ) + ".js"
364
364
}
365
- if err := s .WriteCommandPackage (pkg . Archive , pkgObj ); err != nil {
365
+ if err := s .WriteCommandPackage (archive , pkgObj ); err != nil {
366
366
return err
367
367
}
368
368
return nil
@@ -385,42 +385,41 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
385
385
pkg .GoFiles = append (pkg .GoFiles , file )
386
386
}
387
387
388
- if err := s .BuildPackage (pkg ); err != nil {
388
+ archive , err := s .BuildPackage (pkg )
389
+ if err != nil {
389
390
return err
390
391
}
391
392
if s .Types ["main" ].Name () != "main" {
392
393
return fmt .Errorf ("cannot build/run non-main package" )
393
394
}
394
- return s .WriteCommandPackage (pkg . Archive , pkgObj )
395
+ return s .WriteCommandPackage (archive , pkgObj )
395
396
}
396
397
397
398
func (s * Session ) BuildImportPath (path string ) (* compiler.Archive , error ) {
398
- return s .buildImportPathWithSrcDir (path , "" )
399
+ _ , archive , err := s .buildImportPathWithSrcDir (path , "" )
400
+ return archive , err
399
401
}
400
402
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 ) {
406
404
pkg , err := importWithSrcDir (path , srcDir , 0 , s .InstallSuffix (), s .options .BuildTags )
407
405
if s .Watcher != nil && pkg != nil { // add watch even on error
408
406
s .Watcher .Add (pkg .Dir )
409
407
}
410
408
if err != nil {
411
- return nil , err
409
+ return nil , nil , err
412
410
}
413
411
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
416
415
}
417
- return pkg .Archive , nil
416
+
417
+ return pkg , archive , nil
418
418
}
419
419
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
424
423
}
425
424
426
425
if pkg .PkgObj != "" {
@@ -454,11 +453,11 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
454
453
if importedPkgPath == "unsafe" || ignored {
455
454
continue
456
455
}
457
- _ , err := s .BuildImportPath (importedPkgPath )
456
+ pkg , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , "" )
458
457
if err != nil {
459
- return err
458
+ return nil , err
460
459
}
461
- impModeTime := s . Packages [ importedPkgPath ] .SrcModTime
460
+ impModeTime := pkg .SrcModTime
462
461
if impModeTime .After (pkg .SrcModTime ) {
463
462
pkg .SrcModTime = impModeTime
464
463
}
@@ -467,7 +466,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
467
466
for _ , name := range append (pkg .GoFiles , pkg .JSFiles ... ) {
468
467
fileInfo , err := os .Stat (filepath .Join (pkg .Dir , name ))
469
468
if err != nil {
470
- return err
469
+ return nil , err
471
470
}
472
471
if fileInfo .ModTime ().After (pkg .SrcModTime ) {
473
472
pkg .SrcModTime = fileInfo .ModTime ()
@@ -479,75 +478,79 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
479
478
// package object is up to date, load from disk if library
480
479
pkg .UpToDate = true
481
480
if pkg .IsCommand () {
482
- return nil
481
+ return nil , nil
483
482
}
484
483
485
484
objFile , err := os .Open (pkg .PkgObj )
486
485
if err != nil {
487
- return err
486
+ return nil , err
488
487
}
489
488
defer objFile .Close ()
490
489
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 )
492
491
if err != nil {
493
- return err
492
+ return nil , err
494
493
}
495
494
496
- return nil
495
+ s .Archives [pkg .ImportPath ] = archive
496
+ return archive , err
497
497
}
498
498
}
499
499
500
500
fileSet := token .NewFileSet ()
501
501
files , err := parse (pkg .Package , pkg .IsTest , fileSet )
502
502
if err != nil {
503
- return err
503
+ return nil , err
504
504
}
505
505
506
506
importContext := & compiler.ImportContext {
507
507
Packages : s .Types ,
508
508
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
510
511
},
511
512
}
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 )
513
514
if err != nil {
514
- return err
515
+ return nil , err
515
516
}
516
517
517
518
for _ , jsFile := range pkg .JSFiles {
518
519
code , err := ioutil .ReadFile (filepath .Join (pkg .Dir , jsFile ))
519
520
if err != nil {
520
- return err
521
+ return nil , err
521
522
}
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 " )... )
525
526
}
526
527
527
528
if s .options .Verbose {
528
529
fmt .Println (pkg .ImportPath )
529
530
}
530
531
532
+ s .Archives [pkg .ImportPath ] = archive
533
+
531
534
if pkg .PkgObj == "" || pkg .IsCommand () {
532
- return nil
535
+ return archive , nil
533
536
}
534
537
535
- if err := s .writeLibraryPackage (pkg , pkg .PkgObj ); err != nil {
538
+ if err := s .writeLibraryPackage (archive , pkg .PkgObj ); err != nil {
536
539
if strings .HasPrefix (pkg .PkgObj , s .options .GOROOT ) {
537
540
// fall back to first GOPATH workspace
538
541
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
541
544
}
542
- return nil
545
+ return nil , nil
543
546
}
544
- return err
547
+ return nil , err
545
548
}
546
549
547
- return nil
550
+ return archive , nil
548
551
}
549
552
550
- func (s * Session ) writeLibraryPackage (pkg * PackageData , pkgObj string ) error {
553
+ func (s * Session ) writeLibraryPackage (archive * compiler. Archive , pkgObj string ) error {
551
554
if err := os .MkdirAll (filepath .Dir (pkgObj ), 0777 ); err != nil {
552
555
return err
553
556
}
@@ -558,7 +561,7 @@ func (s *Session) writeLibraryPackage(pkg *PackageData, pkgObj string) error {
558
561
}
559
562
defer objFile .Close ()
560
563
561
- return compiler .WriteArchive (pkg . Archive , objFile )
564
+ return compiler .WriteArchive (archive , objFile )
562
565
}
563
566
564
567
func (s * Session ) WriteCommandPackage (archive * compiler.Archive , pkgObj string ) error {
@@ -588,7 +591,13 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
588
591
sourceMapFilter .MappingCallback = NewMappingCallback (m , s .options .GOROOT , s .options .GOPATH )
589
592
}
590
593
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
+ })
592
601
if err != nil {
593
602
return err
594
603
}
0 commit comments