github.com/sirkon/goproxy@v1.4.8/internal/modload/help.go (about) 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 modload 6 7 import "github.com/sirkon/goproxy/internal/base" 8 9 // TODO(rsc): The "module code layout" section needs to be written. 10 11 var HelpModules = &base.Command{ 12 UsageLine: "modules", 13 Short: "modules, module versions, and more", 14 Long: ` 15 A module is a collection of related Go packages. 16 Modules are the unit of source code interchange and versioning. 17 The go command has direct support for working with modules, 18 including recording and resolving dependencies on other modules. 19 Modules replace the old GOPATH-based approach to specifying 20 which source files are used in a given build. 21 22 Preliminary module support 23 24 Go 1.11 includes preliminary support for Go modules, 25 including a new module-aware 'go get' command. 26 We intend to keep revising this support, while preserving compatibility, 27 until it can be declared official (no longer preliminary), 28 and then at a later point we may remove support for work 29 in GOPATH and the old 'go get' command. 30 31 The quickest way to take advantage of the new Go 1.11 module support 32 is to check out your repository into a directory outside GOPATH/src, 33 create a go.mod file (described in the next section) there, and run 34 go commands from within that file tree. 35 36 For more fine-grained control, the module support in Go 1.11 respects 37 a temporary environment variable, GO111MODULE, which can be set to one 38 of three string values: off, on, or auto (the default). 39 If GO111MODULE=off, then the go command never uses the 40 new module support. Instead it looks in vendor directories and GOPATH 41 to find dependencies; we now refer to this as "GOPATH mode." 42 If GO111MODULE=on, then the go command requires the use of modules, 43 never consulting GOPATH. We refer to this as the command being 44 module-aware or running in "module-aware mode". 45 If GO111MODULE=auto or is unset, then the go command enables or 46 disables module support based on the current directory. 47 Module support is enabled only when the current directory is outside 48 GOPATH/src and itself contains a go.mod file or is below a directory 49 containing a go.mod file. 50 51 In module-aware mode, GOPATH no longer defines the meaning of imports 52 during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod) 53 and installed commands (in GOPATH/bin, unless GOBIN is set). 54 55 Defining a module 56 57 A module is defined by a tree of Go source files with a go.mod file 58 in the tree's root directory. The directory containing the go.mod file 59 is called the module root. Typically the module root will also correspond 60 to a source code repository root (but in general it need not). 61 The module is the set of all Go packages in the module root and its 62 subdirectories, but excluding subtrees with their own go.mod files. 63 64 The "module path" is the import path prefix corresponding to the module root. 65 The go.mod file defines the module path and lists the specific versions 66 of other modules that should be used when resolving imports during a build, 67 by giving their module paths and versions. 68 69 For example, this go.mod declares that the directory containing it is the root 70 of the module with path example.com/m, and it also declares that the module 71 depends on specific versions of golang.org/x/text and gopkg.in/yaml.v2: 72 73 module example.com/m 74 75 require ( 76 golang.org/x/text v0.3.0 77 gopkg.in/yaml.v2 v2.1.0 78 ) 79 80 The go.mod file can also specify replacements and excluded versions 81 that only apply when building the module directly; they are ignored 82 when the module is incorporated into a larger build. 83 For more about the go.mod file, see 'go help go.mod'. 84 85 To start a new module, simply create a go.mod file in the root of the 86 module's directory tree, containing only a module statement. 87 The 'go mod init' command can be used to do this: 88 89 go mod init example.com/m 90 91 In a project already using an existing dependency management tool like 92 godep, glide, or dep, 'go mod init' will also add require statements 93 matching the existing configuration. 94 95 Once the go.mod file exists, no additional steps are required: 96 go commands like 'go build', 'go test', or even 'go list' will automatically 97 add new dependencies as needed to satisfy imports. 98 99 The main module and the build list 100 101 The "main module" is the module containing the directory where the go command 102 is run. The go command finds the module root by looking for a go.mod in the 103 current directory, or else the current directory's parent directory, 104 or else the parent's parent directory, and so on. 105 106 The main module's go.mod file defines the precise set of packages available 107 for use by the go command, through require, replace, and exclude statements. 108 Dependency modules, found by following require statements, also contribute 109 to the definition of that set of packages, but only through their go.mod 110 files' require statements: any replace and exclude statements in dependency 111 modules are ignored. The replace and exclude statements therefore allow the 112 main module complete control over its own build, without also being subject 113 to complete control by dependencies. 114 115 The set of modules providing packages to builds is called the "build list". 116 The build list initially contains only the main module. Then the go command 117 adds to the list the exact module versions required by modules already 118 on the list, recursively, until there is nothing left to add to the list. 119 If multiple versions of a particular module are added to the list, 120 then at the end only the latest version (according to semantic version 121 ordering) is kept for use in the build. 122 123 The 'go list' command provides information about the main module 124 and the build list. For example: 125 126 go list -m # print path of main module 127 go list -m -f={{.Dir}} # print root directory of main module 128 go list -m all # print build list 129 130 Maintaining module requirements 131 132 The go.mod file is meant to be readable and editable by both 133 programmers and tools. The go command itself automatically updates the go.mod file 134 to maintain a standard formatting and the accuracy of require statements. 135 136 Any go command that finds an unfamiliar import will look up the module 137 containing that import and add the latest version of that module 138 to go.mod automatically. In most cases, therefore, it suffices to 139 add an import to source code and run 'go build', 'go test', or even 'go list': 140 as part of analyzing the package, the go command will discover 141 and resolve the import and update the go.mod file. 142 143 Any go command can determine that a module requirement is 144 missing and must be added, even when considering only a single 145 package from the module. On the other hand, determining that a module requirement 146 is no longer necessary and can be deleted requires a full view of 147 all packages in the module, across all possible build configurations 148 (architectures, operating systems, build tags, and so on). 149 The 'go mod tidy' command builds that view and then 150 adds any missing module requirements and removes unnecessary ones. 151 152 As part of maintaining the require statements in go.mod, the go command 153 tracks which ones provide packages imported directly by the current module 154 and which ones provide packages only used indirectly by other module 155 dependencies. Requirements needed only for indirect uses are marked with a 156 "// indirect" comment in the go.mod file. Indirect requirements are 157 automatically removed from the go.mod file once they are implied by other 158 direct requirements. Indirect requirements only arise when using modules 159 that fail to state some of their own dependencies or when explicitly 160 upgrading a module's dependencies ahead of its own stated requirements. 161 162 Because of this automatic maintenance, the information in go.mod is an 163 up-to-date, readable description of the build. 164 165 The 'go get' command updates go.mod to change the module versions used in a 166 build. An upgrade of one module may imply upgrading others, and similarly a 167 downgrade of one module may imply downgrading others. The 'go get' command 168 makes these implied changes as well. If go.mod is edited directly, commands 169 like 'go build' or 'go list' will assume that an upgrade is intended and 170 automatically make any implied upgrades and update go.mod to reflect them. 171 172 The 'go mod' command provides other functionality for use in maintaining 173 and understanding modules and go.mod files. See 'go help mod'. 174 175 The -mod build flag provides additional control over updating and use of go.mod. 176 177 If invoked with -mod=readonly, the go command is disallowed from the implicit 178 automatic updating of go.mod described above. Instead, it fails when any changes 179 to go.mod are needed. This setting is most useful to check that go.mod does 180 not need updates, such as in a continuous integration and testing system. 181 The "go get" command remains permitted to update go.mod even with -mod=readonly, 182 and the "go mod" commands do not take the -mod flag (or any other build flags). 183 184 If invoked with -mod=vendor, the go command assumes that the vendor 185 directory holds the correct copies of dependencies and ignores 186 the dependency descriptions in go.mod. 187 188 Pseudo-versions 189 190 The go.mod file and the go command more generally use semantic versions as 191 the standard form for describing module versions, so that versions can be 192 compared to determine which should be considered earlier or later than another. 193 A module version like v1.2.3 is introduced by tagging a revision in the 194 underlying source repository. Untagged revisions can be referred to 195 using a "pseudo-version" like v0.0.0-yyyymmddhhmmss-abcdefabcdef, 196 where the time is the commit time in UTC and the final suffix is the prefix 197 of the commit hash. The time portion ensures that two pseudo-versions can 198 be compared to determine which happened later, the commit hash identifes 199 the underlying commit, and the prefix (v0.0.0- in this example) is derived from 200 the most recent tagged version in the commit graph before this commit. 201 202 There are three pseudo-version forms: 203 204 vX.0.0-yyyymmddhhmmss-abcdefabcdef is used when there is no earlier 205 versioned commit with an appropriate major version before the target commit. 206 (This was originally the only form, so some older go.mod files use this form 207 even for commits that do follow tags.) 208 209 vX.Y.Z-pre.0.yyyymmddhhmmss-abcdefabcdef is used when the most 210 recent versioned commit before the target commit is vX.Y.Z-pre. 211 212 vX.Y.(Z+1)-0.yyyymmddhhmmss-abcdefabcdef is used when the most 213 recent versioned commit before the target commit is vX.Y.Z. 214 215 Pseudo-versions never need to be typed by hand: the go command will accept 216 the plain commit hash and translate it into a pseudo-version (or a tagged 217 version if available) automatically. This conversion is an example of a 218 module query. 219 220 Module queries 221 222 The go command accepts a "module query" in place of a module version 223 both on the command line and in the main module's go.mod file. 224 (After evaluating a query found in the main module's go.mod file, 225 the go command updates the file to replace the query with its result.) 226 227 A fully-specified semantic version, such as "v1.2.3", 228 evaluates to that specific version. 229 230 A semantic version prefix, such as "v1" or "v1.2", 231 evaluates to the latest available tagged version with that prefix. 232 233 A semantic version comparison, such as "<v1.2.3" or ">=v1.5.6", 234 evaluates to the available tagged version nearest to the comparison target 235 (the latest version for < and <=, the earliest version for > and >=). 236 237 The string "latest" matches the latest available tagged version, 238 or else the underlying source repository's latest untagged revision. 239 240 A revision identifier for the underlying source repository, 241 such as a commit hash prefix, revision tag, or branch name, 242 selects that specific code revision. If the revision is 243 also tagged with a semantic version, the query evaluates to 244 that semantic version. Otherwise the query evaluates to a 245 pseudo-version for the commit. 246 247 All queries prefer release versions to pre-release versions. 248 For example, "<v1.2.3" will prefer to return "v1.2.2" 249 instead of "v1.2.3-pre1", even though "v1.2.3-pre1" is nearer 250 to the comparison target. 251 252 Module versions disallowed by exclude statements in the 253 main module's go.mod are considered unavailable and cannot 254 be returned by queries. 255 256 For example, these commands are all valid: 257 258 go get github.com/gorilla/mux@latest # same (@latest is default for 'go get') 259 go get github.com/gorilla/mux@v1.6.2 # records v1.6.2 260 go get github.com/gorilla/mux@e3702bed2 # records v1.6.2 261 go get github.com/gorilla/mux@c856192 # records v0.0.0-20180517173623-c85619274f5d 262 go get github.com/gorilla/mux@master # records current meaning of master 263 264 Module compatibility and semantic versioning 265 266 The go command requires that modules use semantic versions and expects that 267 the versions accurately describe compatibility: it assumes that v1.5.4 is a 268 backwards-compatible replacement for v1.5.3, v1.4.0, and even v1.0.0. 269 More generally the go command expects that packages follow the 270 "import compatibility rule", which says: 271 272 "If an old package and a new package have the same import path, 273 the new package must be backwards compatible with the old package." 274 275 Because the go command assumes the import compatibility rule, 276 a module definition can only set the minimum required version of one 277 of its dependencies: it cannot set a maximum or exclude selected versions. 278 Still, the import compatibility rule is not a guarantee: it may be that 279 v1.5.4 is buggy and not a backwards-compatible replacement for v1.5.3. 280 Because of this, the go command never updates from an older version 281 to a newer version of a module unasked. 282 283 In semantic versioning, changing the major version number indicates a lack 284 of backwards compatibility with earlier versions. To preserve import 285 compatibility, the go command requires that modules with major version v2 286 or later use a module path with that major version as the final element. 287 For example, version v2.0.0 of example.com/m must instead use module path 288 example.com/m/v2, and packages in that module would use that path as 289 their import path prefix, as in example.com/m/v2/sub/pkg. Including the 290 major version number in the module path and import paths in this way is 291 called "semantic import versioning". Pseudo-versions for modules with major 292 version v2 and later begin with that major version instead of v0, as in 293 v2.0.0-20180326061214-4fc5987536ef. 294 295 As a special case, module paths beginning with gopkg.in/ continue to use the 296 conventions established on that system: the major version is always present, 297 and it is preceded by a dot instead of a slash: gopkg.in/yaml.v1 298 and gopkg.in/yaml.v2, not gopkg.in/yaml and gopkg.in/yaml/v2. 299 300 The go command treats modules with different module paths as unrelated: 301 it makes no connection between example.com/m and example.com/m/v2. 302 Modules with different major versions can be used together in a build 303 and are kept separate by the fact that their packages use different 304 import paths. 305 306 In semantic versioning, major version v0 is for initial development, 307 indicating no expectations of stability or backwards compatibility. 308 Major version v0 does not appear in the module path, because those 309 versions are preparation for v1.0.0, and v1 does not appear in the 310 module path either. 311 312 Code written before the semantic import versioning convention 313 was introduced may use major versions v2 and later to describe 314 the same set of unversioned import paths as used in v0 and v1. 315 To accommodate such code, if a source code repository has a 316 v2.0.0 or later tag for a file tree with no go.mod, the version is 317 considered to be part of the v1 module's available versions 318 and is given an +incompatible suffix when converted to a module 319 version, as in v2.0.0+incompatible. The +incompatible tag is also 320 applied to pseudo-versions derived from such versions, as in 321 v2.0.1-0.yyyymmddhhmmss-abcdefabcdef+incompatible. 322 323 In general, having a dependency in the build list (as reported by 'go list -m all') 324 on a v0 version, pre-release version, pseudo-version, or +incompatible version 325 is an indication that problems are more likely when upgrading that 326 dependency, since there is no expectation of compatibility for those. 327 328 See https://research.swtch.com/vgo-import for more information about 329 semantic import versioning, and see https://semver.org/ for more about 330 semantic versioning. 331 332 Module code layout 333 334 For now, see https://research.swtch.com/vgo-module for information 335 about how source code in version control systems is mapped to 336 module file trees. 337 338 Module downloading and verification 339 340 The go command maintains, in the main module's root directory alongside 341 go.mod, a file named go.sum containing the expected cryptographic checksums 342 of the content of specific module versions. Each time a dependency is 343 used, its checksum is added to go.sum if missing or else required to match 344 the existing entry in go.sum. 345 346 The go command maintains a cache of downloaded packages and computes 347 and records the cryptographic checksum of each package at download time. 348 In normal operation, the go command checks these pre-computed checksums 349 against the main module's go.sum file, instead of recomputing them on 350 each command invocation. The 'go mod verify' command checks that 351 the cached copies of module downloads still match both their recorded 352 checksums and the entries in go.sum. 353 354 The go command can fetch modules from a proxy instead of connecting 355 to source control systems directly, according to the setting of the GOPROXY 356 environment variable. 357 358 See 'go help goproxy' for details about the proxy and also the format of 359 the cached downloaded packages. 360 361 Modules and vendoring 362 363 When using modules, the go command completely ignores vendor directories. 364 365 By default, the go command satisfies dependencies by downloading modules 366 from their sources and using those downloaded copies (after verification, 367 as described in the previous section). To allow interoperation with older 368 versions of Go, or to ensure that all files used for a build are stored 369 together in a single file tree, 'go mod vendor' creates a directory named 370 vendor in the root directory of the main module and stores there all the 371 packages from dependency modules that are needed to support builds and 372 tests of packages in the main module. 373 374 To build using the main module's top-level vendor directory to satisfy 375 dependencies (disabling use of the usual network sources and local 376 caches), use 'go build -mod=vendor'. Note that only the main module's 377 top-level vendor directory is used; vendor directories in other locations 378 are still ignored. 379 `, 380 } 381 382 var HelpGoMod = &base.Command{ 383 UsageLine: "go.mod", 384 Short: "the go.mod file", 385 Long: ` 386 A module version is defined by a tree of source files, with a go.mod 387 file in its root. When the go command is run, it looks in the current 388 directory and then successive parent directories to find the go.mod 389 marking the root of the main (current) module. 390 391 The go.mod file itself is line-oriented, with // comments but 392 no /* */ comments. Each line holds a single directive, made up of a 393 verb followed by arguments. For example: 394 395 module my/thing 396 require other/thing v1.0.2 397 require new/thing v2.3.4 398 exclude old/thing v1.2.3 399 replace bad/thing v1.4.5 => good/thing v1.4.5 400 401 The verbs are module, to define the module path; require, to require 402 a particular module at a given version or later; exclude, to exclude 403 a particular module version from use; and replace, to replace a module 404 version with a different module version. Exclude and replace apply only 405 in the main module's go.mod and are ignored in dependencies. 406 See https://research.swtch.com/vgo-mvs for details. 407 408 The leading verb can be factored out of adjacent lines to create a block, 409 like in Go imports: 410 411 require ( 412 new/thing v2.3.4 413 old/thing v1.2.3 414 ) 415 416 The go.mod file is designed both to be edited directly and to be 417 easily updated by tools. The 'go mod edit' command can be used to 418 parse and edit the go.mod file from programs and tools. 419 See 'go help mod edit'. 420 421 The go command automatically updates go.mod each time it uses the 422 module graph, to make sure go.mod always accurately reflects reality 423 and is properly formatted. For example, consider this go.mod file: 424 425 module M 426 427 require ( 428 A v1 429 B v1.0.0 430 C v1.0.0 431 D v1.2.3 432 E dev 433 ) 434 435 exclude D v1.2.3 436 437 The update rewrites non-canonical version identifiers to semver form, 438 so A's v1 becomes v1.0.0 and E's dev becomes the pseudo-version for the 439 latest commit on the dev branch, perhaps v0.0.0-20180523231146-b3f5c0f6e5f1. 440 441 The update modifies requirements to respect exclusions, so the 442 requirement on the excluded D v1.2.3 is updated to use the next 443 available version of D, perhaps D v1.2.4 or D v1.3.0. 444 445 The update removes redundant or misleading requirements. 446 For example, if A v1.0.0 itself requires B v1.2.0 and C v1.0.0, 447 then go.mod's requirement of B v1.0.0 is misleading (superseded by 448 A's need for v1.2.0), and its requirement of C v1.0.0 is redundant 449 (implied by A's need for the same version), so both will be removed. 450 If module M contains packages that directly import packages from B or 451 C, then the requirements will be kept but updated to the actual 452 versions being used. 453 454 Finally, the update reformats the go.mod in a canonical formatting, so 455 that future mechanical changes will result in minimal diffs. 456 457 Because the module graph defines the meaning of import statements, any 458 commands that load packages also use and therefore update go.mod, 459 including go build, go get, go install, go list, go test, go mod graph, 460 go mod tidy, and go mod why. 461 `, 462 }