本站源代码
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.

223 lines
11KB

  1. // Copyright 2018 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. /*
  5. Package packages loads Go packages for inspection and analysis.
  6. The Load function takes as input a list of patterns and return a list of Package
  7. structs describing individual packages matched by those patterns.
  8. The LoadMode controls the amount of detail in the loaded packages.
  9. Load passes most patterns directly to the underlying build tool,
  10. but all patterns with the prefix "query=", where query is a
  11. non-empty string of letters from [a-z], are reserved and may be
  12. interpreted as query operators.
  13. Two query operators are currently supported: "file" and "pattern".
  14. The query "file=path/to/file.go" matches the package or packages enclosing
  15. the Go source file path/to/file.go. For example "file=~/go/src/fmt/print.go"
  16. might return the packages "fmt" and "fmt [fmt.test]".
  17. The query "pattern=string" causes "string" to be passed directly to
  18. the underlying build tool. In most cases this is unnecessary,
  19. but an application can use Load("pattern=" + x) as an escaping mechanism
  20. to ensure that x is not interpreted as a query operator if it contains '='.
  21. All other query operators are reserved for future use and currently
  22. cause Load to report an error.
  23. The Package struct provides basic information about the package, including
  24. - ID, a unique identifier for the package in the returned set;
  25. - GoFiles, the names of the package's Go source files;
  26. - Imports, a map from source import strings to the Packages they name;
  27. - Types, the type information for the package's exported symbols;
  28. - Syntax, the parsed syntax trees for the package's source code; and
  29. - TypeInfo, the result of a complete type-check of the package syntax trees.
  30. (See the documentation for type Package for the complete list of fields
  31. and more detailed descriptions.)
  32. For example,
  33. Load(nil, "bytes", "unicode...")
  34. returns four Package structs describing the standard library packages
  35. bytes, unicode, unicode/utf16, and unicode/utf8. Note that one pattern
  36. can match multiple packages and that a package might be matched by
  37. multiple patterns: in general it is not possible to determine which
  38. packages correspond to which patterns.
  39. Note that the list returned by Load contains only the packages matched
  40. by the patterns. Their dependencies can be found by walking the import
  41. graph using the Imports fields.
  42. The Load function can be configured by passing a pointer to a Config as
  43. the first argument. A nil Config is equivalent to the zero Config, which
  44. causes Load to run in LoadFiles mode, collecting minimal information.
  45. See the documentation for type Config for details.
  46. As noted earlier, the Config.Mode controls the amount of detail
  47. reported about the loaded packages, with each mode returning all the data of the
  48. previous mode with some extra added. See the documentation for type LoadMode
  49. for details.
  50. Most tools should pass their command-line arguments (after any flags)
  51. uninterpreted to the loader, so that the loader can interpret them
  52. according to the conventions of the underlying build system.
  53. See the Example function for typical usage.
  54. */
  55. package packages // import "golang.org/x/tools/go/packages"
  56. /*
  57. Motivation and design considerations
  58. The new package's design solves problems addressed by two existing
  59. packages: go/build, which locates and describes packages, and
  60. golang.org/x/tools/go/loader, which loads, parses and type-checks them.
  61. The go/build.Package structure encodes too much of the 'go build' way
  62. of organizing projects, leaving us in need of a data type that describes a
  63. package of Go source code independent of the underlying build system.
  64. We wanted something that works equally well with go build and vgo, and
  65. also other build systems such as Bazel and Blaze, making it possible to
  66. construct analysis tools that work in all these environments.
  67. Tools such as errcheck and staticcheck were essentially unavailable to
  68. the Go community at Google, and some of Google's internal tools for Go
  69. are unavailable externally.
  70. This new package provides a uniform way to obtain package metadata by
  71. querying each of these build systems, optionally supporting their
  72. preferred command-line notations for packages, so that tools integrate
  73. neatly with users' build environments. The Metadata query function
  74. executes an external query tool appropriate to the current workspace.
  75. Loading packages always returns the complete import graph "all the way down",
  76. even if all you want is information about a single package, because the query
  77. mechanisms of all the build systems we currently support ({go,vgo} list, and
  78. blaze/bazel aspect-based query) cannot provide detailed information
  79. about one package without visiting all its dependencies too, so there is
  80. no additional asymptotic cost to providing transitive information.
  81. (This property might not be true of a hypothetical 5th build system.)
  82. In calls to TypeCheck, all initial packages, and any package that
  83. transitively depends on one of them, must be loaded from source.
  84. Consider A->B->C->D->E: if A,C are initial, A,B,C must be loaded from
  85. source; D may be loaded from export data, and E may not be loaded at all
  86. (though it's possible that D's export data mentions it, so a
  87. types.Package may be created for it and exposed.)
  88. The old loader had a feature to suppress type-checking of function
  89. bodies on a per-package basis, primarily intended to reduce the work of
  90. obtaining type information for imported packages. Now that imports are
  91. satisfied by export data, the optimization no longer seems necessary.
  92. Despite some early attempts, the old loader did not exploit export data,
  93. instead always using the equivalent of WholeProgram mode. This was due
  94. to the complexity of mixing source and export data packages (now
  95. resolved by the upward traversal mentioned above), and because export data
  96. files were nearly always missing or stale. Now that 'go build' supports
  97. caching, all the underlying build systems can guarantee to produce
  98. export data in a reasonable (amortized) time.
  99. Test "main" packages synthesized by the build system are now reported as
  100. first-class packages, avoiding the need for clients (such as go/ssa) to
  101. reinvent this generation logic.
  102. One way in which go/packages is simpler than the old loader is in its
  103. treatment of in-package tests. In-package tests are packages that
  104. consist of all the files of the library under test, plus the test files.
  105. The old loader constructed in-package tests by a two-phase process of
  106. mutation called "augmentation": first it would construct and type check
  107. all the ordinary library packages and type-check the packages that
  108. depend on them; then it would add more (test) files to the package and
  109. type-check again. This two-phase approach had four major problems:
  110. 1) in processing the tests, the loader modified the library package,
  111. leaving no way for a client application to see both the test
  112. package and the library package; one would mutate into the other.
  113. 2) because test files can declare additional methods on types defined in
  114. the library portion of the package, the dispatch of method calls in
  115. the library portion was affected by the presence of the test files.
  116. This should have been a clue that the packages were logically
  117. different.
  118. 3) this model of "augmentation" assumed at most one in-package test
  119. per library package, which is true of projects using 'go build',
  120. but not other build systems.
  121. 4) because of the two-phase nature of test processing, all packages that
  122. import the library package had to be processed before augmentation,
  123. forcing a "one-shot" API and preventing the client from calling Load
  124. in several times in sequence as is now possible in WholeProgram mode.
  125. (TypeCheck mode has a similar one-shot restriction for a different reason.)
  126. Early drafts of this package supported "multi-shot" operation.
  127. Although it allowed clients to make a sequence of calls (or concurrent
  128. calls) to Load, building up the graph of Packages incrementally,
  129. it was of marginal value: it complicated the API
  130. (since it allowed some options to vary across calls but not others),
  131. it complicated the implementation,
  132. it cannot be made to work in Types mode, as explained above,
  133. and it was less efficient than making one combined call (when this is possible).
  134. Among the clients we have inspected, none made multiple calls to load
  135. but could not be easily and satisfactorily modified to make only a single call.
  136. However, applications changes may be required.
  137. For example, the ssadump command loads the user-specified packages
  138. and in addition the runtime package. It is tempting to simply append
  139. "runtime" to the user-provided list, but that does not work if the user
  140. specified an ad-hoc package such as [a.go b.go].
  141. Instead, ssadump no longer requests the runtime package,
  142. but seeks it among the dependencies of the user-specified packages,
  143. and emits an error if it is not found.
  144. Overlays: The Overlay field in the Config allows providing alternate contents
  145. for Go source files, by providing a mapping from file path to contents.
  146. go/packages will pull in new imports added in overlay files when go/packages
  147. is run in LoadImports mode or greater.
  148. Overlay support for the go list driver isn't complete yet: if the file doesn't
  149. exist on disk, it will only be recognized in an overlay if it is a non-test file
  150. and the package would be reported even without the overlay.
  151. Questions & Tasks
  152. - Add GOARCH/GOOS?
  153. They are not portable concepts, but could be made portable.
  154. Our goal has been to allow users to express themselves using the conventions
  155. of the underlying build system: if the build system honors GOARCH
  156. during a build and during a metadata query, then so should
  157. applications built atop that query mechanism.
  158. Conversely, if the target architecture of the build is determined by
  159. command-line flags, the application can pass the relevant
  160. flags through to the build system using a command such as:
  161. myapp -query_flag="--cpu=amd64" -query_flag="--os=darwin"
  162. However, this approach is low-level, unwieldy, and non-portable.
  163. GOOS and GOARCH seem important enough to warrant a dedicated option.
  164. - How should we handle partial failures such as a mixture of good and
  165. malformed patterns, existing and non-existent packages, successful and
  166. failed builds, import failures, import cycles, and so on, in a call to
  167. Load?
  168. - Support bazel, blaze, and go1.10 list, not just go1.11 list.
  169. - Handle (and test) various partial success cases, e.g.
  170. a mixture of good packages and:
  171. invalid patterns
  172. nonexistent packages
  173. empty packages
  174. packages with malformed package or import declarations
  175. unreadable files
  176. import cycles
  177. other parse errors
  178. type errors
  179. Make sure we record errors at the correct place in the graph.
  180. - Missing packages among initial arguments are not reported.
  181. Return bogus packages for them, like golist does.
  182. - "undeclared name" errors (for example) are reported out of source file
  183. order. I suspect this is due to the breadth-first resolution now used
  184. by go/types. Is that a bug? Discuss with gri.
  185. */
上海开阖软件有限公司 沪ICP备12045867号-1