Skip to content

Commit 99a06e9

Browse files
ConradIrwinmatloob
authored andcommitted
_content/doc: document module tools
For golang/go#48429 Change-Id: I81344da002afe92c8ec60ad5acef0b11e487934e Reviewed-on: https://go-review.googlesource.com/c/website/+/632595 Reviewed-by: Sam Thanawalla <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent a379667 commit 99a06e9

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed

_content/doc/modules/gomod-ref.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,53 @@ For more about managing dependencies, see the following:
345345
* [Upgrading or downgrading a dependency](/doc/modules/managing-dependencies#upgrading)
346346
* [Synchronizing your code's dependencies](/doc/modules/managing-dependencies#synchronizing)
347347
348+
## tool {#tool}
349+
350+
Adds a package as a dependency of the current module, and makes it available to run with `go tool` when the current working directory is within this module.
351+
352+
### Syntax {#tool-syntax}
353+
354+
<pre>tool <var>package-path</var></pre>
355+
356+
<dl>
357+
<dt>package-path</dt>
358+
<dd>The tool's package path, a concatenation of the module containing
359+
the tool and the (possibly empty) path to the package implementing
360+
the tool within the module.</dd>
361+
</dl>
362+
363+
### Examples {#tool-examples}
364+
365+
* Declaring a tool implemented in the current module:
366+
```
367+
module example.com/mymodule
368+
369+
tool example.com/mymodule/cmd/mytool
370+
```
371+
* Declaring a tool implemented in a separate module:
372+
```
373+
module example.com/mymodule
374+
375+
tool example.com/atool/cmd/atool
376+
377+
require example.com/atool v1.2.3
378+
```
379+
380+
### Notes {#tool-notes}
381+
382+
You can use `go tool` to run tools declared in your module by fully qualified package path
383+
or, if there is no ambiguity, by the last path segment. In the first example
384+
above you could run `go tool mytool` or `go tool example.com/mymodule/cmd/mytool`.
385+
386+
In workspace mode, you can use `go tool` to run a tool declared in any workspace module.
387+
388+
Tools are built using the same module graph as the module itself. A [`require`
389+
directive](#require) is needed to select the version of the module that
390+
implements the tool. Any [`replace` directives](#replace), or [`exclude`
391+
directives](#exclude) also apply to the tool and its dependencies.
392+
393+
For more information see [Tool dependencies](/doc/modules/managing-dependencies#tools).
394+
348395
## replace {#replace}
349396
350397
Replaces the content of a module at a specific version (or all versions) with

_content/doc/modules/managing-dependencies.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,66 @@ $ go get example.com/theirmodule@none
477477
The `go get` command will also downgrade or remove other dependencies that
478478
depend on the removed module.
479479
480+
## Tool dependencies {#tools}
481+
482+
Tool dependencies let you manage developer tools that are written in Go and used
483+
when working on your module. For example, you might use
484+
[`stringer`](https://pkg.go.dev/golang.org/x/tools/cmd/stringer) with [`go
485+
generate`](/blog/generate), or a specific linter or formatter as part of
486+
preparing your change for submission.
487+
488+
In Go 1.24 and above, you can add a tool dependency with:
489+
490+
```
491+
$ go get -tool golang.org/x/tools/cmd/stringer
492+
```
493+
494+
This will add a [`tool` directive](/ref/mod/#go-mod-file-tool) to your `go.mod` file, and ensure the
495+
necessary require directives are present. Once this directive is added you can
496+
run the tool by passing the last fragment of the package path to `go tool`:
497+
498+
```
499+
$ go tool stringer
500+
```
501+
502+
In the case that multiple tools share the last path fragment, or the path fragment
503+
matches one of the tools shipped with the Go distribution, you must pass the full
504+
package path instead:
505+
506+
```
507+
$ go tool golang.org/x/tools/cmd/stringer
508+
```
509+
510+
To see a list of all tools currently available, run `go tool` with no arguments:
511+
512+
```
513+
$ go tool
514+
```
515+
516+
You can manually add a `tool` directive to your `go.mod`, but you must ensure
517+
that there is a `require` directive for the module that defines the tool. The
518+
easiest way to add any missing `require` directives is to run:
519+
520+
```
521+
$ go mod tidy
522+
```
523+
524+
Requirements needed to satisfy tool dependencies behave like any other
525+
requirements in your [module graph](/ref/mod#glos-module-graph). They
526+
participate in [minimal version selection](/ref/mod#minimal-version-selection)
527+
and respect `require`, `replace` and `exclude` directives. Due to module
528+
pruning, when you depend on a module that itself has a tool dependency,
529+
requirements that exist to just to satisfy that tool dependency do not usually
530+
become requirements of your module.
531+
532+
The `tool` [meta-pattern](/cmd/go#hdr-Package_lists_and_patterns) provides a way to perform operations on all tools simultaneously. For example you can upgrade all tools with `go get -u tool`, or install them all to $GOBIN with `go install tool`.
533+
534+
In Go versions before 1.24, you can acheive something similar to a `tool`
535+
directive by adding a blank import to a go file within the module that is
536+
excluded from the build using [build
537+
constraints](/pkg/go/build/#hdr-Build_Constraints). If you do this, you can then
538+
use `go run` with the full package path to run the tool.
539+
480540
## Specifying a module proxy server {#proxy_server}
481541
482542
When you use Go tools to work with modules, the tools by default download

_content/ref/mod.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ for details on EBNF syntax.
471471
GoMod = { Directive } .
472472
Directive = ModuleDirective |
473473
GoDirective |
474+
ToolDirective |
474475
RequireDirective |
475476
ExcludeDirective |
476477
ReplaceDirective |
@@ -715,6 +716,35 @@ require (
715716
)
716717
```
717718

719+
### `tool` directive {#go-mod-file-tool}
720+
721+
A `tool` directive adds a package as a dependency of the current module. It also
722+
makes it available to run with `go tool` when the current working directory is
723+
within this module, or within a workspace that contains this module.
724+
725+
If the tool package is not in the current module, a `require`
726+
directive must be present that specifies the version of the tool to use.
727+
728+
The `tool` meta-pattern resolves to the list of tools defined in the current module's
729+
`go.mod`, or in workspace mode to the union of all tools defined in all modules in the
730+
workspace.
731+
732+
```
733+
ToolDirective = "tool" ( ToolSpec | "(" newline { ToolSpec } ")" newline ) .
734+
ToolSpec = ModulePath newline .
735+
```
736+
737+
Example:
738+
739+
```
740+
tool golang.org/x/tools/cmd/stringer
741+
742+
tool (
743+
example.com/module/cmd/a
744+
example.com/module/cmd/b
745+
)
746+
```
747+
718748
### `exclude` directive {#go-mod-file-exclude}
719749

720750
An `exclude` directive prevents a module version from being loaded by the `go`
@@ -2079,6 +2109,10 @@ The editing flags specify a sequence of editing operations.
20792109
flag cannot add a rationale comment for the `retract` directive. Rationale
20802110
comments are recommended and may be shown by `go list -m -u` and other
20812111
commands.
2112+
* The `-tool=path` and `-droptool=path` flags add and drop a `tool` directive
2113+
for the given paths. Note that this will not add necessary dependencies to
2114+
the build graph. Users should prefer `go get -tool path` to add a tool, or
2115+
`go get -tool path@none` to remove one.
20822116

20832117
The editing flags may be repeated. The changes are applied in the order given.
20842118

@@ -2129,6 +2163,10 @@ type Retract struct {
21292163
High string
21302164
Rationale string
21312165
}
2166+
2167+
type Tool struct {
2168+
Path string
2169+
}
21322170
```
21332171

21342172
Note that this only describes the `go.mod` file itself, not other modules
@@ -2250,7 +2288,7 @@ The `-v` flag causes `go mod tidy` to print information about removed modules
22502288
to standard error.
22512289

22522290
`go mod tidy` works by loading all of the packages in the [main
2253-
module](#glos-main-module) and all of the packages they import,
2291+
module](#glos-main-module), all of its tools, and all of the packages they import,
22542292
recursively. This includes packages imported by tests (including tests in other
22552293
modules). `go mod tidy` acts as if all build tags are enabled, so it will
22562294
consider platform-specific source files and files that require custom build

0 commit comments

Comments
 (0)