github.com/golang/dep@v0.5.4/docs/Gopkg.toml.md (about)

     1  ---
     2  title: Gopkg.toml
     3  ---
     4  
     5  The `Gopkg.toml` file is initially generated by `dep init`, and is primarily hand-edited. It contains several types of rule declarations that govern dep's behavior:
     6  
     7  * _Dependency rules:_ [`constraints`](#constraint) and [`overrides`](#override) allow the user to specify which versions of dependencies are acceptable, and where they should be retrieved from.
     8  * _Package graph rules:_ [`required`](#required) and [`ignored`](#ignored) allow the user to manipulate the import graph by including or excluding import paths, respectively.
     9  * [`metadata`](#metadata) are a user-defined maps of key-value pairs that dep will ignore. They provide a data sidecar for tools building on top of dep.
    10  * [`prune`](#prune) settings determine what files and directories can be deemed unnecessary, and thus automatically removed from `vendor/`.
    11  * [`noverify`](#noverify) is a list of project roots for which [vendor verification](glossary.md#vendor-verification) is skipped.
    12  
    13  Note that because TOML does not adhere to a tree structure, the `required` and `ignored` fields must be declared before any `[[constraint]]` or `[[override]]`.
    14  
    15  There is a full [example](#example) `Gopkg.toml` file at the bottom of this document. `dep init` will also, by default, generate a `Gopkg.toml` containing some example values, for guidance.
    16  
    17  ## Dependency rules: `[[constraint]]` and `[[override]]`
    18  
    19  Most of the rule declarations in a `Gopkg.toml` will be either `[[constraint]]` or `[[override]]` stanzas. Both of these types of stanzas allow exactly the same types of values, but dep interprets them differently. Each allows the following values:
    20  
    21  * `name` - the import path corresponding to the [source root](glossary.md#source-root) of a dependency (generally: where the VCS root is)
    22  * At most one [version rule](#version-rules)
    23  * An optional [`source` rule](#source)
    24  * [`metadata`](#metadata) that is specific to the `name`'d project
    25  
    26  A full example (invalid, actually, as it has more than one version rule, for illustrative purposes) of either one of these stanzas looks like this:
    27  
    28  ```toml
    29  [[constraint]]
    30    # Required: the root import path of the project being constrained.
    31    name = "github.com/user/project"
    32    # Recommended: the version constraint to enforce for the project.
    33    # Note that only one of "branch", "version" or "revision" can be specified.
    34    version = "1.0.0"
    35    branch = "master"
    36    revision = "abc123"
    37  
    38    # Optional: an alternate location (URL or import path) for the project's source.
    39    source = "https://github.com/myfork/package.git"
    40  
    41    # Optional: metadata about the constraint or override that could be used by other independent systems
    42    [metadata]
    43    key1 = "value that convey data to other systems"
    44    system1-data = "value that is used by a system"
    45    system2-data = "value that is used by another system"
    46  ```
    47  
    48  ### `[[constraint]]`
    49  
    50  A `[[constraint]]` stanza defines rules for how a [direct dependency](glossary.md#direct-dependency) must be incorporated into the dependency graph. Dep respects these declarations from the current project's `Gopkg.toml`, as well as the `Gopkg.toml` files found in any dependencies.
    51  
    52  **Use this for:** having a [direct dependency](FAQ.md#what-is-a-direct-or-transitive-dependency) use a specific branch, version range, revision, or alternate source (such as a fork).
    53  
    54  ### `[[override]]`
    55  
    56  An `[[override]]` stanza differs from a `[[constraint]]` in that it applies to all dependencies, [direct](glossary.md#direct-dependency) and [transitive](glossary.md#transitive-dependency), and supersedes all other `[[constraint]]` declarations for that project. However, only overrides from the current project's `Gopkg.toml` are incorporated.
    57  
    58  **Use this for:** Overrides are primarily intended as a way of eliminating disagreements between multiple irreconcilable `[[constraint]]` declarations on a single dependency. However, they will also be your primary recourse if you need to [constrain a transitive dependency's version?](FAQ.md#how-do-i-constrain-a-transitive-dependencys-version)
    59  
    60  Overrides should be used cautiously and temporarily, when possible.
    61  
    62  ### `source`
    63  
    64  A `source` rule can specify an alternate location from which the `name`'d project should be retrieved. It is primarily useful for temporarily specifying a fork for a repository.
    65  
    66  `source` rules are generally brittle and should only be used when there is no other recourse. Using them to try to circumvent network reachability issues is typically an antipattern.
    67  
    68  ### Version rules
    69  
    70  Version rules can be used in either `[[constraint]]` or `[[override]]` stanzas. There are three types of version rules - `version`, `branch`, and `revision`. At most one of the three types can be specified.
    71  
    72  #### `version`
    73  
    74  `version` is a property of `constraint`s and `override`s. It is used to specify version constraint of a specific dependency. It can be used to target an arbitrary VCS tag, or a semantic version, or a range of semantic versions.
    75  
    76  Specifying semantic version ranges can be done using the following operators:
    77  
    78  * `=`: equal
    79  * `!=`: not equal
    80  * `>`: greater than
    81  * `<`: less than
    82  * `>=`: greater than or equal to
    83  * `<=`: less than or equal to
    84  * `-`: literal range. E.g., 1.2 - 1.4.5 is equivalent to >= 1.2, <= 1.4.5
    85  * `~`: minor range. E.g., ~1.2.3 is equivalent to >= 1.2.3, < 1.3.0
    86  * `^`: major range. E.g., ^1.2.3 is equivalent to >= 1.2.3, < 2.0.0
    87  * `[xX*]`: wildcard. E.g., 1.2.x is equivalent to >= 1.2.0, < 1.3.0
    88  
    89  You might, for example, include a rule that specifies `version = "=2.0.0"` to pin a dependency to version 2.0.0, or constrain to minor releases with: `version = "~2.1.0"`. Refer to the [semver library](https://github.com/Masterminds/semver) documentation for more info.
    90  
    91  **Note**: When you specify a version _without an operator_, `dep` automatically uses the `^` operator by default. `dep ensure` will interpret the given version as the min-boundary of a range, for example:
    92  
    93  * `1.2.3` becomes the range `>=1.2.3, <2.0.0`
    94  * `0.2.3` becomes the range `>=0.2.3, <0.3.0`
    95  * `0.0.3` becomes the range `>=0.0.3, <0.1.0`
    96  
    97  `~` and `=` operators can be used with the versions. When a version is specified without any operator, `dep` automatically adds a caret operator, `^`. The caret operator pins the left-most non-zero digit in the version. For example:
    98  
    99  ```
   100  ^1.2.3 means 1.2.3 <= X < 2.0.0
   101  ^0.2.3 means 0.2.3 <= X < 0.3.0
   102  ^0.0.3 means 0.0.3 <= X < 0.1.0
   103  ```
   104  
   105  To pin a version of direct dependency in manifest, prefix the version with `=`. For example:
   106  
   107  ```toml
   108  [[constraint]]
   109    name = "github.com/pkg/errors"
   110    version = "=0.8.0"
   111  ```
   112  
   113  #### `branch`
   114  
   115  Using a `branch` constraint will cause dep to use the named branch (e.g., `branch = "master"`) for a particular dependency. The revision at the tip of the branch will be recorded into `Gopkg.lock`, and almost always remain the same until a change is requested, via `dep ensure -update`.
   116  
   117  In general, you should prefer semantic versions to branches, when a project has made them available.
   118  
   119  #### `revision`
   120  
   121  A `revision` is the underlying immutable identifier - like a git commit SHA1. While it is allowed to constrain to a `revision`, doing so is almost always an antipattern.
   122  
   123  Usually, folks are inclined to pin to a revision because they feel it will somehow improve their project's reproducibility. That is not a good reason. `Gopkg.lock` provides reproducibility. Only use `revision` if you have a good reason to believe that _no_ other version of that dependency _could_ work.
   124  
   125  ## Package graph rules: `required` and `ignored`
   126  
   127  As part of normal operation, dep analyzes import statements in Go code. These import statements connect packages together, ultimately forming a graph. The `required` and `ignored` rules manipulate that graph, in ways that are roughly dual to each other: `required` adds import paths to the graph, and `ignored` removes them.
   128  
   129  ### `required`
   130  
   131  `required` lists a set of packages (not projects) that must be included in Gopkg.lock. This list is merged with the set of packages imported by the current project.
   132  
   133  ```toml
   134  required = ["github.com/user/thing/cmd/thing"]
   135  ```
   136  
   137  **Use this for:** linters, generators, and other development tools that
   138  
   139  * Are needed by your project
   140  * Aren't `import`ed by your project, [directly or transitively](FAQ.md#what-is-a-direct-or-transitive-dependency)
   141  * You don't want to put them in your `GOPATH`, and/or you want to lock the version
   142  
   143  Please note that this only pulls in the sources of these dependencies. It does not install or compile them. So, if you need the tool to be installed you should still run the following (manually or from a `Makefile`) after each `dep ensure`:
   144  
   145  ```bash
   146  cd vendor/pkg/to/install
   147  go install .
   148  ```
   149  
   150  This only works reliably if this is the only project to install these executables. This is not enough if you want to be able to run a different version of the same executable depending on the project you're working. In that case you have to use a different `GOBIN` for each project, by doing something like this before running the above commands:
   151  
   152  ```bash
   153  export GOBIN=$PWD/bin
   154  export PATH=$GOBIN:$PATH
   155  ```
   156  
   157  You might also try [virtualgo](https://github.com/GetStream/vg), which installs dependencies in the `required` list automatically in a project specific `GOBIN`.
   158  
   159  ### `ignored`
   160  
   161  `ignored` lists a set of packages (not projects) that are ignored when dep statically analyzes source code. Ignored packages can be in this project, or in a dependency.
   162  
   163  ```toml
   164  ignored = ["github.com/user/project/badpkg"]
   165  ```
   166  
   167  Use `*` to define a package prefix to be ignored. This will cause any lexical wildcard match to be ignored, including the literal string prior to the `*`.
   168  
   169  ```toml
   170  ignored = ["github.com/user/project/badpkg*"]
   171  ```
   172  
   173  **Use this for:** preventing a package, and any of that package's unique dependencies, from being incorporated in `Gopkg.lock`.
   174  
   175  ## `metadata`
   176  
   177  `metadata` can exist at the root as well as under `constraint` and `override` declarations.
   178  
   179  `metadata` declarations are ignored by dep and are meant for usage by other independent systems.
   180  
   181  The root `metadata` declaration defines information about the project itself, while a `metadata` declaration under a `[[constraint]]` or an `[[override]]` defines metadata about that rule, for the `name`d project.
   182  
   183  ```toml
   184  [metadata]
   185  key1 = "value that convey data to other systems"
   186  system1-data = "value that is used by a system"
   187  system2-data = "value that is used by another system"
   188  ```
   189  
   190  ## `prune`
   191  
   192  `prune` defines the global and per-project prune options for dependencies. The options determine which files are discarded when writing the `vendor/` tree.
   193  
   194  The following are the current available options:
   195  
   196  * `unused-packages` indicates that files from directories that do not appear in the package import graph should be pruned.
   197  * `non-go` prunes files that are not used by Go.
   198  * `go-tests` prunes Go test files.
   199  
   200  Out of an abundance of caution, dep non-optionally preserves files that may have legal significance.
   201  
   202  Pruning options are disabled by default. However, generating a `Gopkg.toml` via `dep init` will add lines to enable `go-tests` and `unused-packages` prune options at the root level.
   203  
   204  ```toml
   205  [prune]
   206    go-tests = true
   207    unused-packages = true
   208  ```
   209  
   210  The same prune options can be defined per-project. An additional `name` field is required and, as with `[[constraint]]` and `[[override]]`, should be a [source root](glossary.md#source-root), not just any import path.
   211  
   212  ```toml
   213  [prune]
   214    non-go = true
   215  
   216    [[prune.project]]
   217      name = "github.com/project/name"
   218      go-tests = true
   219      non-go = false
   220  ```
   221  
   222  Almost all projects will be fine without setting any project-specific rules, and enabling the following pruning rules globally:
   223  
   224  ```toml
   225  [prune]
   226    unused-packages = true
   227    go-tests = true
   228  ```
   229  
   230  It is usually safe to set `non-go = true`, as well. However, as dep only has a clear model for the role played by Go files, and non-Go files necessarily fall outside that model, there can be no comparable general definition of safety.
   231  
   232  ## `noverify`
   233  
   234  The `noverify` field is a list of paths, typically [project roots](glossary.md#project-root), to exclude from [vendor verification](glossary.md#vendor-verification).
   235  
   236  Dep uses per-project hash digests, computed after pruning and recorded in [Gopkg.lock](Gopkg.lock.md#digest), to determine if the contents of `vendor/` are as expected. If the recorded digest and the hash of the corresponding tree in `vendor/` differ, that project is considered to be out of sync:
   237  
   238  * `dep ensure` will regenerate it
   239  * `dep check` will complain of a hash mismatch and exit 1
   240  
   241  It is strongly recommended that you leave `vendor/` unmodified, in whatever state dep puts it in. However, this isn't always feasible. If you have no choice but to modify `vendor/` for a particular project, then add the project root for that project to `noverify`. This will have the following effects:
   242  
   243  * `dep ensure` will ignore hash mismatches for the project, and only regenerate it in `vendor/` if absolutely necessary (prune options change, package list changes, version changes)
   244  * `dep check` will continue to report hash mismatches (albeit with an annotation about `noverify`) for the project, but will no longer exit 1.
   245  
   246  `noverify` can also be used to preserve certain excess paths that would otherwise be removed; for example, adding `WORKSPACE` to the `noverify` list would allow you to preserve `vendor/WORKSPACE`, which can help with some Bazel-based workflows.
   247  
   248  ## Scope
   249  
   250  `dep` evaluates
   251  
   252  * `[[override]]`
   253  * `required`
   254  * `ignored`
   255  
   256  only in the root project, i.e. the project where `dep` runs. For example, if you have a project: `github.com/urname/goproject`, and `github.com/foo/bar` is a dependency for your project, then dep will evaluate the `Gopkg.toml` files of these projects as follows:
   257  
   258  | github.com/urname/goproject | github.com/foo/bar |
   259  | --------------------------- | ------------------ |
   260  | [[constraint]] ✔            | [[constraint]] ✔   |
   261  | [[override]] ✔              | [[override]] ✖     |
   262  | required ✔                  | required ✖         |
   263  | ignored ✔                   | ignored ✖          |
   264  
   265  ✔ : Evaluated
   266  ✖ : Not evaluated
   267  
   268  # Example
   269  
   270  Here's a sample `Gopkg.toml` with most elements present.
   271  
   272  ```toml
   273  required = ["github.com/user/thing/cmd/thing"]
   274  
   275  ignored = [
   276    "github.com/user/project/pkgX",
   277    "bitbucket.org/user/project/pkgA/pkgY"
   278  ]
   279  
   280  noverify = ["github.com/something/odd"]
   281  
   282  [metadata]
   283  codename = "foo"
   284  
   285  [prune]
   286    non-go = true
   287  
   288    [[prune.project]]
   289      name = "github.com/project/name"
   290      go-tests = true
   291      non-go = false
   292  
   293  [[constraint]]
   294    name = "github.com/user/project"
   295    version = "1.0.0"
   296  
   297    [constraint.metadata]
   298    property1 = "value1"
   299    property2 = 10
   300  
   301  [[constraint]]
   302    name = "github.com/user/project2"
   303    branch = "dev"
   304    source = "github.com/myfork/project2"
   305  
   306  [[override]]
   307    name = "github.com/x/y"
   308    version = "2.4.0"
   309  
   310    [override.metadata]
   311    propertyX = "valueX"
   312  ```