From a594ec1e4a5ba2207696f74134ff04f936392ae7 Mon Sep 17 00:00:00 2001 From: Grant Nelson Date: Mon, 10 Feb 2025 13:59:31 -0700 Subject: [PATCH 1/3] separating parse and compile --- build/build.go | 255 +++++++++++++++++++++++------ internal/testmain/testmain.go | 20 +-- internal/testmain/testmain_test.go | 11 +- tool.go | 35 +--- 4 files changed, 223 insertions(+), 98 deletions(-) diff --git a/build/build.go b/build/build.go index 2ec712fc7..300e295bd 100644 --- a/build/build.go +++ b/build/build.go @@ -27,12 +27,11 @@ import ( "github.com/fsnotify/fsnotify" "github.com/gopherjs/gopherjs/compiler" "github.com/gopherjs/gopherjs/compiler/astutil" + "github.com/gopherjs/gopherjs/internal/testmain" log "github.com/sirupsen/logrus" "github.com/neelance/sourcemap" "golang.org/x/tools/go/buildutil" - - "github.com/gopherjs/gopherjs/build/cache" ) // DefaultGOROOT is the default GOROOT value for builds. @@ -744,14 +743,60 @@ func (p *PackageData) InstallPath() string { return p.PkgObj } +// ParsedPackage is the results from building a package before it is compiled. +type ParsedPackage struct { + ImportPath string // import path of package ("" if unknown) + Dir string // directory containing package sources + + // GoFiles is the parsed and augmented Go AST files for the package. + GoFiles []*ast.File + FileSet *token.FileSet + JSFiles []JSFile +} + +// Imports calculates the import paths of the package's dependencies +// based on all the imports in the augmented files. +// +// The given skip paths will not be returned in the results. +// This will not return any `*_test` packages in the results. +func (p *ParsedPackage) Imports(skip ...string) []string { + seen := make(map[string]struct{}) + for _, s := range skip { + seen[s] = struct{}{} + } + imports := []string{} + for _, file := range p.GoFiles { + for _, imp := range file.Imports { + path := strings.Trim(imp.Path.Value, `"`) + if _, ok := seen[path]; !ok { + if !strings.HasSuffix(path, "_test") { + imports = append(imports, path) + } + seen[path] = struct{}{} + } + } + } + sort.Strings(imports) + return imports +} + // Session manages internal state GopherJS requires to perform a build. // // This is the main interface to GopherJS build system. Session lifetime is // roughly equivalent to a single GopherJS tool invocation. type Session struct { - options *Options - xctx XContext - buildCache cache.BuildCache + options *Options + xctx XContext + + // importPaths is a map of the resolved import paths given the srcDir + // and path. This is used to avoid redetermining build packages during + // compilation when we're looking up parsed packages. + importPaths map[string]map[string]string + + // parsePackage is a map of parsed packages that have been built and augmented. + // This is keyed using resolved import paths. This is used to avoid + // rebuilding and augmenting packages that are imported by several packages. + parsedPackages map[string]*ParsedPackage // Binary archives produced during the current session and assumed to be // up to date with input sources and dependencies. In the -w ("watch") mode @@ -767,6 +812,8 @@ func NewSession(options *Options) (*Session, error) { s := &Session{ options: options, + importPaths: make(map[string]map[string]string), + parsedPackages: make(map[string]*ParsedPackage), UpToDateArchives: make(map[string]*compiler.Archive), } s.xctx = NewBuildContext(s.InstallSuffix(), s.options.BuildTags) @@ -777,15 +824,6 @@ func NewSession(options *Options) (*Session, error) { return nil, err } - s.buildCache = cache.BuildCache{ - GOOS: env.GOOS, - GOARCH: env.GOARCH, - GOROOT: env.GOROOT, - GOPATH: env.GOPATH, - BuildTags: append([]string{}, env.BuildTags...), - Minify: options.Minify, - TestedPackage: options.TestedPackage, - } s.Types = make(map[string]*types.Package) if options.Watch { if out, err := exec.Command("ulimit", "-n").Output(); err == nil { @@ -900,7 +938,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro }) } - archive, err := s.BuildPackage(pkg) + archive, err := s.BuildProject(pkg) if err != nil { return err } @@ -910,19 +948,76 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro return s.WriteCommandPackage(archive, pkgObj) } -// BuildImportPath loads and compiles package with the given import path. -// -// Relative paths are interpreted relative to the current working dir. -func (s *Session) BuildImportPath(path string) (*compiler.Archive, error) { - _, archive, err := s.buildImportPathWithSrcDir(path, "") - return archive, err +// BuildProject builds a command project (one with a main method) or +// builds a test project (one with a synthesized test main package). +func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) { + // ensure that runtime for gopherjs is imported + pkg.Imports = append(pkg.Imports, `runtime`) + + // Build the project to get the parsed packages. + var parsed *ParsedPackage + var err error + if pkg.IsTest { + parsed, err = s.buildTestPackage(pkg) + } else { + parsed, err = s.buildPackages(pkg) + } + if err != nil { + return nil, err + } + + // TODO(grantnelson-wf): At this point we have all the parsed packages we + // need to compile the whole project, including testmain, if needed. + // We can perform analysis on the whole project at this point to propagate + // flatten, blocking, etc. information and check types to get the type info + // with all the instances for all generics in the whole project. + + return s.compilePackages(parsed) +} + +func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) { + _, err := s.buildPackages(pkg.TestPackage()) + if err != nil { + return nil, err + } + _, err = s.buildPackages(pkg.XTestPackage()) + if err != nil { + return nil, err + } + + // Generate a synthetic testmain package. + fset := token.NewFileSet() + tests := testmain.TestMain{Package: pkg.Package, Context: pkg.bctx} + tests.Scan(fset) + mainPkg, mainFile, err := tests.Synthesize(fset) + if err != nil { + return nil, fmt.Errorf("failed to generate testmain package for %s: %w", pkg.ImportPath, err) + } + + // Create a parsed package for the testmain package. + parsed := &ParsedPackage{ + ImportPath: mainPkg.ImportPath, + Dir: mainPkg.Dir, + GoFiles: []*ast.File{mainFile}, + FileSet: fset, + } + + // Import dependencies for the testmain package. + for _, importedPkgPath := range parsed.Imports() { + _, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir) + if err != nil { + return nil, err + } + } + + return parsed, nil } -// buildImportPathWithSrcDir builds the package specified by the import path. +// buildImportPathWithSrcDir builds the parsed package specified by the import path. // // Relative import paths are interpreted relative to the passed srcDir. If // srcDir is empty, current working directory is assumed. -func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*PackageData, *compiler.Archive, error) { +func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, *ParsedPackage, error) { pkg, err := s.xctx.Import(path, srcDir, 0) if s.Watcher != nil && pkg != nil { // add watch even on error s.Watcher.Add(pkg.Dir) @@ -931,12 +1026,25 @@ func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*Packag return nil, nil, err } - archive, err := s.BuildPackage(pkg) + parsed, err := s.buildPackages(pkg) if err != nil { return nil, nil, err } - return pkg, archive, nil + s.cacheImportPath(path, srcDir, pkg.ImportPath) + return pkg, parsed, nil +} + +// cacheImportPath stores the import path for the build package so we can look +// it up later without getting the whole build package. +// The given path and source directly are the ones passed into +// XContext.Import to the get the build package originally. +func (s *Session) cacheImportPath(path, srcDir, importPath string) { + if paths, ok := s.importPaths[srcDir]; ok { + paths[path] = importPath + } else { + s.importPaths[srcDir] = map[string]string{path: importPath} + } } // getExeModTime will determine the mod time of the GopherJS binary @@ -965,10 +1073,9 @@ var getExeModTime = func() func() time.Time { } }() -// BuildPackage compiles an already loaded package. -func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) { - if archive, ok := s.UpToDateArchives[pkg.ImportPath]; ok { - return archive, nil +func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) { + if parsed, ok := s.parsedPackages[pkg.ImportPath]; ok { + return parsed, nil } if exeModTime := getExeModTime(); exeModTime.After(pkg.SrcModTime) { @@ -993,16 +1100,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) { pkg.SrcModTime = fileModTime } - if !s.options.NoCache { - archive := s.buildCache.LoadArchive(pkg.ImportPath, pkg.SrcModTime, s.Types) - if archive != nil { - s.UpToDateArchives[pkg.ImportPath] = archive - // Existing archive is up to date, no need to build it from scratch. - return archive, nil - } - } - - // Existing archive is out of date or doesn't exist, let's build the package. + // Build the package by parsing and augmenting the original files with overlay files. fileSet := token.NewFileSet() files, overlayJsFiles, err := parseAndAugment(s.xctx, pkg, pkg.IsTest, fileSet) if err != nil { @@ -1016,16 +1114,42 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) { files = append(files, embed) } + parsed := &ParsedPackage{ + ImportPath: pkg.ImportPath, + Dir: pkg.Dir, + GoFiles: files, + FileSet: fileSet, + JSFiles: append(pkg.JSFiles, overlayJsFiles...), + } + s.parsedPackages[pkg.ImportPath] = parsed + + // Import dependencies from the augmented files, + // whilst skipping any that have been already imported. + for _, importedPkgPath := range parsed.Imports(pkg.Imports...) { + _, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir) + if err != nil { + return nil, err + } + } + + return parsed, nil +} + +func (s *Session) compilePackages(pkg *ParsedPackage) (*compiler.Archive, error) { + if archive, ok := s.UpToDateArchives[pkg.ImportPath]; ok { + return archive, nil + } + importContext := &compiler.ImportContext{ Packages: s.Types, - Import: s.ImportResolverFor(pkg), + Import: s.ImportResolverFor(pkg.Dir), } - archive, err := compiler.Compile(pkg.ImportPath, files, fileSet, importContext, s.options.Minify) + archive, err := compiler.Compile(pkg.ImportPath, pkg.GoFiles, pkg.FileSet, importContext, s.options.Minify) if err != nil { return nil, err } - for _, jsFile := range append(pkg.JSFiles, overlayJsFiles...) { + for _, jsFile := range pkg.JSFiles { archive.IncJSCode = append(archive.IncJSCode, []byte("\t(function() {\n")...) archive.IncJSCode = append(archive.IncJSCode, jsFile.Content...) archive.IncJSCode = append(archive.IncJSCode, []byte("\n\t}).call($global);\n")...) @@ -1035,21 +1159,50 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) { fmt.Println(pkg.ImportPath) } - s.buildCache.StoreArchive(archive, time.Now()) s.UpToDateArchives[pkg.ImportPath] = archive return archive, nil } +func (s *Session) getImportPath(path, srcDir string) (string, error) { + // If path is for an xtest package, just return it. + if strings.HasSuffix(path, "_test") { + return path, nil + } + + // Check if the import path is already cached. + if importPath, ok := s.importPaths[srcDir][path]; ok { + return importPath, nil + } + + // Fall back to the slop import of the build package. + pkg, err := s.xctx.Import(path, srcDir, 0) + if err != nil { + return ``, err + } + s.cacheImportPath(path, srcDir, pkg.ImportPath) + return pkg.ImportPath, nil +} + // ImportResolverFor returns a function which returns a compiled package archive // given an import path. -func (s *Session) ImportResolverFor(pkg *PackageData) func(string) (*compiler.Archive, error) { +func (s *Session) ImportResolverFor(srcDir string) func(string) (*compiler.Archive, error) { return func(path string) (*compiler.Archive, error) { - if archive, ok := s.UpToDateArchives[path]; ok { + importPath, err := s.getImportPath(path, srcDir) + if err != nil { + return nil, err + } + + if archive, ok := s.UpToDateArchives[importPath]; ok { return archive, nil } - _, archive, err := s.buildImportPathWithSrcDir(path, pkg.Dir) - return archive, err + + // The archive hasn't been compiled yet so compile it with the parsed package. + if parsed, ok := s.parsedPackages[importPath]; ok { + return s.compilePackages(parsed) + } + + return nil, fmt.Errorf(`parsed package for %q not found`, importPath) } } @@ -1087,13 +1240,7 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string) sourceMapFilter.MappingCallback = s.SourceMappingCallback(m) } - deps, err := compiler.ImportDependencies(archive, func(path string) (*compiler.Archive, error) { - if archive, ok := s.UpToDateArchives[path]; ok { - return archive, nil - } - _, archive, err := s.buildImportPathWithSrcDir(path, "") - return archive, err - }) + deps, err := compiler.ImportDependencies(archive, s.ImportResolverFor("")) if err != nil { return err } diff --git a/internal/testmain/testmain.go b/internal/testmain/testmain.go index f1b3257d5..3de87d382 100644 --- a/internal/testmain/testmain.go +++ b/internal/testmain/testmain.go @@ -5,7 +5,7 @@ import ( "errors" "fmt" "go/ast" - gobuild "go/build" + "go/build" "go/doc" "go/parser" "go/token" @@ -16,7 +16,6 @@ import ( "unicode" "unicode/utf8" - "github.com/gopherjs/gopherjs/build" "golang.org/x/tools/go/buildutil" ) @@ -66,7 +65,8 @@ func (ef ExampleFunc) Executable() bool { // TestMain is a helper type responsible for generation of the test main package. type TestMain struct { - Package *build.PackageData + Package *build.Package + Context *build.Context Tests []TestFunc Benchmarks []TestFunc Fuzz []TestFunc @@ -88,7 +88,7 @@ func (tm *TestMain) Scan(fset *token.FileSet) error { func (tm *TestMain) scanPkg(fset *token.FileSet, files []string, loc FuncLocation) error { for _, name := range files { srcPath := path.Join(tm.Package.Dir, name) - f, err := buildutil.OpenFile(tm.Package.InternalBuildContext(), srcPath) + f, err := buildutil.OpenFile(tm.Context, srcPath) if err != nil { return fmt.Errorf("failed to open source file %q: %w", srcPath, err) } @@ -158,7 +158,7 @@ func (tm *TestMain) scanFile(f *ast.File, loc FuncLocation) error { } // Synthesize main package for the tests. -func (tm *TestMain) Synthesize(fset *token.FileSet) (*build.PackageData, *ast.File, error) { +func (tm *TestMain) Synthesize(fset *token.FileSet) (*build.Package, *ast.File, error) { buf := &bytes.Buffer{} if err := testmainTmpl.Execute(buf, tm); err != nil { return nil, nil, fmt.Errorf("failed to generate testmain source for package %s: %w", tm.Package.ImportPath, err) @@ -167,12 +167,10 @@ func (tm *TestMain) Synthesize(fset *token.FileSet) (*build.PackageData, *ast.Fi if err != nil { return nil, nil, fmt.Errorf("failed to parse testmain source for package %s: %w", tm.Package.ImportPath, err) } - pkg := &build.PackageData{ - Package: &gobuild.Package{ - ImportPath: tm.Package.ImportPath + ".testmain", - Name: "main", - GoFiles: []string{"_testmain.go"}, - }, + pkg := &build.Package{ + ImportPath: tm.Package.ImportPath + ".testmain", + Name: "main", + GoFiles: []string{"_testmain.go"}, } return pkg, src, nil } diff --git a/internal/testmain/testmain_test.go b/internal/testmain/testmain_test.go index 01c92cc76..8e0b268d2 100644 --- a/internal/testmain/testmain_test.go +++ b/internal/testmain/testmain_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "github.com/gopherjs/gopherjs/build" "github.com/gopherjs/gopherjs/internal/srctesting" . "github.com/gopherjs/gopherjs/internal/testmain" @@ -21,7 +22,10 @@ func TestScan(t *testing.T) { fset := token.NewFileSet() - got := TestMain{Package: pkg} + got := TestMain{ + Package: pkg.Package, + Context: pkg.InternalBuildContext(), + } if err := got.Scan(fset); err != nil { t.Fatalf("Got: tm.Scan() returned error: %s. Want: no error.", err) } @@ -47,6 +51,7 @@ func TestScan(t *testing.T) { } opts := cmp.Options{ cmpopts.IgnoreFields(TestMain{}, "Package"), // Inputs. + cmpopts.IgnoreFields(TestMain{}, "Context"), } if diff := cmp.Diff(want, got, opts...); diff != "" { t.Errorf("List of test function is different from expected (-want,+got):\n%s", diff) @@ -54,9 +59,7 @@ func TestScan(t *testing.T) { } func TestSynthesize(t *testing.T) { - pkg := &build.PackageData{ - Package: &gobuild.Package{ImportPath: "foo/bar"}, - } + pkg := &gobuild.Package{ImportPath: "foo/bar"} tests := []struct { descr string diff --git a/tool.go b/tool.go index d72e187c6..9f791af1b 100644 --- a/tool.go +++ b/tool.go @@ -4,10 +4,8 @@ import ( "bytes" "errors" "fmt" - "go/ast" "go/build" "go/scanner" - "go/token" "go/types" "io" "net" @@ -29,7 +27,6 @@ import ( "github.com/gopherjs/gopherjs/build/cache" "github.com/gopherjs/gopherjs/compiler" "github.com/gopherjs/gopherjs/internal/sysutil" - "github.com/gopherjs/gopherjs/internal/testmain" "github.com/neelance/sourcemap" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -147,7 +144,7 @@ func main() { if err != nil { return err } - archive, err := s.BuildPackage(pkg) + archive, err := s.BuildProject(pkg) if err != nil { return err } @@ -214,8 +211,7 @@ func main() { if err != nil { return err } - - archive, err := s.BuildPackage(pkg) + archive, err := s.BuildProject(pkg) if err != nil { return err } @@ -371,27 +367,8 @@ func main() { return err } - _, err = s.BuildPackage(pkg.TestPackage()) - if err != nil { - return err - } - _, err = s.BuildPackage(pkg.XTestPackage()) - if err != nil { - return err - } - - fset := token.NewFileSet() - tests := testmain.TestMain{Package: pkg} - tests.Scan(fset) - mainPkg, mainFile, err := tests.Synthesize(fset) - if err != nil { - return fmt.Errorf("failed to generate testmain package for %s: %w", pkg.ImportPath, err) - } - importContext := &compiler.ImportContext{ - Packages: s.Types, - Import: s.ImportResolverFor(mainPkg), - } - mainPkgArchive, err := compiler.Compile(mainPkg.ImportPath, []*ast.File{mainFile}, fset, importContext, options.Minify) + pkg.IsTest = true + mainPkgArchive, err := s.BuildProject(pkg) if err != nil { return fmt.Errorf("failed to compile testmain package for %s: %w", pkg.ImportPath, err) } @@ -664,7 +641,7 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) { buf := new(bytes.Buffer) browserErrors := new(bytes.Buffer) err := func() error { - archive, err := s.BuildPackage(pkg) + archive, err := s.BuildProject(pkg) if err != nil { return err } @@ -673,7 +650,7 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) { m := &sourcemap.Map{File: base + ".js"} sourceMapFilter.MappingCallback = s.SourceMappingCallback(m) - deps, err := compiler.ImportDependencies(archive, s.BuildImportPath) + deps, err := compiler.ImportDependencies(archive, s.ImportResolverFor("")) if err != nil { return err } From 2a9b9a42d559dc48b0f554c635d7c3125290cfaa Mon Sep 17 00:00:00 2001 From: Grant Nelson Date: Fri, 14 Feb 2025 09:25:42 -0700 Subject: [PATCH 2/3] Fixing spelling mistake --- build/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/build.go b/build/build.go index 300e295bd..62b65d0b2 100644 --- a/build/build.go +++ b/build/build.go @@ -1175,7 +1175,7 @@ func (s *Session) getImportPath(path, srcDir string) (string, error) { return importPath, nil } - // Fall back to the slop import of the build package. + // Fall back to the slow import of the build package. pkg, err := s.xctx.Import(path, srcDir, 0) if err != nil { return ``, err From ba2ea8d05c63459aec7067740f9af3560b67f04b Mon Sep 17 00:00:00 2001 From: Grant Nelson Date: Mon, 24 Feb 2025 12:58:13 -0700 Subject: [PATCH 3/3] clarifying naming and comments --- build/build.go | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/build/build.go b/build/build.go index 62b65d0b2..833e7da92 100644 --- a/build/build.go +++ b/build/build.go @@ -788,9 +788,13 @@ type Session struct { options *Options xctx XContext - // importPaths is a map of the resolved import paths given the srcDir - // and path. This is used to avoid redetermining build packages during - // compilation when we're looking up parsed packages. + // importPaths is a map of the resolved import paths given the + // source directory (first key) and the unresolved import path (second key). + // This is used to cache the resolved import returned from XContext.Import. + // XContent.Import can be slow, so we cache the resolved path that is used + // as the map key by parsedPackages and UpToDateArchives. + // This makes subsequent lookups faster during compilation when all we have + // is the unresolved import path and source directory. importPaths map[string]map[string]string // parsePackage is a map of parsed packages that have been built and augmented. @@ -958,9 +962,9 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) { var parsed *ParsedPackage var err error if pkg.IsTest { - parsed, err = s.buildTestPackage(pkg) + parsed, err = s.loadTestPackage(pkg) } else { - parsed, err = s.buildPackages(pkg) + parsed, err = s.loadPackages(pkg) } if err != nil { return nil, err @@ -975,12 +979,12 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) { return s.compilePackages(parsed) } -func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) { - _, err := s.buildPackages(pkg.TestPackage()) +func (s *Session) loadTestPackage(pkg *PackageData) (*ParsedPackage, error) { + _, err := s.loadPackages(pkg.TestPackage()) if err != nil { return nil, err } - _, err = s.buildPackages(pkg.XTestPackage()) + _, err = s.loadPackages(pkg.XTestPackage()) if err != nil { return nil, err } @@ -1004,7 +1008,7 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) { // Import dependencies for the testmain package. for _, importedPkgPath := range parsed.Imports() { - _, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir) + _, _, err := s.loadImportPathWithSrcDir(importedPkgPath, pkg.Dir) if err != nil { return nil, err } @@ -1013,11 +1017,11 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) { return parsed, nil } -// buildImportPathWithSrcDir builds the parsed package specified by the import path. +// loadImportPathWithSrcDir gets the parsed package specified by the import path. // -// Relative import paths are interpreted relative to the passed srcDir. If -// srcDir is empty, current working directory is assumed. -func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, *ParsedPackage, error) { +// Relative import paths are interpreted relative to the passed srcDir. +// If srcDir is empty, current working directory is assumed. +func (s *Session) loadImportPathWithSrcDir(path, srcDir string) (*PackageData, *ParsedPackage, error) { pkg, err := s.xctx.Import(path, srcDir, 0) if s.Watcher != nil && pkg != nil { // add watch even on error s.Watcher.Add(pkg.Dir) @@ -1026,7 +1030,7 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, return nil, nil, err } - parsed, err := s.buildPackages(pkg) + parsed, err := s.loadPackages(pkg) if err != nil { return nil, nil, err } @@ -1035,8 +1039,8 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, return pkg, parsed, nil } -// cacheImportPath stores the import path for the build package so we can look -// it up later without getting the whole build package. +// cacheImportPath stores the resolved import path for the build package +// so we can look it up later without getting the whole build package. // The given path and source directly are the ones passed into // XContext.Import to the get the build package originally. func (s *Session) cacheImportPath(path, srcDir, importPath string) { @@ -1073,7 +1077,7 @@ var getExeModTime = func() func() time.Time { } }() -func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) { +func (s *Session) loadPackages(pkg *PackageData) (*ParsedPackage, error) { if parsed, ok := s.parsedPackages[pkg.ImportPath]; ok { return parsed, nil } @@ -1086,7 +1090,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) { if importedPkgPath == "unsafe" { continue } - importedPkg, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir) + importedPkg, _, err := s.loadImportPathWithSrcDir(importedPkgPath, pkg.Dir) if err != nil { return nil, err } @@ -1126,7 +1130,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) { // Import dependencies from the augmented files, // whilst skipping any that have been already imported. for _, importedPkgPath := range parsed.Imports(pkg.Imports...) { - _, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir) + _, _, err := s.loadImportPathWithSrcDir(importedPkgPath, pkg.Dir) if err != nil { return nil, err }