Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 1 | # Go 1.11 Modules |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 2 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 3 | Go 1.11 will add preliminary support for versioned modules as proposed [here](https://golang.org/design/24301-versioned-go). |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 4 | |
thepudds | 847a5de | 2018-07-27 07:54:08 -0400 | [diff] [blame] | 5 | Go modules will be an [experimental](https://research.swtch.com/vgo-accepted) opt-in feature in Go 1.11, with the hope of incorporating feedback and finalizing the feature for Go 1.12. Even though the details may change, future releases will support modules defined using Go 1.11 or `vgo`. |
thepudds | f398d51 | 2018-07-22 18:10:33 -0400 | [diff] [blame] | 6 | |
| 7 | ## Current Status |
| 8 | |
thepudds | c7f8aed | 2018-08-01 01:34:33 -0400 | [diff] [blame] | 9 | * The recent work by the Go team on versioned Go modules started outside of the main Go repository with the `vgo` tool, but on **July 12, 2018** support for versioned Go modules **landed in the main Go repository** ([announcement thread](https://groups.google.com/d/msg/golang-dev/a5PqQuBljF4/61QK4JdtBgAJ)). |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 10 | * Development work on modules is now occurring exclusively in the main Go repository, with a periodic export to the vgo repository for people still using `vgo`. |
thepudds | a7ce6d7 | 2018-08-05 09:34:37 -0400 | [diff] [blame] | 11 | * Beta support for modules is now also available: |
thepudds | 170f51d | 2018-08-09 06:41:45 -0400 | [diff] [blame] | 12 | * Initial beta started with [Go 1.11 beta 2](https://groups.google.com/d/msg/golang-dev/A6TCp2kCoss/XLQoI4MeBgAJ) (released on **July 20, 2018**). |
Fazlul Shahriar | 5c82840 | 2018-08-05 12:07:17 -0400 | [diff] [blame] | 13 | * **Latest beta** is [Go 1.11 beta 3](https://groups.google.com/d/msg/golang-nuts/vOMqDrIwxBo/-wJvN12oCwAJ) (released on **August 3, 2018**). |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 14 | * Beta 3 and `master` include some significant changes for the `go mod` commands. See FAQ [below](https://github.com/golang/go/wiki/Modules#how-have-the-go-mod-commands-changed-recently-in-go111beta3) for an overview of these changes. |
thepudds | f398d51 | 2018-07-22 18:10:33 -0400 | [diff] [blame] | 15 | |
thepudds | a7ce6d7 | 2018-08-05 09:34:37 -0400 | [diff] [blame] | 16 | **NOTE:** Some issues you might experience: |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 17 | * There are some regressions in beta 3 compared to beta 2 (e.g., [#26722](https://github.com/golang/go/issues/26722) and [#26602](https://github.com/golang/go/issues/26602), both fixed in `master`) |
thepudds | a7ce6d7 | 2018-08-05 09:34:37 -0400 | [diff] [blame] | 18 | * Some older versions of git might not work: |
| 19 | * [#26501](https://github.com/golang/go/issues/26501) covers git 2.10.0 and earlier not working. (Fixed in beta 3 but not fixed in beta 2). |
thepudds | 7b9e5bb | 2018-08-05 09:55:03 -0400 | [diff] [blame] | 20 | * [#26594](https://github.com/golang/go/issues/26594) appears to be different problem than #26501 but might be related to older git as well. (Not fixed in beta 3 -- help triaging this particular issue is welcomed). |
| 21 | * [#26754](https://github.com/golang/go/issues/26754) is example where git 2.18.0 succeeds, but git 2.7.4 can't resolve a commit unreachable from any branch. |
thepudds | 634f341 | 2018-07-28 15:27:09 -0400 | [diff] [blame] | 22 | |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 23 | ## Table of Contents |
| 24 | |
thepudds | e4474f2 | 2018-08-02 22:26:17 -0400 | [diff] [blame] | 25 | The remaining content on this page is organized as follows: |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 26 | * [Installing and Activating Module Support](https://github.com/golang/go/wiki/Modules#installing-and-activating-module-support) |
| 27 | * [New Concepts](https://github.com/golang/go/wiki/Modules#new-concepts) |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 28 | * [Modules](https://github.com/golang/go/wiki/Modules#modules) |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 29 | * [go.mod](https://github.com/golang/go/wiki/Modules#gomod) |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 30 | * [Version Selection](https://github.com/golang/go/wiki/Modules#version-selection) |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 31 | * [Semantic Import Versioning](https://github.com/golang/go/wiki/Modules#semantic-import-versioning) |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 32 | * [How to Define a Module](https://github.com/golang/go/wiki/Modules#how-to-define-a-module) |
| 33 | * [How to Upgrade and Downgrade Dependencies](https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies) |
| 34 | * [How to Prepare for a Release](https://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release) |
| 35 | * [Additional Resources](https://github.com/golang/go/wiki/Modules#additional-resources) |
| 36 | * [Changes Since the Initial Vgo Proposal](https://github.com/golang/go/wiki/Modules#changes-since-the-initial-vgo-proposal) |
| 37 | * [GitHub Issues](https://github.com/golang/go/wiki/Modules#github-issues) |
| 38 | * [FAQ](https://github.com/golang/go/wiki/Modules#faq) |
| 39 | |
thepudds | 847a5de | 2018-07-27 07:54:08 -0400 | [diff] [blame] | 40 | ## Installing and Activating Module Support |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 41 | |
thepudds | 847a5de | 2018-07-27 07:54:08 -0400 | [diff] [blame] | 42 | To use modules, you currently have three install options: |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 43 | * [Install the Go toolchain from source](https://golang.org/doc/install/source) on the `master` branch. |
Dmitry Savintsev | cff8bfd | 2018-08-08 12:33:40 +0200 | [diff] [blame] | 44 | * [Install Go 1.11 beta 3](https://groups.google.com/forum/#!topic/golang-dev/7u0nPlsup5I) (and replace `go` with `go1.11beta3` in the commands below). |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 45 | * Install the `vgo` binary from the [`vgo` subrepository](https://github.com/golang/vgo) (and replace `go` with `vgo` in the commands below). |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 46 | |
thepudds | eef37be | 2018-07-27 08:35:11 -0400 | [diff] [blame] | 47 | Once installed, you can then activate module support in one of three ways: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 48 | * Invoke the `go` command in a directory outside of the `$GOPATH/src` tree, with a valid `go.mod` file in the current directory or any parent of it and the environment variable `GO111MODULE` unset (or explicitly set to `auto`). |
| 49 | * Invoke the `go` command with `GO111MODULE=on` in the command environment. |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 50 | * Invoke the binary named `vgo` (if you have installed `vgo` from the subrepository). |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 51 | |
thepudds | 140883f | 2018-08-13 13:10:20 -0400 | [diff] [blame] | 52 | In addition to trying modules-specific workflows: |
| 53 | * There is also benefit in running your general tests to catch any new issues early. |
| 54 | * Please consider adding `1.11beta3` or similar to your CI (such as in this example [Travis yaml file](https://github.com/gomods/athens/blob/master/.travis.yml#L6), or following the build matrix pattern shown in this [CircleCI 2.0 yaml file](https://discuss.circleci.com/t/build-matrix-configuration/14448); see [this thread](https://groups.google.com/d/msg/golang-dev/ONpj39nviNg/Zn9rPADhCwAJ) for more related discussion). |
| 55 | |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 56 | ## New Concepts |
| 57 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 58 | These sections provide a high-level introduction to the main new concepts. For more details and rationale, please see [the official proposal document](), this 40 minute introductory [video by Russ Cox describing the philosophy behind the design](https://www.youtube.com/watch?v=F8nrpe0XWRg&list=PLq2Nv-Sh8EbbIjQgDzapOFeVfv5bGOoPE&index=3&t=0s), or the more detailed initial [vgo blog series](https://research.swtch.com/vgo). |
| 59 | |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 60 | ### Modules |
| 61 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 62 | A module is a collection of related Go packages that are versioned together as a single unit. Most often, a single version-control repository corresponds exactly to a single module, but alternatively a single version-control repository can hold multiple modules. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 63 | |
thepudds | 80d05d3 | 2018-08-13 07:38:09 -0400 | [diff] [blame] | 64 | Modules must be [semantically versioned](https://semver.org/) in the form `v(major).(minor).(patch)`, such as `v0.1.0`, `v1.2.3`, or `v3.0.1`. If using Git, [tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) released commits with their versions. (Stand-alone distributed module repositories, such as [Project Athens](https://github.com/gomods/athens), are in the works.) |
| 65 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 66 | ### go.mod |
| 67 | |
| 68 | A module is defined by a tree of Go source files with a `go.mod` file in the tree's root directory. Module source code may be located outside of $GOPATH. |
| 69 | |
thepudds | 4975e28 | 2018-08-05 12:47:20 -0400 | [diff] [blame] | 70 | All of the packages in a module share a common prefix -- the module path. The `go.mod` file defines the module path via the `module` directive. For example, the first line in a `go.mod` file typically would be `module example.com/my/module` if it was defining a module for packages `example.com/my/module/pkg/foo` and `example.com/my/module/pkg/bar`. |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 71 | |
| 72 | Module files may include comments and will look familiar to a go programmer. Here is an example go.mod file: |
| 73 | |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 74 | ``` |
| 75 | module github.com/kardianos/vmain/v3 |
| 76 | |
| 77 | require ( |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 78 | github.com/kardianos/vtest v1.0.2 |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 79 | ) |
| 80 | ``` |
| 81 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 82 | There are 4 directives: `module`, `require`, `exclude`, `replace`. Module paths may be quoted but are not required to be. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 83 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 84 | `exclude` and `replace` directives only operate on the current (“main”) module. `exclude` and `replace` directives in modules other than the main module are ignored when building the main module. 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. (TODO: show example exclude and replace directives and/or FAQ on when to use them). |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 85 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 86 | In Go source code, packages are imported using the full path including the module, for example: |
| 87 | * `import "example.com/my/module/v2/pkg/foo"` to import `foo` from module `example.com/my/module/v2`. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 88 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 89 | ### Version Selection |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 90 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 91 | If you add a new import to your source code that is not yet covered by a `require` in `go.mod`, any go command run (e.g., 'go build') will automatically look up the proper module and add the *highest* version of that new direct dependency to your module's `go.mod` as a `require` directive. For example, if your new import corresponds to dependency M whose latest tagged release version is `v1.2.3`, your module's `go.mod` will end up with `require M v1.2.3`, which indicates module M is a dependency with allowed version >= v1.2.3 (and < v2, given v2 is considered incompatible with v1). |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 92 | |
thepudds | ca4878d | 2018-08-03 13:06:14 -0400 | [diff] [blame] | 93 | The *minimal version selection* algorithm is used to select the versions of all modules used in a build. For each module in a build, the version selected by minimal version selection is always the semantically *highest* of the versions explicitly listed by a `require` directive in the main module or one of its dependencies. This effectively locks each version into place until the module author or user chooses an explicit new version or chooses to upgrade to the latest available version. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 94 | |
thepudds | c164d5a | 2018-08-03 13:04:28 -0400 | [diff] [blame] | 95 | For example, if your module A depends on B which has a `require D v1.0.0`, and your module also depends on module C which has a `require D v1.1.0`, then minimal version selection would choose `v1.1.0` of D to include in the build (even if a version v1.2.0 of D is available). |
| 96 | |
thepudds | 91ffcea | 2018-08-02 23:55:11 -0400 | [diff] [blame] | 97 | For a brief rationale and overview of the minimal version selection algorithm, [see the "High Fidelity Builds" section](https://github.com/golang/proposal/blob/master/design/24301-versioned-go.md#update-timing--high-fidelity-builds) of the official proposal, or see the [more detailed `vgo` blog series](https://research.swtch.com/vgo). |
thepudds | 5077edf | 2018-08-02 09:01:12 -0400 | [diff] [blame] | 98 | |
thepudds | 01d386c | 2018-08-02 09:48:29 -0400 | [diff] [blame] | 99 | To see a list of the selected module versions, use `go list -m`. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 100 | |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 101 | See also the ["How to Upgrade and Downgrade Dependencies"](https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies) section below and the ["How are versions marked as incompatible?"](https://github.com/golang/go/wiki/Modules#how-are-versions-marked-as-incompatible) FAQ below. |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 102 | |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 103 | ### Semantic Import Versioning |
| 104 | |
thepudds | 74c22d7 | 2018-08-05 12:50:37 -0400 | [diff] [blame] | 105 | The major version of a module must be included in both the module path and the package import path if the major version is v2 or higher, such as `me.io/my/mod/v2/pkg`. Module versions of v1 and v0 must not be included in the path. In Go, packages with different import paths (e.g., due to different major versions) are different packages. Thus `me.io/my/mod/pkg` is a different package than `me.io/my/mod/v2/pkg`, and both may be imported in a single build, which among other benefits allows a v1 module to be implemented in terms of its v2 replacement or vice versa. |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 106 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 107 | Including major versions in import paths will produce incompatibilities with old versions of Go. To work around this prior versions of the go tool have been updated and released to continue building as before when they encounter major versions in import paths. (See [issue 25069](https://github.com/golang/go/issues/25069).) |
| 108 | |
| 109 | There are two ways to release a v2 or higher module version. Using the example of creating a `v2.0.0` release, the two options are: |
| 110 | 1. Update the `go.mod` file to include a `/v2` at the end of the module path. Tag the release with `v2.0.0`. |
| 111 | * To avoid confusion with this approach, consider putting the `v2.*.*` commits on a separate v2 branch. |
| 112 | 2. Alternatively, create a `v2` directory and place a new `go.mod` file in that directory. The module path must end with `/v2`. Tag the release with `v2.0.0`. |
| 113 | |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 114 | The behavior of modules for existing packages with post-`v1` tags is still in flux; an important related recent change was [issue 26238](https://golang.org/issue/26238), which substantially [improved the behavior](https://github.com/golang/go/issues/25967#issuecomment-407567904) for existing packages with post-`v1` tags. |
| 115 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 116 | ## How to Define a Module |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 117 | |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 118 | To create a `go.mod` for an existing project: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 119 | |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 120 | 1. Navigate to the root of the module's source tree and activate module mode in the `go` command: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 121 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 122 | ``` |
| 123 | $ cd $GOPATH/src/<project path> |
| 124 | $ export GO111MODULE=on |
| 125 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 126 | |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 127 | or: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 128 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 129 | ``` |
| 130 | $ cd <project path outside $GOPATH/src> |
| 131 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 132 | |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 133 | 2. Create the initial module definition and write it to the `go.mod` file: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 134 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 135 | ``` |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 136 | # if using 'master', go1.11beta3, or the latest `vgo`, use the newer form: |
| 137 | $ go mod init |
thepudds | f105431 | 2018-08-01 01:30:34 -0400 | [diff] [blame] | 138 | |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 139 | # in go1.11beta2 and earlier, the older form was: |
| 140 | $ go mod -init |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 141 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 142 | |
thepudds | 9bdb272 | 2018-07-27 07:02:48 -0400 | [diff] [blame] | 143 | This step converts from any existing [`dep`](https://github.com/golang/dep) `Gopkg.lock` file or from any of the other [nine total supported dependency formats](https://tip.golang.org/pkg/cmd/go/internal/modconv/?m=all#pkg-variables), adding require statements to match the existing configuration. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 144 | |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 145 | If `go mod` cannot automatically determine an appropriate module path (e.g., if running outside of VCS), or if you need to override that path, use the `-module` flag: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 146 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 147 | ``` |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 148 | # if using 'master', go1.11beta3, or the latest `vgo`, use the newer form: |
thepudds | f105431 | 2018-08-01 01:30:34 -0400 | [diff] [blame] | 149 | $ go mod init example.com/my/module/v2 |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 150 | |
| 151 | # in go1.11beta2 and earlier, the older form was: |
| 152 | $ go mod -init -module example.com/my/module/v2 |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 153 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 154 | |
thepudds | 74241a1 | 2018-07-31 13:31:00 -0400 | [diff] [blame] | 155 | 3. Build the module. This will automatically add missing or unconverted dependencies as needed to satisfy imports for this particular build invocation: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 156 | |
| 157 | ``` |
thepudds | 74241a1 | 2018-07-31 13:31:00 -0400 | [diff] [blame] | 158 | $ go build ./... |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 159 | ``` |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 160 | 4. Test the module as configured to ensure that it works with the selected versions: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 161 | |
| 162 | ``` |
| 163 | $ go test ./... |
| 164 | ``` |
| 165 | |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 166 | 5. (Optional) Run the tests for all imported modules (direct and indirect dependencies) to check for incompatibilities: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 167 | |
| 168 | ``` |
thepudds | 4b3d893 | 2018-07-27 06:50:53 -0400 | [diff] [blame] | 169 | $ go test all |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 170 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 171 | |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 172 | Prior to tagging a release, see the ["How to Prepare for a Release"](https://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release) section below. |
thepudds | 74241a1 | 2018-07-31 13:31:00 -0400 | [diff] [blame] | 173 | |
thepudds | 432bda3 | 2018-08-07 05:22:33 -0400 | [diff] [blame] | 174 | For more information on all of these topics, the primary entry point to the official modules documentation is [available on tip.golang.org](https://tip.golang.org/cmd/go/#hdr-Modules__module_versions__and_more). |
| 175 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 176 | ## How to Upgrade and Downgrade Dependencies |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 177 | |
thepudds | 4b3d893 | 2018-07-27 06:50:53 -0400 | [diff] [blame] | 178 | Day-to-day adding, removing, upgrading, and downgrading of dependencies should be done using 'go get', which will automatically update the `go.mod` file. |
| 179 | |
thepudds | 61c12ce | 2018-07-27 08:04:34 -0400 | [diff] [blame] | 180 | In addition, go commands like 'go build', 'go test', or even 'go list' will automatically add new dependencies as needed to satisfy imports (updating `go.mod` and downloading the new dependencies). |
thepudds | b05a844 | 2018-07-26 08:18:42 -0400 | [diff] [blame] | 181 | |
thepudds | 927647d | 2018-07-26 09:03:39 -0400 | [diff] [blame] | 182 | To upgrade to the latest version for all transitive dependencies of the current module: |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 183 | * run `go get -u` to use the latest *minor or patch* releases |
| 184 | * run `go get -u=patch` to use the latest *patch* releases |
thepudds | ee879c5 | 2018-07-25 12:08:34 -0400 | [diff] [blame] | 185 | |
thepudds | 8f75c6c | 2018-08-01 09:18:37 -0400 | [diff] [blame] | 186 | To upgrade or downgrade to a more specific version, 'go get' allows version selection to be overridden by adding an @version suffix or "module query" to the package argument, such as `go get github.com/gorilla/mux@v1.6.2`, `go get github.com/gorilla/mux@e3702bed2`, or `go get github.com/gorilla/mux@'<v1.6.2'`. |
| 187 | |
| 188 | See the ["Module-aware go get"](https://tip.golang.org/cmd/go/#hdr-Module_aware_go_get) and ["Module queries"](https://tip.golang.org/cmd/go/#hdr-Module_queries) sections of the tip documentation for more information on the topics here. |
thepudds | b05a844 | 2018-07-26 08:18:42 -0400 | [diff] [blame] | 189 | |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 190 | After upgrading or downgrading any dependencies, you may then want to run the tests again for all imported modules (direct and indirect dependencies) to check for incompatibilities: |
thepudds | ee879c5 | 2018-07-25 12:08:34 -0400 | [diff] [blame] | 191 | ``` |
thepudds | 4b3d893 | 2018-07-27 06:50:53 -0400 | [diff] [blame] | 192 | $ go test all |
thepudds | ee879c5 | 2018-07-25 12:08:34 -0400 | [diff] [blame] | 193 | ``` |
thepudds | f398d51 | 2018-07-22 18:10:33 -0400 | [diff] [blame] | 194 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 195 | ## How to Prepare for a Release |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 196 | |
thepudds | df13b28 | 2018-08-01 00:38:40 -0400 | [diff] [blame] | 197 | Best practices for creating a release of a module are expected to emerge as part of the initial modules experiment. Many of these might end up being automated by a [future 'go release' tool](https://github.com/golang/go/issues/26420). |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 198 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 199 | Some current suggested best practices to consider prior to tagging a release: |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 200 | |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame^] | 201 | * Run `go mod tidy` (or if running go1.11beta2 or earlier: `go mod -sync`) to ensure your current go.mod reflects all possible build tags/OS/architecture combinations (as described [here](https://github.com/golang/go/issues/26571)) and possibly prune any extraneous requirements (as described [here](https://tip.golang.org/cmd/go/#hdr-Maintaining_module_requirements)). |
thepudds | f918d5c | 2018-07-31 13:36:22 -0400 | [diff] [blame] | 202 | |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 203 | * Run `go test all` to test your module (including your direct and indirect dependencies) as a way of validating that the currently selected packages versions are compatible. |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 204 | * The number of possible version combinations in general is exponential in the number of modules, so you cannot expect your dependencies to have tested against all possible combinations of their dependencies. |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 205 | * As part of the modules work, `go test all` has been [re-defined to be more useful](https://research.swtch.com/vgo-cmd) to include all the packages in the current module, plus all the packages they depend on through a sequence of one or more imports, while excluding packages that don't matter in the current module. |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 206 | |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 207 | ## Additional Resources |
thepudds | f398d51 | 2018-07-22 18:10:33 -0400 | [diff] [blame] | 208 | |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 209 | ### Documentation and Proposal |
| 210 | |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 211 | * Official documentation: |
| 212 | * Latest [HTML documentation for modules on tip.golang.org](https://tip.golang.org/cmd/go/#hdr-Modules__module_versions__and_more) |
| 213 | * Run `go help modules` for more about modules. (This is the main entry point for modules topics via `go help`) |
| 214 | * Run `go help mod` for more about the `go mod` command. |
| 215 | * Run `go help module-get` for more about the behavior of `go get` when in module-aware mode. |
| 216 | * Run `go help goproxy` for more about the module proxy, including a pure file-based option via a `file:///` URL. |
thepudds | 592f743 | 2018-07-26 10:03:47 -0400 | [diff] [blame] | 217 | * The initial ["Go & Versioning"](https://research.swtch.com/vgo) series of blog posts on `vgo` by Russ Cox (first posted February 20, 2018) |
| 218 | * Official [golang.org blog post introducing the proposal](https://blog.golang.org/versioning-proposal) (March 26, 2018) |
| 219 | * This provides a more succinct overview of the proposal than the full `vgo` blog series, along with some of the history and process behind the proposal |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 220 | * Official [Versioned Go Modules Proposal](https://golang.org/design/24301-versioned-go) (last updated March 20, 2018) |
| 221 | |
| 222 | ### Introductory Material |
| 223 | |
| 224 | * Introductory blog post ["Taking Go Modules for a Spin"](https://dave.cheney.net/2018/07/14/taking-go-modules-for-a-spin) by Dave Cheney (July 14, 2018) |
| 225 | * Introductory blog post on [how to build go from tip and start using go modules](https://carolynvanslyck.com/blog/2018/07/building-go-from-source/) by Carolyn Van Slyck (July 16, 2018) |
| 226 | * Introductory [Go Meetup slides on modules](https://docs.google.com/presentation/d/1ansfXN8a_aVL-QuvQNY7xywnS78HE8aG7fPiITNQWMM/edit#slide=id.g3d87f3177d_0_0) by Chris Hines (July 16, 2018) |
| 227 | * Introductory video ["The Principles of Versions in Go"](https://www.youtube.com/watch?v=F8nrpe0XWRg&list=PLq2Nv-Sh8EbbIjQgDzapOFeVfv5bGOoPE&index=3&t=0s) from GopherCon Singapore by Russ Cox (May 2, 2018) |
| 228 | * Succinctly covers the philosophy behind the design of versioned Go modules, including the three core principles of "Compatibility", "Repeatability", and "Cooperation" |
| 229 | |
| 230 | ### Additional Material |
| 231 | |
thepudds | 592f743 | 2018-07-26 10:03:47 -0400 | [diff] [blame] | 232 | * Blog post ["Using Go modules with Travis CI"](https://dave.cheney.net/2018/07/16/using-go-modules-with-travis-ci) by Dave Cheney (July 16, 2018) |
| 233 | * Blog post ["The vgo proposal is accepted. Now what?"](https://research.swtch.com/vgo-accepted) by Russ Cox (May 29, 2018) |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 234 | * Includes summary of what it means that versioned modules are currently an experimental opt-in feature |
| 235 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 236 | ## Changes Since the Initial Vgo Proposal |
| 237 | |
thepudds | a7d3054 | 2018-08-03 18:02:38 -0400 | [diff] [blame] | 238 | As part of the proposal, prototype, and beta processes, there have been over 400 issues created by the overall community. Please continue to supply feedback. |
| 239 | |
| 240 | Here is a partial list of some of the larger changes and improvements, almost all of which were primarily based on feedback from the community: |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 241 | |
| 242 | * Top-level vendor support was retained rather than vgo-based builds ignoring vendor directories entirely ([discussion](https://groups.google.com/d/msg/golang-dev/FTMScX1fsYk/uEUSjBAHAwAJ), [CL](https://go-review.googlesource.com/c/vgo/+/118316)) |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 243 | * Backported minimal module-awareness to allow older Go versions 1.9.7+ and 1.10.3+ to more easily consume modules for v2+ projects ([discussion](https://github.com/golang/go/issues/24301#issuecomment-371228742), [CL](https://golang.org/cl/109340)) |
thepudds | 42f1dea | 2018-08-03 18:07:15 -0400 | [diff] [blame] | 244 | * Allowed vgo to use v2+ tags by default for pre-existing packages did not yet have a go.mod (recent update in related behavior described [here](https://github.com/golang/go/issues/25967#issuecomment-407567904)) |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 245 | * Added support via command `go get -u=patch` to update all transitive dependencies to the latest available patch-level versions on the same minor version ([discussion](https://research.swtch.com/vgo-cmd), [documentation](https://tip.golang.org/cmd/go/#hdr-Module_aware_go_get)) |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 246 | * Additional control via environmental variables (e.g., GOFLAGS in [#26585](https://github.com/golang/go/issues/26585), [CL](https://go-review.googlesource.com/c/go/+/126656)) |
thepudds | 55989cc | 2018-08-05 20:39:02 -0400 | [diff] [blame] | 247 | * Finer grain control on whether or not go.mod is allowed to be updated, how vendor directory is used, and whether or not network access is allowed (e.g., -mod=readonly, -mod=vendor, GOPROXY=off; related [CL](https://go-review.googlesource.com/c/go/+/126696) for recent change) |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 248 | * Added more flexible replace directives ([CL](https://go-review.googlesource.com/c/vgo/+/122400)) |
| 249 | * Added additional ways to interrogate modules (for human consumption, as well as for better editor / IDE integration) |
| 250 | * The UX of the go CLI has continued to be refined based on experiences so far (e.g., [#26581](https://github.com/golang/go/issues/26581), [CL](https://go-review.googlesource.com/c/go/+/126655)) |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 251 | * **Most likely:** additional support for warming caches for use cases such as CI or docker builds ([#26610](https://github.com/golang/go/issues/26610#issuecomment-408654653)) |
| 252 | * **Most likely**: better support for installing specific versions of programs to GOBIN ([#24250](https://github.com/golang/go/issues/24250#issuecomment-377553022)) |
| 253 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 254 | ## GitHub Issues |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 255 | |
thepudds | df13b28 | 2018-08-01 00:38:40 -0400 | [diff] [blame] | 256 | * [Currently open module issues](https://golang.org/issues?q=is%3Aopen+is%3Aissue+label:modules) |
| 257 | * [Closed module issues](https://golang.org/issues?q=is%3Aclosed+is%3Aissue+label:modules) |
| 258 | * [Closed vgo issues](https://github.com/golang/go/issues?page=3&q=-label%3Amodules+vgo+is%3Aclosed) |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 259 | * Submit a [new module issue](https://github.com/golang/go/issues/new?title=cmd%2Fgo%3A%20%3Cfill%20this%20in%3E) using 'cmd/go:' as the prefix |
| 260 | |
| 261 | ## FAQ |
| 262 | |
| 263 | ### How are versions marked as incompatible? |
| 264 | |
thepudds | d944259 | 2018-08-02 09:44:49 -0400 | [diff] [blame] | 265 | The `require` directive allows any module to declare that it should be built with version >= x.y.z of a dependency D (which may be specified due to incompatibilities with version < x.y.z of module D). Empirical data suggests [this is the dominant form of constraints used in `dep` and `cargo`](https://twitter.com/_rsc/status/1022590868967116800). In addition, the top-level module in the build can `exclude` specific versions of dependencies or `replace` other modules with different code. See the full proposal for [more details and rationale](https://github.com/golang/proposal/blob/master/design/24301-versioned-go.md). |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 266 | |
thepudds | 601d231 | 2018-08-06 23:26:24 -0400 | [diff] [blame] | 267 | One of the key goals of the versioned modules proposal is to add a common vocabulary and semantics around versions of Go code for both tools and developers. This lays a foundation for future capabilities to declare additional forms of incompatibilities, such as possibly: |
thepudds | 0bb0f8d | 2018-08-01 01:51:19 -0400 | [diff] [blame] | 268 | * declaring deprecated versions as [described](https://research.swtch.com/vgo-module) in the initial `vgo` blog series |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 269 | * declaring pair-wise incompatibility between modules in an external system as discussed for example [here](https://github.com/golang/go/issues/24301#issuecomment-392111327) during the proposal process |
thepudds | d944259 | 2018-08-02 09:44:49 -0400 | [diff] [blame] | 270 | * declaring incompatible versions or insecure versions of a module after a release has been published. See for example the on-going discussion in [#24031](https://github.com/golang/go/issues/24031#issuecomment-407798552) |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 271 | |
Dmitry Savintsev | 0e8db23 | 2018-08-08 12:28:04 +0200 | [diff] [blame] | 272 | ### How have the `go mod` commands changed recently in `go1.11beta3`? |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 273 | |
Dmitry Savintsev | 0e8db23 | 2018-08-08 12:28:04 +0200 | [diff] [blame] | 274 | As of go1.11beta3, there has been a significant change for the `go mod` commands. See https://tip.golang.org/cmd/go/#hdr-Module_maintenance as well as two snippets from the [CL](https://go-review.googlesource.com/c/go/+/126655) briefly covering the rationale and the list of new vs. old commands: |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 275 | ``` |
| 276 | The current "go mod" command does too many things. |
| 277 | |
| 278 | It looks like "everything you might want to do with modules" |
| 279 | which causes people to think all module operations go through |
| 280 | "go mod", which is the opposite of the seamless integration we're |
| 281 | going for. In particular too many people think "go mod -require" |
| 282 | and "go get" are the same. |
| 283 | ``` |
| 284 | and: |
| 285 | ``` |
| 286 | split "go mod" into multiple subcommands: |
| 287 | |
| 288 | go mod edit # old go mod -require ... |
| 289 | go mod fix # old go mod -fix |
| 290 | go mod graph # old go mod -graph |
| 291 | go mod init # old go mod -init |
| 292 | go mod tidy # old go mod -sync |
| 293 | go mod vendor # old go mod -vendor |
| 294 | go mod verify # old go mod -verify |
| 295 | |
| 296 | Splitting out the individual commands makes both the docs |
| 297 | and the implementations dramatically easier to read. |
| 298 | It simplifies the command lines |
| 299 | (go mod -init -module m is now 'go mod init m') |
| 300 | and allows command-specific flags. |
| 301 | ``` |
| 302 | |
thepudds | 1da29f6 | 2018-08-05 11:19:52 -0400 | [diff] [blame] | 303 | ### My project has historically made breaking changes without bumping the major version. What are some strategies to consider moving forward? |
| 304 | |
thepudds | 162ccfe | 2018-08-05 11:27:29 -0400 | [diff] [blame] | 305 | In response to a question *"k8s does minor releases but changes the Go API in each minor release. How would people handle importing k8s as a vendored project via vgo?"*, Russ Cox made the following [comment](https://github.com/kubernetes/kubernetes/pull/65683#issuecomment-403705882): |
thepudds | 1da29f6 | 2018-08-05 11:19:52 -0400 | [diff] [blame] | 306 | |
| 307 | > I don't fully understand the k8s dev cycle etc, but I think generally the k8s team needs to decide/confirm what they intend to guarantee to users about stability and then apply version numbers accordingly to express that. |
| 308 | > |
| 309 | > * To make a promise about API compatibility (which seems like the best user experience!) then start doing that and use 1.X.Y. |
| 310 | > * To have the flexibility to make backwards-incompatible changes in every release but allow different parts of a large program to upgrade their code on different schedules, meaning different parts can use different major versions of the API in one program, then use X.Y.0, along with import paths like k8s.io/client/vX/foo. |
| 311 | > * To make no promises about API compatible and also require every build to have only one copy of the k8s libraries no matter what, with the implied forcing of all parts of a build to use the same version even if not all of them are ready for it, then use 0.X.Y. |
| 312 | |
thepudds | 162ccfe | 2018-08-05 11:27:29 -0400 | [diff] [blame] | 313 | On a related note, Kubernetes has some atypical build approaches (currently including custom wrapper scripts on top of godep), and hence Kubernetes is an imperfect example for many other projects, but it will likely be an interesting example as [Kubernetes moves towards adopting Go 1.11 modules](https://github.com/kubernetes/kubernetes/pull/64731#issuecomment-407345841). |
| 314 | |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 315 | ### Additional frequently asked questions |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 316 | |
| 317 | * Please see the earlier [FAQ from the official proposal discussion](https://github.com/golang/go/issues/24301#issuecomment-371228664), including answers to common question such as: |
| 318 | * "Won't minimal version selection keep developers from getting important updates?" |
| 319 | * "Why are major versions v0, v1 omitted from import paths?" |
| 320 | * "Why must major version numbers appear in import paths?" |