Skip to content

Commit e3a496a

Browse files
authored
Merge pull request #1451 from future-architect/feature
go modules tool
2 parents 74883a5 + 73c1140 commit e3a496a

6 files changed

+207
-1
lines changed

source/_posts/20250127a_Go_1.24連載始まります&os.Root、WASMの最新のまとめ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ lede: "恒例のGo新リリース記念連載が始まります。今回の1.24
2626
| 1/30 | [templateの新文法(イテレータ)](/articles/20250130a/) | 大江さん |
2727
| 1/31 | testing/synctest + 生成AI | 棚井さん |
2828
| 2/3 | [strings関数 + encoding.TextAppender](/articles/20250203a/) | 市川燿さん |
29-
| 2/4 | ツール管理回りの進化 | 辻さん |
29+
| 2/4 | [ツール管理回りの進化](/articles/20250203a/) | 辻さん |
3030

3131
この記事では概要と`os.Root`、GoのWASM出力の現在地について紹介します。
3232

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
---
2+
title: "Go 1.24リリース連載 Go Modulesにおけるツール管理の進化"
3+
date: 2025/02/04 00:00:00
4+
postid: a
5+
tag:
6+
- Go
7+
- Go1.24
8+
- GoModules
9+
category:
10+
- Programming
11+
thumbnail: /images/20250204a/thumbnail.png
12+
author: 辻大志郎
13+
lede: "Go1.24 リリース連携の7本目です。Go Modules におけるツール管理の進化について取り上げます。"
14+
---
15+
16+
<img src="/images/20250204a/go1.24.png" alt="" width="800" height="484">
17+
18+
# はじめに
19+
20+
[Go1.24 リリース連携](/articles/20250127a/)の7本目です。
21+
22+
Go Modules におけるツール管理の進化について取り上げます。
23+
24+
# アップデートサマリ
25+
26+
* Go Modules で go.mod に `tool` ディレクティブが追加になった
27+
* `go get -tool``tool` ディレクティブに指定されたパッケージを追加できる
28+
* `go get tool` でツールのアップデートや、`go install tool` でツールのインストール、`go tool hoge` でツールの実行ができる、など
29+
* インストールした実行ファイルは、Goのビルドキャッシュにキャッシュされる
30+
31+
# アップデートのモチベーションなど([#48429](https://github.com/golang/go/issues/48429)
32+
33+
## 歴史的経緯
34+
35+
Goでアプリケーションを実装するときに、Goのツールを使って開発することはよくあります。Goのツールの例として、たとえばや `fmt.Stringer` インターフェースを満たすコードを生成する [Stringer](https://pkg.go.dev/golang.org/x/tools/cmd/stringer) や SQL から Go のコードを生成できる [sqlc](https://pkg.go.dev/github.com/sqlc-dev/sqlc) などが挙げられるでしょう。
36+
37+
チーム開発するうえでツールのバージョン管理は重要です。チーム内で同じバージョンのツールを使って開発できることが望ましく、別バージョンを利用していると開発者間で不要な差分や、動作しないソースがリポジトリにコミットされてしまう可能性などが考えられます。
38+
39+
Go1.24の本機能がリリースされる以前は、主に以下の方法でツールのバージョン管理が行われていました。このあたりのノウハウは Go Wiki の [How can I track tool dependencies for a module?](https://go.dev/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module) にまとまっています。
40+
41+
1. `tools.go` を利用する方法
42+
2. `go install` でバージョンを指定する方法
43+
44+
### tools.go を利用する方法
45+
46+
`go install` でツールのバイナリをインストールできましたが、Go 1.15 以前ではインストール時にバージョン指定ができませんでした。インストール時に go.mod にバージョンが追記されますが、どこからも import されていない場合 `go mod tidy` で依存関係が削除されてしまいます。そこで `Build Constraints` を利用するテクニックが知られています。ブランクインポートにより import されているが、`Build Constraints` でコンパイル/ビルドはされないようにしています。`Build Constraints` を利用する Go のファイル例の記載例は以下のようなものです。ファイル名は慣習として `tools.go` とすることが多いでしょう。
47+
48+
```go tools.go
49+
// +build tools
50+
51+
package tools
52+
53+
import (
54+
_ "golang.org/x/tools/cmd/stringer"
55+
)
56+
```
57+
58+
### go install でバージョンを指定する方法
59+
60+
Go 1.16 からは `go install` でインストール時にバージョン指定できるようになりました。このリリースにより go.mod の依存関係に影響を与えず、ツールをインストールできるようになりました。このあたりの話は、過去のGo1.16のリリース連携記事([Go 1.16のgo installについて](https://future-architect.github.io/articles/20210209/))でも触れています。
61+
62+
余談ですが、私たちのチームでは `go install` でバージョン指定してインストールするコマンドを Makefile などにまとめて記載することがよくあります。
63+
64+
```Makefile Makefile
65+
.PHONY: install
66+
67+
install:
68+
go install golang.org/x/tools/cmd/[email protected]
69+
go install github.com/sqlc-dev/sqlc/cmd/[email protected]
70+
```
71+
72+
## 提案のモチベーション
73+
74+
`tools.go` `go install` を利用したプラクティスは Go の開発者にはよく知られています。ただ Go Modules によるバージョン管理のサポートとしては比較的弱い、Go で書かれたツール周りの開発者体験を向上させたい、というのが [#48429](https://github.com/golang/go/issues/48429) や [Proposal:Adding tool dependencies to go.mod](https://go.googlesource.com/proposal/+/54d6775ff71ccbc00c276db2a4e4841d67011cf4/design/48429-go-tool-modules.md) で述べられています。
75+
76+
# 試してみた
77+
78+
導入された新機能のうち、ツールの追加、バージョンの更新、ツールの実行のそれぞれを試してみます。なお、Goバージョンは `go 1.24rc2` です。
79+
80+
空の `go.mod` に `github.com/sqlc-dev/sqlc/cmd/[email protected]` をツールとして追加します。
81+
82+
```hcl go.mod
83+
module sample
84+
85+
go 1.24rc2
86+
```
87+
88+
`go get -tool github.com/sqlc-dev/sqlc/cmd/[email protected]` します。
89+
90+
```sh
91+
> go get -tool github.com/sqlc-dev/sqlc/cmd/[email protected]
92+
go: downloading github.com/sqlc-dev/sqlc v1.27.0
93+
go: downloading github.com/google/cel-go v0.21.0
94+
go: downloading github.com/jackc/pgx/v5 v5.6.0
95+
go: downloading golang.org/x/sync v0.8.0
96+
go: downloading google.golang.org/grpc v1.65.0
97+
go: downloading google.golang.org/protobuf v1.34.2
98+
99+
・・・(略)
100+
101+
go: added modernc.org/libc v1.55.3
102+
go: added modernc.org/mathutil v1.6.0
103+
go: added modernc.org/memory v1.8.0
104+
go: added modernc.org/sqlite v1.31.1
105+
go: added modernc.org/strutil v1.2.0
106+
go: added modernc.org/token v1.1.0
107+
```
108+
109+
コマンドを実行すると `go.mod``tool` ディレクティブに `github.com/sqlc-dev/sqlc/cmd/sqlc` が追加されており、バージョンが `require` で管理されていることがわかります。バージョンは `v1.27.0` が入っていますね。
110+
111+
```hcl go.mod
112+
module sample
113+
114+
go 1.24rc2
115+
116+
tool github.com/sqlc-dev/sqlc/cmd/sqlc
117+
118+
require (
119+
filippo.io/edwards25519 v1.1.0 // indirect
120+
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
121+
github.com/cubicdaiya/gonp v1.0.4 // indirect
122+
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
123+
・・・(略)・・・
124+
125+
github.com/sqlc-dev/sqlc v1.27.0 // indirect
126+
127+
・・・(略)・・・
128+
modernc.org/sqlite v1.31.1 // indirect
129+
modernc.org/strutil v1.2.0 // indirect
130+
modernc.org/token v1.1.0 // indirect
131+
)
132+
```
133+
134+
また `go get tool` でツールのバージョンを最新化(`v1.27.0``v1.28.0`)してみます。
135+
136+
```sh
137+
> go get tool
138+
go: upgraded cel.dev/expr v0.15.0 => v0.18.0
139+
go: upgraded github.com/google/cel-go v0.21.0 => v0.22.1
140+
go: upgraded github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a => v0.0.0-20240606120523-5a60cdf6a761
141+
142+
・・・(略)・・・
143+
go: upgraded google.golang.org/protobuf v1.34.2 => v1.36.3
144+
go: upgraded modernc.org/sqlite v1.31.1 => v1.34.5
145+
```
146+
147+
`go.mod` を確認すると、たしかにバージョンが2025/01/23現在の最新である `v.1.28.0` に更新されていることがわかります。
148+
149+
```hcl go.mod
150+
module sample
151+
152+
go 1.24rc2
153+
154+
tool github.com/sqlc-dev/sqlc/cmd/sqlc
155+
156+
require (
157+
cel.dev/expr v0.18.0 // indirect
158+
filippo.io/edwards25519 v1.1.0 // indirect
159+
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
160+
github.com/cubicdaiya/gonp v1.0.4 // indirect
161+
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
162+
・・・(略)・・・
163+
164+
github.com/sqlc-dev/sqlc v1.28.0 // indirect
165+
166+
・・・(略)・・・
167+
modernc.org/sqlite v1.34.5 // indirect
168+
modernc.org/strutil v1.2.0 // indirect
169+
modernc.org/token v1.1.0 // indirect
170+
)
171+
```
172+
173+
また `go install tool``$GOBIN` (デフォルトで `$GOPATH/bin`) にバイナリがインストールされていることも確認できました。これで go.mod で管理しているツールをまとめてインストールできます。
174+
175+
ツールは `go tool sqlc` として実行できました。なお `$GOBIN` へのパスが通っていれば、`go tool` コマンドなしで `sqlc` としても実行できます。
176+
177+
```sh
178+
> go tool sqlc
179+
Usage:
180+
sqlc [command]
181+
182+
Available Commands:
183+
compile Statically check SQL for syntax and type errors
184+
completion Generate the autocompletion script for the specified shell
185+
createdb Create an ephemeral database
186+
diff Compare the generated files to the existing files
187+
generate Generate source code from SQL
188+
help Help about any command
189+
init Create an empty sqlc.yaml settings file
190+
push Push the schema, queries, and configuration for this project
191+
verify Verify schema, queries, and configuration for this project
192+
version Print the sqlc version number
193+
vet Vet examines queries
194+
195+
Flags:
196+
-f, --file string specify an alternate config file (default: sqlc.yaml)
197+
-h, --help help for sqlc
198+
--no-remote disable remote execution (default: false)
199+
--remote enable remote execution (default: false)
200+
201+
Use "sqlc [command] --help" for more information about a command.
202+
```
203+
204+
# まとめ
205+
206+
`go.mod``tool` ディレクティブの追加により、ツールのバージョン管理がシンプルにできるようになりました。Makefile などによるインストールスクリプトや `tools.go` のテクニックが不要になる日も近いかもしれません。

source/images/20250203a/go1.24.png:Zone.Identifier

Whitespace-only changes.

source/images/20250203a/thumbnail.png:Zone.Identifier

Whitespace-only changes.

source/images/20250204a/go1.24.png

25 KB
Loading
7.97 KB
Loading

0 commit comments

Comments
 (0)