7
7
"go/parser"
8
8
"go/scanner"
9
9
"go/token"
10
+ "go/types"
10
11
"io/ioutil"
11
12
"os"
12
13
"os/exec"
@@ -58,6 +59,10 @@ func NewBuildContext(installSuffix string, buildTags []string) *build.Context {
58
59
// If an error occurs, Import returns a non-nil error and a nil
59
60
// *PackageData.
60
61
func Import (path string , mode build.ImportMode , installSuffix string , buildTags []string ) (* PackageData , error ) {
62
+ return importWithSrcDir (path , "" , mode , installSuffix , buildTags )
63
+ }
64
+
65
+ func importWithSrcDir (path string , srcDir string , mode build.ImportMode , installSuffix string , buildTags []string ) (* PackageData , error ) {
61
66
buildContext := NewBuildContext (installSuffix , buildTags )
62
67
if path == "runtime" || path == "syscall" {
63
68
buildContext .GOARCH = build .Default .GOARCH
@@ -66,7 +71,7 @@ func Import(path string, mode build.ImportMode, installSuffix string, buildTags
66
71
buildContext .InstallSuffix += "_" + installSuffix
67
72
}
68
73
}
69
- pkg , err := buildContext .Import (path , "" , mode )
74
+ pkg , err := buildContext .Import (path , srcDir , mode )
70
75
if err != nil {
71
76
return nil , err
72
77
}
@@ -292,10 +297,10 @@ type PackageData struct {
292
297
}
293
298
294
299
type Session struct {
295
- options * Options
296
- Packages map [string ]* PackageData
297
- ImportContext * compiler. ImportContext
298
- Watcher * fsnotify.Watcher
300
+ options * Options
301
+ Packages map [string ]* PackageData
302
+ Types map [ string ] * types. Package
303
+ Watcher * fsnotify.Watcher
299
304
}
300
305
301
306
func NewSession (options * Options ) * Session {
@@ -311,7 +316,7 @@ func NewSession(options *Options) *Session {
311
316
options : options ,
312
317
Packages : make (map [string ]* PackageData ),
313
318
}
314
- s .ImportContext = compiler . NewImportContext ( s . BuildImportPath )
319
+ s .Types = map [ string ] * types. Package { "unsafe" : types . Unsafe }
315
320
if options .Watch {
316
321
if out , err := exec .Command ("ulimit" , "-n" ).Output (); err == nil {
317
322
if n , err := strconv .Atoi (strings .TrimSpace (string (out ))); err == nil && n < 1024 {
@@ -381,18 +386,22 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
381
386
if err := s .BuildPackage (pkg ); err != nil {
382
387
return err
383
388
}
384
- if s .ImportContext . Packages ["main" ].Name () != "main" {
389
+ if s .Types ["main" ].Name () != "main" {
385
390
return fmt .Errorf ("cannot build/run non-main package" )
386
391
}
387
392
return s .WriteCommandPackage (pkg , pkgObj )
388
393
}
389
394
390
395
func (s * Session ) BuildImportPath (path string ) (* compiler.Archive , error ) {
396
+ return s .buildImportPathWithSrcDir (path , "" )
397
+ }
398
+
399
+ func (s * Session ) buildImportPathWithSrcDir (path string , srcDir string ) (* compiler.Archive , error ) {
391
400
if pkg , found := s .Packages [path ]; found {
392
401
return pkg .Archive , nil
393
402
}
394
403
395
- pkg , err := Import (path , 0 , s .InstallSuffix (), s .options .BuildTags )
404
+ pkg , err := importWithSrcDir (path , srcDir , 0 , s .InstallSuffix (), s .options .BuildTags )
396
405
if s .Watcher != nil && pkg != nil { // add watch even on error
397
406
s .Watcher .Add (pkg .Dir )
398
407
}
@@ -477,7 +486,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
477
486
}
478
487
defer objFile .Close ()
479
488
480
- pkg .Archive , err = compiler .ReadArchive (pkg .PkgObj , pkg .ImportPath , objFile , s .ImportContext . Packages )
489
+ pkg .Archive , err = compiler .ReadArchive (pkg .PkgObj , pkg .ImportPath , objFile , s .Types )
481
490
if err != nil {
482
491
return err
483
492
}
@@ -492,7 +501,13 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
492
501
return err
493
502
}
494
503
495
- pkg .Archive , err = compiler .Compile (pkg .ImportPath , files , fileSet , s .ImportContext , s .options .Minify )
504
+ importContext := & compiler.ImportContext {
505
+ Packages : s .Types ,
506
+ Import : func (path string ) (* compiler.Archive , error ) {
507
+ return s .buildImportPathWithSrcDir (path , pkg .Dir )
508
+ },
509
+ }
510
+ pkg .Archive , err = compiler .Compile (pkg .ImportPath , files , fileSet , importContext , s .options .Minify )
496
511
if err != nil {
497
512
return err
498
513
}
@@ -575,7 +590,7 @@ func (s *Session) WriteCommandPackage(pkg *PackageData, pkgObj string) error {
575
590
sourceMapFilter .MappingCallback = NewMappingCallback (m , s .options .GOROOT , s .options .GOPATH )
576
591
}
577
592
578
- deps , err := compiler .ImportDependencies (pkg .Archive , s .ImportContext . Import )
593
+ deps , err := compiler .ImportDependencies (pkg .Archive , s .BuildImportPath )
579
594
if err != nil {
580
595
return err
581
596
}
0 commit comments