Slide 1

Slide 1 text

How to manage tool dependencies golang.tokyo #20 - @izumi5210

Slide 2

Slide 2 text

@izumin5210 Application Engineer, Wantedly People Wantedly, Inc.

Slide 3

Slide 3 text

tool dependencies ? Executable tools that installed
 via `go get` e.g.mockgen, protoc-gen-grpc-gateway, ...

Slide 4

Slide 4 text

Why should we manage versions of tools?

Slide 5

Slide 5 text

Why should we manage versions of tools? ⬇ Behaviors of tools may change when developers install them via `go get` (e.g. an output file format of code generator)

Slide 6

Slide 6 text

[email protected] [email protected] [email protected] mockgen@bd3c8e8 [email protected] [email protected] mockgen@ddf6e33 Why should we manage versions of tools? ⬇ Behaviors of tools may change when developers install them via `go get` (e.g. an output file format of code generator)

Slide 7

Slide 7 text

Mock implementation that I wanna create Mocks that updated unexpectedly Because I use [email protected] but they are generated by [email protected] If you do not manage tool dependencies ...

Slide 8

Slide 8 text

How about existing package managers? (e.g. dep ,go mod) `go build` is the out of scope of their responsibility dep document says* if you need the tool to be installed you still run the following (manually or from a `Makefile`) after each `dep ensure`
 ```
 cd vendor/pkg/to/install
 go install .
 ``` * https://golang.github.io/dep/docs/Gopkg.toml.html#required

Slide 9

Slide 9 text

How about existing package managers? (e.g. dep ,go mod) # Makefile DEP_COMMANDS := \ vendor/github.com/golang/mock/mockgen .PHONY: dep dep: Gopkg.toml Gopkg.lock @dep ensure -v -vendor-only pkgs="$(DEP_COMMANDS)"; \ for pkg in $$pkgs; do \ cd $$pkg; \ GOBIN="$(shell pwd)/bin" go install .; \ cd -; \ done # Gopkg.toml required = [ "github.com/golang/mock/mockgen", ]

Slide 10

Slide 10 text

https://github.com/golang/go/issues/25922 cmd/go: clarify best practice for tool dependencies #25922 · golang/go

Slide 11

Slide 11 text

cmd/go: clarify best practice for tool dependencies #25922 · golang/go // tools.go // +build tools package tools import ( _ "golang.org/x/tools/cmd/stringer" ) // go.mod module example.com/hello require golang.org/x/tools v0.0.0-20180813205110-a434f64ace81 // indirect go get export GOBIN=$(pwd)/bin go install golang.org/x/tools/cmd/stringer

Slide 12

Slide 12 text

cmd/go: clarify best practice for tool dependencies #25922 · golang/go Other 3rd-party tools (e.g. virtualgo, retool, ...) need to
 introduce new management mechanisms "tools.go" method can use existing dependency management mechanisms (e.g. dep, go mod)

Slide 13

Slide 13 text

cmd/go: clarify best practice for tool dependencies #25922 · golang/go // tools.go // +build tools package tools import ( _ "golang.org/x/tools/cmd/stringer" ) // go.mod module example.com/hello require golang.org/x/tools v0.0.0-20180813205110-a434f64ace81 // indirect go get export GOBIN=$(pwd)/bin go install golang.org/x/tools/cmd/stringer Best practice?

Slide 14

Slide 14 text

cmd/go: clarify best practice for tool dependencies #25922 · golang/go // tools.go // +build tools package tools import ( _ "golang.org/x/tools/cmd/stringer" ) // go.mod module example.com/hello require golang.org/x/tools v0.0.0-20180813205110-a434f64ace81 // indirect go get export GOBIN=$(pwd)/bin go install golang.org/x/tools/cmd/stringer We want to also automate this step!

Slide 15

Slide 15 text

gex: manage tools with `tools.go` github.com/izumin5210/gex $ gex --add github.com/golang/mock/mockgen $ cat tools.go // Code generated by github.com/izumin5210/gex. DO NOT EDIT. // +build tools package tools // tool dependencies import ( _ "github.com/golang/mock/mockgen" ) $ cat go.mod | grep mock github.com/golang/mock v1.1.1 // indirect $ gex mockgen # prints mockgen's help text...

Slide 16

Slide 16 text

gex: manage tools with `tools.go` github.com/izumin5210/gex $ gex --add github.com/golang/mock/mockgen $ cat tools.go // Code generated by github.com/izumin5210/gex. DO NOT EDIT. // +build tools package tools // tool dependencies import ( _ "github.com/golang/mock/mockgen" ) $ cat go.mod | grep mock github.com/golang/mock v1.1.1 // indirect $ gex mockgen # prints mockgen's help text... Install mockgen via gex - gex manages dependencies version using `dep` or `go mod` - So `gex --add ...` runs `go get ...` or `dep ensure -add ...` internally - Build mockgen and output to `$PWD/bin`

Slide 17

Slide 17 text

gex: manage tools with `tools.go` github.com/izumin5210/gex $ gex --add github.com/golang/mock/mockgen $ cat tools.go // Code generated by github.com/izumin5210/gex. DO NOT EDIT. // +build tools package tools // tool dependencies import ( _ "github.com/golang/mock/mockgen" ) $ cat go.mod | grep mock github.com/golang/mock v1.1.1 // indirect $ gex mockgen # prints mockgen's help text... `tools.go` will generate by `gex`

Slide 18

Slide 18 text

gex: manage tools with `tools.go` github.com/izumin5210/gex $ gex --add github.com/golang/mock/mockgen $ cat tools.go // Code generated by github.com/izumin5210/gex. DO NOT EDIT. // +build tools package tools // tool dependencies import ( _ "github.com/golang/mock/mockgen" ) $ cat go.mod | grep mock github.com/golang/mock v1.1.1 // indirect $ gex mockgen # prints mockgen's help text... `go.mod` will modify because `gex` run `go get`

Slide 19

Slide 19 text

gex: manage tools with `tools.go` github.com/izumin5210/gex $ gex --add github.com/golang/mock/mockgen $ cat tools.go // Code generated by github.com/izumin5210/gex. DO NOT EDIT. // +build tools package tools // tool dependencies import ( _ "github.com/golang/mock/mockgen" ) $ cat go.mod | grep mock github.com/golang/mock v1.1.1 // indirect $ gex mockgen # prints mockgen's help text... Run versioned mockgen - Execute `$PWD/bin/mockgen` - If `$PWD/bin/mockgen` does not exist, run `go build -o mockgen ...` automatically

Slide 20

Slide 20 text

gex: manage tools with `tools.go` github.com/izumin5210/gex Does not introduce new mechanism to manage tool deps. - `gex` is very thin wrapper command of `dep` or `go mod` - For example, if `go mod` was to support to manage tools officially,
 we would be able to move from `gex` easily Only 2 commands that you use: `--add` and `--build` (I believe) tools management mechanism will be introduced
 into `golang/go` officially - When the time coms, we graduate from`gex`

Slide 21

Slide 21 text

How to manage tool dependencies golang.tokyo #20 - @izumi5210 Let's manage tools with `gex` efficiently, and happy hacking with Go!

Slide 22

Slide 22 text

https://www.wantedly.com/projects/268180