github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/list/list.go (about) 1 // Copyright 2011 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 list implements the “go list” command. 6 package list 7 8 import ( 9 "github.com/shogo82148/std/bufio" 10 11 "github.com/shogo82148/std/cmd/go/internal/base" 12 ) 13 14 var CmdList = &base.Command{ 15 16 UsageLine: "go list [-f format] [-json] [-m] [list flags] [build flags] [packages]", 17 Short: "list packages or modules", 18 Long: ` 19 List lists the named packages, one per line. 20 The most commonly-used flags are -f and -json, which control the form 21 of the output printed for each package. Other list flags, documented below, 22 control more specific details. 23 24 The default output shows the package import path: 25 26 bytes 27 encoding/json 28 github.com/gorilla/mux 29 golang.org/x/net/html 30 31 The -f flag specifies an alternate format for the list, using the 32 syntax of package template. The default output is equivalent 33 to -f '{{.ImportPath}}'. The struct being passed to the template is: 34 35 type Package struct { 36 Dir string // directory containing package sources 37 ImportPath string // import path of package in dir 38 ImportComment string // path in import comment on package statement 39 Name string // package name 40 Doc string // package documentation string 41 Target string // install path 42 Shlib string // the shared library that contains this package (only set when -linkshared) 43 Goroot bool // is this package in the Go root? 44 Standard bool // is this package part of the standard Go library? 45 Stale bool // would 'go install' do anything for this package? 46 StaleReason string // explanation for Stale==true 47 Root string // Go root or Go path dir containing this package 48 ConflictDir string // this directory shadows Dir in $GOPATH 49 BinaryOnly bool // binary-only package (no longer supported) 50 ForTest string // package is only for use in named test 51 Export string // file containing export data (when using -export) 52 BuildID string // build ID of the compiled package (when using -export) 53 Module *Module // info about package's containing module, if any (can be nil) 54 Match []string // command-line patterns matching this package 55 DepOnly bool // package is only a dependency, not explicitly listed 56 DefaultGODEBUG string // default GODEBUG setting, for main packages 57 58 // Source files 59 GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) 60 CgoFiles []string // .go source files that import "C" 61 CompiledGoFiles []string // .go files presented to compiler (when using -compiled) 62 IgnoredGoFiles []string // .go source files ignored due to build constraints 63 IgnoredOtherFiles []string // non-.go source files ignored due to build constraints 64 CFiles []string // .c source files 65 CXXFiles []string // .cc, .cxx and .cpp source files 66 MFiles []string // .m source files 67 HFiles []string // .h, .hh, .hpp and .hxx source files 68 FFiles []string // .f, .F, .for and .f90 Fortran source files 69 SFiles []string // .s source files 70 SwigFiles []string // .swig files 71 SwigCXXFiles []string // .swigcxx files 72 SysoFiles []string // .syso object files to add to archive 73 TestGoFiles []string // _test.go files in package 74 XTestGoFiles []string // _test.go files outside package 75 76 // Embedded files 77 EmbedPatterns []string // //go:embed patterns 78 EmbedFiles []string // files matched by EmbedPatterns 79 TestEmbedPatterns []string // //go:embed patterns in TestGoFiles 80 TestEmbedFiles []string // files matched by TestEmbedPatterns 81 XTestEmbedPatterns []string // //go:embed patterns in XTestGoFiles 82 XTestEmbedFiles []string // files matched by XTestEmbedPatterns 83 84 // Cgo directives 85 CgoCFLAGS []string // cgo: flags for C compiler 86 CgoCPPFLAGS []string // cgo: flags for C preprocessor 87 CgoCXXFLAGS []string // cgo: flags for C++ compiler 88 CgoFFLAGS []string // cgo: flags for Fortran compiler 89 CgoLDFLAGS []string // cgo: flags for linker 90 CgoPkgConfig []string // cgo: pkg-config names 91 92 // Dependency information 93 Imports []string // import paths used by this package 94 ImportMap map[string]string // map from source import to ImportPath (identity entries omitted) 95 Deps []string // all (recursively) imported dependencies 96 TestImports []string // imports from TestGoFiles 97 XTestImports []string // imports from XTestGoFiles 98 99 // Error information 100 Incomplete bool // this package or a dependency has an error 101 Error *PackageError // error loading package 102 DepsErrors []*PackageError // errors loading dependencies 103 } 104 105 Packages stored in vendor directories report an ImportPath that includes the 106 path to the vendor directory (for example, "d/vendor/p" instead of "p"), 107 so that the ImportPath uniquely identifies a given copy of a package. 108 The Imports, Deps, TestImports, and XTestImports lists also contain these 109 expanded import paths. See golang.org/s/go15vendor for more about vendoring. 110 111 The error information, if any, is 112 113 type PackageError struct { 114 ImportStack []string // shortest path from package named on command line to this one 115 Pos string // position of error (if present, file:line:col) 116 Err string // the error itself 117 } 118 119 The module information is a Module struct, defined in the discussion 120 of list -m below. 121 122 The template function "join" calls strings.Join. 123 124 The template function "context" returns the build context, defined as: 125 126 type Context struct { 127 GOARCH string // target architecture 128 GOOS string // target operating system 129 GOROOT string // Go root 130 GOPATH string // Go path 131 CgoEnabled bool // whether cgo can be used 132 UseAllFiles bool // use files regardless of //go:build lines, file names 133 Compiler string // compiler to assume when computing target paths 134 BuildTags []string // build constraints to match in //go:build lines 135 ToolTags []string // toolchain-specific build constraints 136 ReleaseTags []string // releases the current release is compatible with 137 InstallSuffix string // suffix to use in the name of the install dir 138 } 139 140 For more information about the meaning of these fields see the documentation 141 for the go/build package's Context type. 142 143 The -json flag causes the package data to be printed in JSON format 144 instead of using the template format. The JSON flag can optionally be 145 provided with a set of comma-separated required field names to be output. 146 If so, those required fields will always appear in JSON output, but 147 others may be omitted to save work in computing the JSON struct. 148 149 The -compiled flag causes list to set CompiledGoFiles to the Go source 150 files presented to the compiler. Typically this means that it repeats 151 the files listed in GoFiles and then also adds the Go code generated 152 by processing CgoFiles and SwigFiles. The Imports list contains the 153 union of all imports from both GoFiles and CompiledGoFiles. 154 155 The -deps flag causes list to iterate over not just the named packages 156 but also all their dependencies. It visits them in a depth-first post-order 157 traversal, so that a package is listed only after all its dependencies. 158 Packages not explicitly listed on the command line will have the DepOnly 159 field set to true. 160 161 The -e flag changes the handling of erroneous packages, those that 162 cannot be found or are malformed. By default, the list command 163 prints an error to standard error for each erroneous package and 164 omits the packages from consideration during the usual printing. 165 With the -e flag, the list command never prints errors to standard 166 error and instead processes the erroneous packages with the usual 167 printing. Erroneous packages will have a non-empty ImportPath and 168 a non-nil Error field; other information may or may not be missing 169 (zeroed). 170 171 The -export flag causes list to set the Export field to the name of a 172 file containing up-to-date export information for the given package, 173 and the BuildID field to the build ID of the compiled package. 174 175 The -find flag causes list to identify the named packages but not 176 resolve their dependencies: the Imports and Deps lists will be empty. 177 With the -find flag, the -deps, -test and -export commands cannot be 178 used. 179 180 The -test flag causes list to report not only the named packages 181 but also their test binaries (for packages with tests), to convey to 182 source code analysis tools exactly how test binaries are constructed. 183 The reported import path for a test binary is the import path of 184 the package followed by a ".test" suffix, as in "math/rand.test". 185 When building a test, it is sometimes necessary to rebuild certain 186 dependencies specially for that test (most commonly the tested 187 package itself). The reported import path of a package recompiled 188 for a particular test binary is followed by a space and the name of 189 the test binary in brackets, as in "math/rand [math/rand.test]" 190 or "regexp [sort.test]". The ForTest field is also set to the name 191 of the package being tested ("math/rand" or "sort" in the previous 192 examples). 193 194 The Dir, Target, Shlib, Root, ConflictDir, and Export file paths 195 are all absolute paths. 196 197 By default, the lists GoFiles, CgoFiles, and so on hold names of files in Dir 198 (that is, paths relative to Dir, not absolute paths). 199 The generated files added when using the -compiled and -test flags 200 are absolute paths referring to cached copies of generated Go source files. 201 Although they are Go source files, the paths may not end in ".go". 202 203 The -m flag causes list to list modules instead of packages. 204 205 When listing modules, the -f flag still specifies a format template 206 applied to a Go struct, but now a Module struct: 207 208 type Module struct { 209 Path string // module path 210 Query string // version query corresponding to this version 211 Version string // module version 212 Versions []string // available module versions 213 Replace *Module // replaced by this module 214 Time *time.Time // time version was created 215 Update *Module // available update (with -u) 216 Main bool // is this the main module? 217 Indirect bool // module is only indirectly needed by main module 218 Dir string // directory holding local copy of files, if any 219 GoMod string // path to go.mod file describing module, if any 220 GoVersion string // go version used in module 221 Retracted []string // retraction information, if any (with -retracted or -u) 222 Deprecated string // deprecation message, if any (with -u) 223 Error *ModuleError // error loading module 224 Sum string // checksum for path, version (as in go.sum) 225 GoModSum string // checksum for go.mod (as in go.sum) 226 Origin any // provenance of module 227 Reuse bool // reuse of old module info is safe 228 } 229 230 type ModuleError struct { 231 Err string // the error itself 232 } 233 234 The file GoMod refers to may be outside the module directory if the 235 module is in the module cache or if the -modfile flag is used. 236 237 The default output is to print the module path and then 238 information about the version and replacement if any. 239 For example, 'go list -m all' might print: 240 241 my/main/module 242 golang.org/x/text v0.3.0 => /tmp/text 243 rsc.io/pdf v0.1.1 244 245 The Module struct has a String method that formats this 246 line of output, so that the default format is equivalent 247 to -f '{{.String}}'. 248 249 Note that when a module has been replaced, its Replace field 250 describes the replacement module, and its Dir field is set to 251 the replacement's source code, if present. (That is, if Replace 252 is non-nil, then Dir is set to Replace.Dir, with no access to 253 the replaced source code.) 254 255 The -u flag adds information about available upgrades. 256 When the latest version of a given module is newer than 257 the current one, list -u sets the Module's Update field 258 to information about the newer module. list -u will also set 259 the module's Retracted field if the current version is retracted. 260 The Module's String method indicates an available upgrade by 261 formatting the newer version in brackets after the current version. 262 If a version is retracted, the string "(retracted)" will follow it. 263 For example, 'go list -m -u all' might print: 264 265 my/main/module 266 golang.org/x/text v0.3.0 [v0.4.0] => /tmp/text 267 rsc.io/pdf v0.1.1 (retracted) [v0.1.2] 268 269 (For tools, 'go list -m -u -json all' may be more convenient to parse.) 270 271 The -versions flag causes list to set the Module's Versions field 272 to a list of all known versions of that module, ordered according 273 to semantic versioning, earliest to latest. The flag also changes 274 the default output format to display the module path followed by the 275 space-separated version list. 276 277 The -retracted flag causes list to report information about retracted 278 module versions. When -retracted is used with -f or -json, the Retracted 279 field will be set to a string explaining why the version was retracted. 280 The string is taken from comments on the retract directive in the 281 module's go.mod file. When -retracted is used with -versions, retracted 282 versions are listed together with unretracted versions. The -retracted 283 flag may be used with or without -m. 284 285 The arguments to list -m are interpreted as a list of modules, not packages. 286 The main module is the module containing the current directory. 287 The active modules are the main module and its dependencies. 288 With no arguments, list -m shows the main module. 289 With arguments, list -m shows the modules specified by the arguments. 290 Any of the active modules can be specified by its module path. 291 The special pattern "all" specifies all the active modules, first the main 292 module and then dependencies sorted by module path. 293 A pattern containing "..." specifies the active modules whose 294 module paths match the pattern. 295 A query of the form path@version specifies the result of that query, 296 which is not limited to active modules. 297 See 'go help modules' for more about module queries. 298 299 The template function "module" takes a single string argument 300 that must be a module path or query and returns the specified 301 module as a Module struct. If an error occurs, the result will 302 be a Module struct with a non-nil Error field. 303 304 When using -m, the -reuse=old.json flag accepts the name of file containing 305 the JSON output of a previous 'go list -m -json' invocation with the 306 same set of modifier flags (such as -u, -retracted, and -versions). 307 The go command may use this file to determine that a module is unchanged 308 since the previous invocation and avoid redownloading information about it. 309 Modules that are not redownloaded will be marked in the new output by 310 setting the Reuse field to true. Normally the module cache provides this 311 kind of reuse automatically; the -reuse flag can be useful on systems that 312 do not preserve the module cache. 313 314 For more about build flags, see 'go help build'. 315 316 For more about specifying packages, see 'go help packages'. 317 318 For more about modules, see https://golang.org/ref/mod. 319 `, 320 } 321 322 // TrackingWriter tracks the last byte written on every write so 323 // we can avoid printing a newline if one was already written or 324 // if there is no output at all. 325 type TrackingWriter struct { 326 w *bufio.Writer 327 last byte 328 } 329 330 func (t *TrackingWriter) Write(p []byte) (n int, err error) 331 332 func (t *TrackingWriter) Flush() 333 334 func (t *TrackingWriter) NeedNL() bool