srcimporter.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package srcimporter implements importing directly
  5. // from source files rather than installed packages.
  6. package srcimporter
  7. import (
  8. "fmt"
  9. "go/ast"
  10. "go/build"
  11. "go/parser"
  12. "go/token"
  13. "go/types"
  14. "io"
  15. "os"
  16. "path/filepath"
  17. "sync"
  18. )
  19. // An Importer provides the context for importing packages from source code.
  20. type Importer struct {
  21. ctxt *build.Context
  22. fset *token.FileSet
  23. sizes types.Sizes
  24. packages map[string]*types.Package
  25. }
  26. // NewImporter returns a new Importer for the given context, file set, and map
  27. // of packages. The context is used to resolve import paths to package paths,
  28. // and identifying the files belonging to the package. If the context provides
  29. // non-nil file system functions, they are used instead of the regular package
  30. // os functions. The file set is used to track position information of package
  31. // files; and imported packages are added to the packages map.
  32. func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Package) *Importer {
  33. return &Importer{
  34. ctxt: ctxt,
  35. fset: fset,
  36. sizes: types.SizesFor(ctxt.Compiler, ctxt.GOARCH), // uses go/types default if GOARCH not found
  37. packages: packages,
  38. }
  39. }
  40. // Importing is a sentinel taking the place in Importer.packages
  41. // for a package that is in the process of being imported.
  42. var importing types.Package
  43. // Import(path) is a shortcut for ImportFrom(path, ".", 0).
  44. func (p *Importer) Import(path string) (*types.Package, error) {
  45. return p.ImportFrom(path, ".", 0) // use "." rather than "" (see issue #24441)
  46. }
  47. // ImportFrom imports the package with the given import path resolved from the given srcDir,
  48. // adds the new package to the set of packages maintained by the importer, and returns the
  49. // package. Package path resolution and file system operations are controlled by the context
  50. // maintained with the importer. The import mode must be zero but is otherwise ignored.
  51. // Packages that are not comprised entirely of pure Go files may fail to import because the
  52. // type checker may not be able to determine all exported entities (e.g. due to cgo dependencies).
  53. func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
  54. if mode != 0 {
  55. panic("non-zero import mode")
  56. }
  57. if abs, err := p.absPath(srcDir); err == nil { // see issue #14282
  58. srcDir = abs
  59. }
  60. bp, err := p.ctxt.Import(path, srcDir, 0)
  61. if err != nil {
  62. return nil, err // err may be *build.NoGoError - return as is
  63. }
  64. // package unsafe is known to the type checker
  65. if bp.ImportPath == "unsafe" {
  66. return types.Unsafe, nil
  67. }
  68. // no need to re-import if the package was imported completely before
  69. pkg := p.packages[bp.ImportPath]
  70. if pkg != nil {
  71. if pkg == &importing {
  72. return nil, fmt.Errorf("import cycle through package %q", bp.ImportPath)
  73. }
  74. if !pkg.Complete() {
  75. // Package exists but is not complete - we cannot handle this
  76. // at the moment since the source importer replaces the package
  77. // wholesale rather than augmenting it (see #19337 for details).
  78. // Return incomplete package with error (see #16088).
  79. return pkg, fmt.Errorf("reimported partially imported package %q", bp.ImportPath)
  80. }
  81. return pkg, nil
  82. }
  83. p.packages[bp.ImportPath] = &importing
  84. defer func() {
  85. // clean up in case of error
  86. // TODO(gri) Eventually we may want to leave a (possibly empty)
  87. // package in the map in all cases (and use that package to
  88. // identify cycles). See also issue 16088.
  89. if p.packages[bp.ImportPath] == &importing {
  90. p.packages[bp.ImportPath] = nil
  91. }
  92. }()
  93. var filenames []string
  94. filenames = append(filenames, bp.GoFiles...)
  95. filenames = append(filenames, bp.CgoFiles...)
  96. files, err := p.parseFiles(bp.Dir, filenames)
  97. if err != nil {
  98. return nil, err
  99. }
  100. // type-check package files
  101. var firstHardErr error
  102. conf := types.Config{
  103. IgnoreFuncBodies: true,
  104. FakeImportC: true,
  105. // continue type-checking after the first error
  106. Error: func(err error) {
  107. if firstHardErr == nil && !err.(types.Error).Soft {
  108. firstHardErr = err
  109. }
  110. },
  111. Importer: p,
  112. Sizes: p.sizes,
  113. }
  114. pkg, err = conf.Check(bp.ImportPath, p.fset, files, nil)
  115. if err != nil {
  116. // If there was a hard error it is possibly unsafe
  117. // to use the package as it may not be fully populated.
  118. // Do not return it (see also #20837, #20855).
  119. if firstHardErr != nil {
  120. pkg = nil
  121. err = firstHardErr // give preference to first hard error over any soft error
  122. }
  123. return pkg, fmt.Errorf("type-checking package %q failed (%v)", bp.ImportPath, err)
  124. }
  125. if firstHardErr != nil {
  126. // this can only happen if we have a bug in go/types
  127. panic("package is not safe yet no error was returned")
  128. }
  129. p.packages[bp.ImportPath] = pkg
  130. return pkg, nil
  131. }
  132. func (p *Importer) parseFiles(dir string, filenames []string) ([]*ast.File, error) {
  133. // use build.Context's OpenFile if there is one
  134. open := p.ctxt.OpenFile
  135. if open == nil {
  136. open = func(name string) (io.ReadCloser, error) { return os.Open(name) }
  137. }
  138. files := make([]*ast.File, len(filenames))
  139. errors := make([]error, len(filenames))
  140. var wg sync.WaitGroup
  141. wg.Add(len(filenames))
  142. for i, filename := range filenames {
  143. go func(i int, filepath string) {
  144. defer wg.Done()
  145. src, err := open(filepath)
  146. if err != nil {
  147. errors[i] = err // open provides operation and filename in error
  148. return
  149. }
  150. files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, 0)
  151. src.Close() // ignore Close error - parsing may have succeeded which is all we need
  152. }(i, p.joinPath(dir, filename))
  153. }
  154. wg.Wait()
  155. // if there are errors, return the first one for deterministic results
  156. for _, err := range errors {
  157. if err != nil {
  158. return nil, err
  159. }
  160. }
  161. return files, nil
  162. }
  163. // context-controlled file system operations
  164. func (p *Importer) absPath(path string) (string, error) {
  165. // TODO(gri) This should be using p.ctxt.AbsPath which doesn't
  166. // exist but probably should. See also issue #14282.
  167. return filepath.Abs(path)
  168. }
  169. func (p *Importer) isAbsPath(path string) bool {
  170. if f := p.ctxt.IsAbsPath; f != nil {
  171. return f(path)
  172. }
  173. return filepath.IsAbs(path)
  174. }
  175. func (p *Importer) joinPath(elem ...string) string {
  176. if f := p.ctxt.JoinPath; f != nil {
  177. return f(elem...)
  178. }
  179. return filepath.Join(elem...)
  180. }