You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
9.4 KiB

  1. // Copyright 2015 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 loader loads a complete Go program from source code, parsing
  5. // and type-checking the initial packages plus their transitive closure
  6. // of dependencies. The ASTs and the derived facts are retained for
  7. // later use.
  8. //
  9. // THIS INTERFACE IS EXPERIMENTAL AND IS LIKELY TO CHANGE.
  10. //
  11. // The package defines two primary types: Config, which specifies a
  12. // set of initial packages to load and various other options; and
  13. // Program, which is the result of successfully loading the packages
  14. // specified by a configuration.
  15. //
  16. // The configuration can be set directly, but *Config provides various
  17. // convenience methods to simplify the common cases, each of which can
  18. // be called any number of times. Finally, these are followed by a
  19. // call to Load() to actually load and type-check the program.
  20. //
  21. // var conf loader.Config
  22. //
  23. // // Use the command-line arguments to specify
  24. // // a set of initial packages to load from source.
  25. // // See FromArgsUsage for help.
  26. // rest, err := conf.FromArgs(os.Args[1:], wantTests)
  27. //
  28. // // Parse the specified files and create an ad hoc package with path "foo".
  29. // // All files must have the same 'package' declaration.
  30. // conf.CreateFromFilenames("foo", "foo.go", "bar.go")
  31. //
  32. // // Create an ad hoc package with path "foo" from
  33. // // the specified already-parsed files.
  34. // // All ASTs must have the same 'package' declaration.
  35. // conf.CreateFromFiles("foo", parsedFiles)
  36. //
  37. // // Add "runtime" to the set of packages to be loaded.
  38. // conf.Import("runtime")
  39. //
  40. // // Adds "fmt" and "fmt_test" to the set of packages
  41. // // to be loaded. "fmt" will include *_test.go files.
  42. // conf.ImportWithTests("fmt")
  43. //
  44. // // Finally, load all the packages specified by the configuration.
  45. // prog, err := conf.Load()
  46. //
  47. // See examples_test.go for examples of API usage.
  48. //
  49. //
  50. // CONCEPTS AND TERMINOLOGY
  51. //
  52. // The WORKSPACE is the set of packages accessible to the loader. The
  53. // workspace is defined by Config.Build, a *build.Context. The
  54. // default context treats subdirectories of $GOROOT and $GOPATH as
  55. // packages, but this behavior may be overridden.
  56. //
  57. // An AD HOC package is one specified as a set of source files on the
  58. // command line. In the simplest case, it may consist of a single file
  59. // such as $GOROOT/src/net/http/triv.go.
  60. //
  61. // EXTERNAL TEST packages are those comprised of a set of *_test.go
  62. // files all with the same 'package foo_test' declaration, all in the
  63. // same directory. (go/build.Package calls these files XTestFiles.)
  64. //
  65. // An IMPORTABLE package is one that can be referred to by some import
  66. // spec. Every importable package is uniquely identified by its
  67. // PACKAGE PATH or just PATH, a string such as "fmt", "encoding/json",
  68. // or "cmd/vendor/golang.org/x/arch/x86/x86asm". A package path
  69. // typically denotes a subdirectory of the workspace.
  70. //
  71. // An import declaration uses an IMPORT PATH to refer to a package.
  72. // Most import declarations use the package path as the import path.
  73. //
  74. // Due to VENDORING (https://golang.org/s/go15vendor), the
  75. // interpretation of an import path may depend on the directory in which
  76. // it appears. To resolve an import path to a package path, go/build
  77. // must search the enclosing directories for a subdirectory named
  78. // "vendor".
  79. //
  80. // ad hoc packages and external test packages are NON-IMPORTABLE. The
  81. // path of an ad hoc package is inferred from the package
  82. // declarations of its files and is therefore not a unique package key.
  83. // For example, Config.CreatePkgs may specify two initial ad hoc
  84. // packages, both with path "main".
  85. //
  86. // An AUGMENTED package is an importable package P plus all the
  87. // *_test.go files with same 'package foo' declaration as P.
  88. // (go/build.Package calls these files TestFiles.)
  89. //
  90. // The INITIAL packages are those specified in the configuration. A
  91. // DEPENDENCY is a package loaded to satisfy an import in an initial
  92. // package or another dependency.
  93. //
  94. package loader
  95. // IMPLEMENTATION NOTES
  96. //
  97. // 'go test', in-package test files, and import cycles
  98. // ---------------------------------------------------
  99. //
  100. // An external test package may depend upon members of the augmented
  101. // package that are not in the unaugmented package, such as functions
  102. // that expose internals. (See bufio/export_test.go for an example.)
  103. // So, the loader must ensure that for each external test package
  104. // it loads, it also augments the corresponding non-test package.
  105. //
  106. // The import graph over n unaugmented packages must be acyclic; the
  107. // import graph over n-1 unaugmented packages plus one augmented
  108. // package must also be acyclic. ('go test' relies on this.) But the
  109. // import graph over n augmented packages may contain cycles.
  110. //
  111. // First, all the (unaugmented) non-test packages and their
  112. // dependencies are imported in the usual way; the loader reports an
  113. // error if it detects an import cycle.
  114. //
  115. // Then, each package P for which testing is desired is augmented by
  116. // the list P' of its in-package test files, by calling
  117. // (*types.Checker).Files. This arrangement ensures that P' may
  118. // reference definitions within P, but P may not reference definitions
  119. // within P'. Furthermore, P' may import any other package, including
  120. // ones that depend upon P, without an import cycle error.
  121. //
  122. // Consider two packages A and B, both of which have lists of
  123. // in-package test files we'll call A' and B', and which have the
  124. // following import graph edges:
  125. // B imports A
  126. // B' imports A
  127. // A' imports B
  128. // This last edge would be expected to create an error were it not
  129. // for the special type-checking discipline above.
  130. // Cycles of size greater than two are possible. For example:
  131. // compress/bzip2/bzip2_test.go (package bzip2) imports "io/ioutil"
  132. // io/ioutil/tempfile_test.go (package ioutil) imports "regexp"
  133. // regexp/exec_test.go (package regexp) imports "compress/bzip2"
  134. //
  135. //
  136. // Concurrency
  137. // -----------
  138. //
  139. // Let us define the import dependency graph as follows. Each node is a
  140. // list of files passed to (Checker).Files at once. Many of these lists
  141. // are the production code of an importable Go package, so those nodes
  142. // are labelled by the package's path. The remaining nodes are
  143. // ad hoc packages and lists of in-package *_test.go files that augment
  144. // an importable package; those nodes have no label.
  145. //
  146. // The edges of the graph represent import statements appearing within a
  147. // file. An edge connects a node (a list of files) to the node it
  148. // imports, which is importable and thus always labelled.
  149. //
  150. // Loading is controlled by this dependency graph.
  151. //
  152. // To reduce I/O latency, we start loading a package's dependencies
  153. // asynchronously as soon as we've parsed its files and enumerated its
  154. // imports (scanImports). This performs a preorder traversal of the
  155. // import dependency graph.
  156. //
  157. // To exploit hardware parallelism, we type-check unrelated packages in
  158. // parallel, where "unrelated" means not ordered by the partial order of
  159. // the import dependency graph.
  160. //
  161. // We use a concurrency-safe non-blocking cache (importer.imported) to
  162. // record the results of type-checking, whether success or failure. An
  163. // entry is created in this cache by startLoad the first time the
  164. // package is imported. The first goroutine to request an entry becomes
  165. // responsible for completing the task and broadcasting completion to
  166. // subsequent requestors, which block until then.
  167. //
  168. // Type checking occurs in (parallel) postorder: we cannot type-check a
  169. // set of files until we have loaded and type-checked all of their
  170. // immediate dependencies (and thus all of their transitive
  171. // dependencies). If the input were guaranteed free of import cycles,
  172. // this would be trivial: we could simply wait for completion of the
  173. // dependencies and then invoke the typechecker.
  174. //
  175. // But as we saw in the 'go test' section above, some cycles in the
  176. // import graph over packages are actually legal, so long as the
  177. // cycle-forming edge originates in the in-package test files that
  178. // augment the package. This explains why the nodes of the import
  179. // dependency graph are not packages, but lists of files: the unlabelled
  180. // nodes avoid the cycles. Consider packages A and B where B imports A
  181. // and A's in-package tests AT import B. The naively constructed import
  182. // graph over packages would contain a cycle (A+AT) --> B --> (A+AT) but
  183. // the graph over lists of files is AT --> B --> A, where AT is an
  184. // unlabelled node.
  185. //
  186. // Awaiting completion of the dependencies in a cyclic graph would
  187. // deadlock, so we must materialize the import dependency graph (as
  188. // importer.graph) and check whether each import edge forms a cycle. If
  189. // x imports y, and the graph already contains a path from y to x, then
  190. // there is an import cycle, in which case the processing of x must not
  191. // wait for the completion of processing of y.
  192. //
  193. // When the type-checker makes a callback (doImport) to the loader for a
  194. // given import edge, there are two possible cases. In the normal case,
  195. // the dependency has already been completely type-checked; doImport
  196. // does a cache lookup and returns it. In the cyclic case, the entry in
  197. // the cache is still necessarily incomplete, indicating a cycle. We
  198. // perform the cycle check again to obtain the error message, and return
  199. // the error.
  200. //
  201. // The result of using concurrency is about a 2.5x speedup for stdlib_test.
  202. // TODO(adonovan): overhaul the package documentation.