// Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package modload import "cmd/go/internal/base" // TODO(rsc): The "module code layout" section needs to be written. var HelpModules = &base.Command{ UsageLine: "modules", Short: "modules, module versions, and more", Long: ` A module is a collection of related Go packages. Modules are the unit of source code interchange and versioning. The go command has direct support for working with modules, including recording and resolving dependencies on other modules. Modules replace the old GOPATH-based approach to specifying which source files are used in a given build. Module support The go command includes support for Go modules. Module-aware mode is active by default whenever a go.mod file is found in the current directory or in any parent directory. The quickest way to take advantage of module support is to check out your repository, create a go.mod file (described in the next section) there, and run go commands from within that file tree. For more fine-grained control, the go command continues to respect a temporary environment variable, GO111MODULE, which can be set to one of three string values: off, on, or auto (the default). If GO111MODULE=on, then the go command requires the use of modules, never consulting GOPATH. We refer to this as the command being module-aware or running in "module-aware mode". If GO111MODULE=off, then the go command never uses module support. Instead it looks in vendor directories and GOPATH to find dependencies; we now refer to this as "GOPATH mode." If GO111MODULE=auto or is unset, then the go command enables or disables module support based on the current directory. Module support is enabled only when the current directory contains a go.mod file or is below a directory containing a go.mod file. In module-aware mode, GOPATH no longer defines the meaning of imports during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod) and installed commands (in GOPATH/bin, unless GOBIN is set). Defining a module A module is defined by a tree of Go source files with a go.mod file in the tree's root directory. The directory containing the go.mod file is called the module root. Typically the module root will also correspond to a source code repository root (but in general it need not). The module is the set of all Go packages in the module root and its subdirectories, but excluding subtrees with their own go.mod files. The "module path" is the import path prefix corresponding to the module root. The go.mod file defines the module path and lists the specific versions of other modules that should be used when resolving imports during a build, by giving their module paths and versions. For example, this go.mod declares that the directory containing it is the root of the module with path example.com/m, and it also declares that the module depends on specific versions of golang.org/x/text and gopkg.in/yaml.v2: module example.com/m require ( golang.org/x/text v0.3.0 gopkg.in/yaml.v2 v2.1.0 ) The go.mod file can also specify replacements and excluded versions that only apply when building the module directly; they are ignored when the module is incorporated into a larger build. For more about the go.mod file, see 'go help go.mod'. To start a new module, simply create a go.mod file in the root of the module's directory tree, containing only a module statement. The 'go mod init' command can be used to do this: go mod init example.com/m In a project already using an existing dependency management tool like godep, glide, or dep, 'go mod init' will also add require statements matching the existing configuration. Once the go.mod file exists, no additional steps are required: go commands like 'go build', 'go test', or even 'go list' will automatically add new dependencies as needed to satisfy imports. The main module and the build list The "main module" is the module containing the directory where the go command is run. The go command finds the module root by looking for a go.mod in the current directory, or else the current directory's parent directory, or else the parent's parent directory, and so on. The main module's go.mod file defines the precise set of packages available for use by the go command, through require, replace, and exclude statements. Dependency modules, found by following require statements, also contribute to the definition of that set of packages, but only through their go.mod files' require statements: any replace and exclude statements in dependency modules are ignored. The replace and exclude statements therefore allow the main module complete control over its own build, without also being subject to complete control by dependencies. The set of modules providing packages to builds is called the "build list". The build list initially contains only the main module. Then the go command adds to the list the exact module versions required by modules already on the list, recursively, until there is nothing left to add to the list. If multiple versions of a particular module are added to the list, then at the end only the latest version (according to semantic version ordering) is kept for use in the build. The 'go list' command provides information about the main module and the build list. For example: go list -m # print path of main module go list -m -f={{.Dir}} # print root directory of main module go list -m all # print build list Maintaining module requirements The go.mod file is meant to be readable and editable by both programmers and tools. Most updates to dependencies can be performed using "go get" and "go mod tidy". Other module-aware build commands may be invoked using the -mod=mod flag to automatically add missing requirements and fix inconsistencies. The "go get" command updates go.mod to change the module versions used in a build. An upgrade of one module may imply upgrading others, and similarly a downgrade of one module may imply downgrading others. The "go get" command makes these implied changes as well. See "go help module-get". The "go mod" command provides other functionality for use in maintaining and understanding modules and go.mod files. See "go help mod", particularly "go help mod tidy" and "go help mod edit". As part of maintaining the require statements in go.mod, the go command tracks which ones provide packages imported directly by the current module and which ones provide packages only used indirectly by other module dependencies. Requirements needed only for indirect uses are marked with a "// indirect" comment in the go.mod file. Indirect requirements may be automatically removed from the go.mod file once they are implied by other direct requirements. Indirect requirements only arise when using modules that fail to state some of their own dependencies or when explicitly upgrading a module's dependencies ahead of its own stated requirements. The -mod build flag provides additional control over the updating and use of go.mod for commands that build packages like "go build" and "go test". If invoked with -mod=readonly (the default in most situations), the go command reports an error if a package named on the command line or an imported package is not provided by any module in the build list computed from the main module's requirements. The go command also reports an error if a module's checksum is missing from go.sum (see Module downloading and verification). Either go.mod or go.sum must be updated in these situations. If invoked with -mod=mod, the go command automatically updates go.mod and go.sum, fixing inconsistencies and adding missing requirements and checksums as needed. If the go command finds an unfamiliar import, it looks up the module containing that import and adds a requirement for the latest version of that module to go.mod. In most cases, therefore, one may add an import to source code and run "go build", "go test", or even "go list" with -mod=mod: as part of analyzing the package, the go command will resolve the import and update the go.mod file. If invoked with -mod=vendor, the go command loads packages from the main module's vendor directory instead of downloading modules to and loading packages from the module cache. The go command assumes the vendor directory holds correct copies of dependencies, and it does not compute the set of required module versions from go.mod files. However, the go command does check that vendor/modules.txt (generated by "go mod vendor") contains metadata consistent with go.mod. If the go command is not invoked with a -mod flag, and the vendor directory is present, and the "go" version in go.mod is 1.14 or higher, the go command will act as if it were invoked with -mod=vendor. Otherwise, the -mod flag defaults to -mod=readonly. Note that neither "go get" nor the "go mod" subcommands accept the -mod flag. Pseudo-versions The go.mod file and the go command more generally use semantic versions as the standard form for describing module versions, so that versions can be compared to determine which should be considered earlier or later than another. A module version like v1.2.3 is introduced by tagging a revision in the underlying source repository. Untagged revisions can be referred to using a "pseudo-version" like v0.0.0-yyyymmddhhmmss-abcdefabcdef, where the time is the commit time in UTC and the final suffix is the prefix of the commit hash. The time portion ensures that two pseudo-versions can be compared to determine which happened later, the commit hash identifes the underlying commit, and the prefix (v0.0.0- in this example) is derived from the most recent tagged version in the commit graph before this commit. There are three pseudo-version forms: vX.0.0-yyyymmddhhmmss-abcdefabcdef is used when there is no earlier versioned commit with an appropriate major version before the target commit. (This was originally the only form, so some older go.mod files use this form even for commits that do follow tags.) vX.Y.Z-pre.0.yyyymmddhhmmss-abcdefabcdef is used when the most recent versioned commit before the target commit is vX.Y.Z-pre. vX.Y.(Z+1)-0.yyyymmddhhmmss-abcdefabcdef is used when the most recent versioned commit before the target commit is vX.Y.Z. Pseudo-versions never need to be typed by hand: the go command will accept the plain commit hash and translate it into a pseudo-version (or a tagged version if available) automatically. This conversion is an example of a module query. Module queries The go command accepts a "module query" in place of a module version both on the command line and in the main module's go.mod file. (After evaluating a query found in the main module's go.mod file, the go command updates the file to replace the query with its result.) A fully-specified semantic version, such as "v1.2.3", evaluates to that specific version. A semantic version prefix, such as "v1" or "v1.2", evaluates to the latest available tagged version with that prefix. A semantic version comparison, such as "=v1.5.6", evaluates to the available tagged version nearest to the comparison target (the latest version for < and <=, the earliest version for > and >=). The string "latest" matches the latest available tagged version, or else the underlying source repository's latest untagged revision. The string "upgrade" is like "latest", but if the module is currently required at a later version than the version "latest" would select (for example, a newer pre-release version), "upgrade" will select the later version instead. The string "patch" matches the latest available tagged version of a module with the same major and minor version numbers as the currently required version. If no version is currently required, "patch" is equivalent to "latest". A revision identifier for the underlying source repository, such as a commit hash prefix, revision tag, or branch name, selects that specific code revision. If the revision is also tagged with a semantic version, the query evaluates to that semantic version. Otherwise the query evaluates to a pseudo-version for the commit. Note that branches and tags with names that are matched by other query syntax cannot be selected this way. For example, the query "v2" means the latest version starting with "v2", not the branch named "v2". All queries prefer release versions to pre-release versions. For example, " good/thing v1.4.5 retract v1.5.6 The verbs are module, to define the module path; go, to set the expected language version; require, to require a particular module at a given version or later; exclude, to exclude a particular module version from use; replace, to replace a module version with a different module version; and retract, to indicate a previously released version should not be used. Exclude and replace apply only in the main module's go.mod and are ignored in dependencies. See https://golang.org/ref/mod for details. The leading verb can be factored out of adjacent lines to create a block, like in Go imports: require ( new/thing v2.3.4 old/thing v1.2.3 ) The go.mod file is designed both to be edited directly and to be easily updated by tools. The 'go mod edit' command can be used to parse and edit the go.mod file from programs and tools. See 'go help mod edit'. The go command automatically updates go.mod each time it uses the module graph, to make sure go.mod always accurately reflects reality and is properly formatted. For example, consider this go.mod file: module M require ( A v1 B v1.0.0 C v1.0.0 D v1.2.3 E dev ) exclude D v1.2.3 The update rewrites non-canonical version identifiers to semver form, so A's v1 becomes v1.0.0 and E's dev becomes the pseudo-version for the latest commit on the dev branch, perhaps v0.0.0-20180523231146-b3f5c0f6e5f1. The update modifies requirements to respect exclusions, so the requirement on the excluded D v1.2.3 is updated to use the next available version of D, perhaps D v1.2.4 or D v1.3.0. The update removes redundant or misleading requirements. For example, if A v1.0.0 itself requires B v1.2.0 and C v1.0.0, then go.mod's requirement of B v1.0.0 is misleading (superseded by A's need for v1.2.0), and its requirement of C v1.0.0 is redundant (implied by A's need for the same version), so both will be removed. If module M contains packages that directly import packages from B or C, then the requirements will be kept but updated to the actual versions being used. Finally, the update reformats the go.mod in a canonical formatting, so that future mechanical changes will result in minimal diffs. Because the module graph defines the meaning of import statements, any commands that load packages also use and therefore update go.mod, including go build, go get, go install, go list, go test, go mod graph, go mod tidy, and go mod why. The expected language version, set by the go directive, determines which language features are available when compiling the module. Language features available in that version will be available for use. Language features removed in earlier versions, or added in later versions, will not be available. Note that the language version does not affect build tags, which are determined by the Go release being used. `, }