[インデックス 16113] ファイルの概要
このコミットは、Go言語のコマンドラインツール go
のZshシェル補完スクリプト (misc/zsh/go
) を更新するものです。新しいコマンドや既存のコマンドに追加されたフラグに対する補完機能が追加され、ユーザーがZsh環境でGoコマンドをより効率的に利用できるようになります。
コミット
commit 44c4a0d1c495809c881ed3105f32d54a99a31741
Author: Jonathan Rudenberg <jonathan@titanous.com>
Date: Sun Apr 7 04:11:12 2013 +0800
misc: update zsh completion
R=golang-dev, minux.ma
CC=golang-dev
https://golang.org/cl/8437043
GitHub上でのコミットページへのリンク
https://github.com/golang/go/commit/44c4a0d1c495809c881ed3105f32d54a99a31741
元コミット内容
misc: update zsh completion
R=golang-dev, minux.ma
CC=golang-dev
https://golang.org/cl/8437043
変更の背景
このコミットは、Go言語のツールチェインが進化し、新しいコマンド(例: go env
)や既存のコマンド(go build
, go test
)に新しいフラグが追加されたことに伴い、Zshシェルでのコマンド補完機能がそれらの変更に追従する必要があったために行われました。Go言語の開発は活発であり、新しい機能やオプションが頻繁に導入されます。ユーザーエクスペリエンスを向上させるためには、これらの新しい機能がシェル環境で適切に補完されることが重要です。特に、go env
コマンドはGoの環境変数を表示する重要なツールであり、go build -race
はデータ競合検出のための画期的な機能でした。これらの機能が導入された時期に、それらの補完機能も追加されることで、開発者はよりスムーズに新しいツールを利用できるようになります。
前提知識の解説
Zsh (Z Shell)
Zshは、Bourne Shell (sh) をベースにしたUnixシェルであり、BashやKornShell (ksh) と同様に、コマンドラインインターフェースを提供します。Zshは、強力な補完機能、テーマ、プラグインシステムなど、豊富なカスタマイズオプションと高度なインタラクティブ機能で知られています。特に、その補完システムは非常に柔軟で、複雑なコマンドライン引数やファイルパスの補完をサポートします。
シェル補完 (Shell Completion)
シェル補完とは、ユーザーがコマンドの一部を入力した際に、シェルが残りの部分を自動的に補完する機能です。これにより、入力の手間が省け、タイプミスを減らし、利用可能なオプションをユーザーに提示することができます。Zshの補完スクリプトは、特定のコマンドに対してどのような引数やフラグが有効であるかをシェルに教える役割を果たします。
Goコマンド (go
tool)
go
コマンドは、Go言語のソースコードのビルド、テスト、パッケージ管理、ドキュメント生成など、Go開発における主要なツールです。go build
、go test
、go get
、go run
など、多くのサブコマンドを持ちます。
go env
コマンド
go env
コマンドは、Goの環境変数(GOPATH, GOROOT, GOOS, GOARCHなど)の値を表示するために使用されます。これは、Goのビルド環境や実行環境をデバッグする際に非常に役立ちます。このコマンドはGo 1.1で導入されました。
go build -race
フラグ
go build -race
フラグは、Go 1.1で導入されたデータ競合検出機能(Race Detector)を有効にしてプログラムをビルドします。データ競合は並行プログラミングにおける一般的なバグであり、複数のゴルーチンが同時に共有データにアクセスし、少なくとも1つのアクセスが書き込みである場合に発生します。Race Detectorは、実行時にこれらの競合を検出し、デバッグを支援します。
go test
のベンチマーク関連フラグ
-benchmem
: ベンチマーク実行時にメモリ割り当て統計(割り当てバイト数と割り当て回数)を表示します。Go 1.1で導入されました。-blockprofile
: ゴルーチンのブロッキングプロファイル(ブロッキング操作に費やされた時間)をファイルに書き出します。Go 1.1で導入されました。-blockprofilerate
: ブロッキングプロファイルのサンプリングレートを設定します。Go 1.1で導入されました。
これらのフラグは、Goプログラムのパフォーマンス最適化や並行処理のデバッグにおいて重要な情報を提供します。
技術的詳細
このコミットは、misc/zsh/go
ファイル、すなわちGoコマンドのZsh補完スクリプトを直接編集しています。Zshの補完スクリプトは、通常、_arguments
や_values
といったZshの組み込み関数を使用して、コマンドの引数やオプション、その説明を定義します。
具体的には、以下の変更が行われています。
-
go env
コマンドの追加:__go_tool_complete
関数内のコマンドリストに'env[print Go environment information]'
が追加されました。これにより、go
と入力した後にTabキーを押すと、env
が補完候補として表示され、その説明も表示されるようになります。 -
go build
コマンドの新しいフラグの追加:build_flags
配列に以下のフラグが追加されました。-race[enable data race detection]
-ccflags[flags for 5c/6c/8c]:flags
-compiler[name of compiler to use]:name
-installsuffix[suffix to add to package directory]:suffix
-tags[list of build tags to consider satisfied]:tags
これらのフラグは、Go 1.1で導入されたビルドオプションに対応しており、Zshの補完機能を通じてこれらのオプションが利用可能になります。特に-race
は、Goのデータ競合検出機能の導入と密接に関連しています。
-
go test
コマンドの新しいフラグの追加:test
サブコマンドの引数定義に以下のフラグが追加されました。-benchmem[print memory allocation stats]
-blockprofile[write goroutine blocking profile to file]:file
-blockprofilerate[set sampling rate of goroutine blocking profile]:number
これらは、Go 1.1で導入されたテストおよびプロファイリング関連のフラグであり、ベンチマークのメモリ使用量分析やゴルーチンのブロッキングプロファイリングに役立ちます。
-
go help
のトピック名の変更:help
サブコマンドの補完候補において、'importpath[description of import paths]'
が'packages[description of package lists]'
に変更されました。これは、Goのドキュメントや概念における「import path」から「packages」への用語の変更を反映している可能性があります。
これらの変更は、Goツールチェインの機能拡張にZsh補完が追従するための典型的なメンテナンス作業です。
コアとなるコードの変更箇所
--- a/misc/zsh/go
+++ b/misc/zsh/go
@@ -20,6 +20,7 @@ __go_tool_complete() {
'build[compile packages and dependencies]'
'clean[remove object files]'
'doc[run godoc on package sources]'
+ 'env[print Go environment information]'
'fix[run go tool fix on packages]'
'fmt[run gofmt on package sources]'
'get[download and install packages and dependencies]'
@@ -40,12 +41,17 @@ __go_tool_complete() {
build_flags=(
'-a[force reinstallation of packages that are already up-to-date]'
'-n[print the commands but do not run them]'
- "-p[number of parallel builds]:number"
+ '-p[number of parallel builds]:number'
+ '-race[enable data race detection]'
'-x[print the commands]'
- "-work[print temporary directory name and keep it]"
- "-gcflags[flags for 5g/6g/8g]:flags"
- "-ldflags[flags for 5l/6l/8l]:flags"
- "-gccgoflags[flags for gccgo]:flags"
+ '-work[print temporary directory name and keep it]'
++ '-ccflags[flags for 5c/6c/8c]:flags'
+ '-gcflags[flags for 5g/6g/8g]:flags'
+ '-ldflags[flags for 5l/6l/8l]:flags'
+ '-gccgoflags[flags for gccgo]:flags'
+ '-compiler[name of compiler to use]:name'
+ '-installsuffix[suffix to add to package directory]:suffix'
+ '-tags[list of build tags to consider satisfied]:tags'
)
__go_list() {
local expl importpaths
@@ -63,7 +69,7 @@ __go_tool_complete() {
install)
_arguments -s -w : ${build_flags[@]} \
"-v[show package names]" \
- '*:importpaths:__go_list'
+ '*:importpaths:__go_list'
;;
get)
_arguments -s -w : \
@@ -88,7 +94,10 @@ __go_tool_complete() {
"-cpu[values of GOMAXPROCS to use]:number list" \
"-run[run tests and examples matching regexp]:regexp" \
"-bench[run benchmarks matching regexp]:regexp" \
+ "-benchmem[print memory allocation stats]" \
"-benchtime[run each benchmark until taking this long]:duration" \
+ "-blockprofile[write goroutine blocking profile to file]:file" \
+ "-blockprofilerate[set sampling rate of goroutine blocking profile]:number" \
"-timeout[kill test after that duration]:duration" \
"-cpuprofile[write CPU profile to file]:file:_files" \
"-memprofile[write heap profile to file]:file:_files" \
@@ -98,7 +107,7 @@ __go_tool_complete() {
help)
_values "${commands[@]}" \
'gopath[GOPATH environment variable]' \
- 'importpath[description of import paths]' \
+ 'packages[description of package lists]' \
'remote[remote import path syntax]' \
'testflag[description of testing flags]' \
'testfunc[description of testing functions]'
コアとなるコードの解説
このZsh補完スクリプトは、__go_tool_complete
という関数を定義しており、これがgo
コマンドの補完ロジックをカプセル化しています。
-
__go_tool_complete()
関数: この関数は、go
コマンドのサブコマンド(build
,clean
,doc
など)のリストを定義しています。今回の変更では、このリストに'env[print Go environment information]'
が追加されました。これにより、go env
コマンドがZshの補完対象となります。 -
build_flags
配列: この配列は、go build
コマンドに渡すことができるフラグとその説明を定義しています。追加された各エントリは、'-flagname[description]:type'
の形式に従っています。-race
: データ競合検出を有効にするフラグ。-ccflags
,-compiler
,-installsuffix
,-tags
: ビルドプロセスをより細かく制御するための新しいフラグ。これらはGo 1.1で導入されたビルド機能に対応しています。
-
install)
およびget)
ケース:install
サブコマンドの補完定義において、以前は'*:importpaths:__go_list'
と記述されていた部分が、'*:importpaths:__go_list'
と変更されています。これは、行頭のタブ文字がスペースに置き換えられただけの、機能的には影響のないフォーマットの修正です。 -
test)
ケース:test
サブコマンドの補完定義に、ベンチマークやプロファイリングに関連する新しいフラグが追加されました。-benchmem
: ベンチマークのメモリ統計を表示。-blockprofile
,-blockprofilerate
: ゴルーチンのブロッキングプロファイリング関連。 これらは、Go 1.1で導入されたテストおよびプロファイリング機能に対応しています。
-
help)
ケース:help
サブコマンドの補完候補リストにおいて、'importpath[description of import paths]'
が'packages[description of package lists]'
に修正されました。これは、go help importpath
がgo help packages
に変わったことを反映しています。
全体として、このコミットはGoツールチェインの進化に合わせて、Zshユーザーが最新のGoコマンドとフラグを効率的に利用できるようにするための、補完スクリプトの同期更新です。
関連リンク
- Go言語公式ウェブサイト: https://golang.org/
- Go 1.1 Release Notes: https://golang.org/doc/go1.1 (特に
go env
, Race Detector,go test
の新しいフラグに関する記述) - Zsh公式ウェブサイト: https://www.zsh.org/
参考にした情報源リンク
- Go 1.1 Release Notes: https://golang.org/doc/go1.1
- Go Race Detector: https://go.dev/doc/articles/race_detector
- Zsh Completion System: https://zsh.sourceforge.io/Doc/Release/Completion-System.html
- Go CL 8437043: https://golang.org/cl/8437043 (元のGerritレビューページ)
go env
command: https://pkg.go.dev/cmd/go#hdr-Go_environment_variablesgo build
command: https://pkg.go.dev/cmd/go#hdr-Build_commandsgo test
command: https://pkg.go.dev/cmd/go#hdr-Test_packages