[インデックス 15625] ファイルの概要
このコミットは、Go言語の標準ライブラリであるnetパッケージ内のipraw_test.goファイルに対する変更です。ipraw_test.goは、IP生のソケット(IP raw socket)に関するテストコードを定義しているファイルです。IP生のソケットは、TCPやUDPのような上位プロトコルを介さずに、直接IPパケットを送受信するための低レベルなネットワークインターフェースを提供します。このファイルは、netパッケージが様々なオペレーティングシステムで正しく機能することを確認するためのテストスイートの一部です。
コミット
このコミットは、Go言語のnetパッケージにおけるPlan 9ビルドの問題を修正することを目的としています。具体的には、ipraw_test.goファイルから特定のビルドタグを削除することで、Plan 9環境でのコンパイルエラーを解消します。
GitHub上でのコミットページへのリンク
https://github.com/golang/go/commit/3fc244f37065f3eb0bc8cd54e6aff0ced2889969
元コミット内容
net: fix plan9 build
R=golang-dev, akumar
CC=golang-dev
https://golang.org/cl/7564043
変更の背景
Go言語は、クロスプラットフォーム開発を強く意識しており、様々なオペレーティングシステム(Linux, Windows, macOS, FreeBSDなど)をサポートしています。その中には、ベル研究所が開発した分散オペレーティングシステムであるPlan 9も含まれます。
Goのビルドシステムには「ビルドタグ(build tags)」という機能があり、これを使用することで、特定のオペレーティングシステムやアーキテクチャ、あるいはその他の条件に基づいて、ソースファイルのコンパイルを条件付きで行うことができます。ビルドタグは、Goソースファイルの先頭に// +build <expression>(Go 1.17以降は//go:build <expression>)という形式で記述されます。
このコミット以前のsrc/pkg/net/ipraw_test.goファイルには、// +build !plan9というビルドタグが含まれていました。このタグは、「Plan 9以外の環境でのみこのファイルをビルドする」という意味を持ちます。しかし、ipraw_test.goがPlan 9環境でビルドされるべきであるにもかかわらず、このタグが存在していたため、Plan 9環境でのビルド時に問題が発生していました。具体的には、Plan 9環境でnetパッケージのテストをビルドしようとすると、ipraw_test.goが除外されてしまい、依存関係の解決やテストの実行に支障をきたしていたと考えられます。
このコミットは、このビルドタグを削除することで、ipraw_test.goがPlan 9環境でも適切にビルドされるようにし、Plan 9におけるnetパッケージのビルド問題を解決することを目的としています。
前提知識の解説
Go言語のビルドタグ(Build Tags)
Go言語のビルドタグは、条件付きコンパイルを可能にするための強力なメカニズムです。ソースファイルの先頭に特別なコメント行を記述することで、Goツールチェーンは、特定の条件が満たされた場合にのみそのファイルをコンパイルに含めるかどうかを決定します。
- 構文:
- Go 1.17以前:
// +build <expression> - Go 1.17以降:
//go:build <expression>(両方の構文が互換性のためにサポートされています)
- Go 1.17以前:
- 配置: ビルドタグは、
package宣言の前に、ファイルの先頭に記述され、その後に空行が続く必要があります。 - 式の種類:
- OS名:
linux,windows,darwin,plan9など。 - アーキテクチャ名:
amd64,arm,386など。 - カスタムタグ: 任意の文字列を定義し、
go build -tags <tag_name>で指定できます。
- OS名:
- 論理演算子:
&&(AND): 例:// +build linux && amd64(LinuxかつAMD64の場合)||(OR): 例:// +build linux || darwin(LinuxまたはmacOSの場合)!(NOT): 例:// +build !windows(Windows以外の場合)
このコミットで問題となっていたのは、// +build !plan9というタグです。これは「Plan 9ではない環境でビルドする」という意味になります。
Plan 9 from Bell Labs
Plan 9 from Bell Labsは、ベル研究所の計算機科学研究センターで開発された分散オペレーティングシステムです。Unixの設計思想をさらに推し進め、すべてのリソース(ファイル、デバイス、ネットワーク接続など)をファイルシステムとして表現し、それらをネットワーク越しに透過的にアクセスできる「Everything is a file」という哲学を特徴としています。Go言語の設計者の一部は、Plan 9の開発にも携わっており、Go言語の設計にはPlan 9の思想が色濃く反映されています。
技術的詳細
src/pkg/net/ipraw_test.goファイルに存在していた// +build !plan9というビルドタグは、Goツールチェーンに対して、このファイルがPlan 9環境でビルドされる際にはコンパイル対象から除外されるべきであることを指示していました。
しかし、ipraw_test.goはnetパッケージのテストの一部であり、Plan 9を含むすべてのサポート対象プラットフォームでテストが実行されることが望ましいです。Plan 9環境でこのファイルが除外されると、netパッケージのテストスイートが不完全になり、Plan 9固有のバグが見過ごされる可能性がありました。
このコミットでは、この// +build !plan9という行を削除することで、ipraw_test.goがPlan 9環境でも他のプラットフォームと同様にコンパイルされ、テストスイートに含まれるように変更されました。これにより、Plan 9環境でのnetパッケージのビルドが正常に行われるようになり、テストカバレッジも維持されることになります。
この変更は、Go言語のクロスプラットフォームサポートの堅牢性を高めるための、小さなしかし重要な修正です。特定のプラットフォームで意図せずファイルが除外されることを防ぎ、一貫したビルドとテストのプロセスを保証します。
コアとなるコードの変更箇所
--- a/src/pkg/net/ipraw_test.go
+++ b/src/pkg/net/ipraw_test.go
@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.\n
\n-// +build !plan9
-\n package net
\n import (
コアとなるコードの解説
変更は非常にシンプルで、src/pkg/net/ipraw_test.goファイルの2行が削除されています。
// +build !plan9- その直後の空行
この// +build !plan9という行が、このファイルがPlan 9環境でビルドされる際にコンパイル対象から除外される原因となっていました。この行を削除することで、Goツールチェーンはもはやこのファイルに対してPlan 9固有の除外ルールを適用しなくなります。結果として、ipraw_test.goはPlan 9環境でも他のGoソースファイルと同様に扱われ、コンパイルおよびテストの対象に含まれるようになります。
この修正により、Plan 9環境でのnetパッケージのビルドが成功し、テストが適切に実行されるようになります。
関連リンク
- Go言語のビルドタグに関する公式ドキュメント: https://pkg.go.dev/cmd/go#hdr-Build_constraints
- Plan 9 from Bell Labs: https://9p.io/plan9/
参考にした情報源リンク
- Go build tags are a mechanism in the Go programming language that allow developers to include or exclude specific source files from a build based on various conditions, such as the target operating system or architecture. This feature is particularly relevant for "Plan 9 from Bell Labs" as Go provides experimental support for this operating system. (go.dev)
- Purpose Build tags enable conditional compilation, allowing the Go toolchain to select which files to compile into the final executable. This is crucial for writing platform-specific code, including code tailored for Plan 9. (digitalocean.com)
- Syntax For Go versions 1.17 and later, the standard syntax for build tags is
//go:build <expression>. Prior to Go 1.17, the syntax was// +build <expression>. Both syntaxes are still supported for backward compatibility. Build tags must be placed at the very top of a.gofile, preceding thepackagedeclaration, and must be followed by a blank newline. (leapcell.io, medium.com) - Plan 9 Specific Tag To target Plan 9, you would typically use the
plan9operating system tag. For example, a file intended only for Plan 9 would start with//go:build plan9. (cheney.net) - File Suffixes In addition to explicit build tags, Go also recognizes special file suffixes for conditional compilation. A file named
mypkg_plan9.go, for instance, would automatically be included only when building for the Plan 9 operating system. While both methods achieve conditional compilation, file suffixes are often simpler for exact platform matches, whereas build tags offer more flexibility for complex conditions or when a file applies to multiple platforms. (cheney.net) - Boolean Logic Build tags can be combined using logical operators (
&&for AND,||for OR,!for NOT) to create more complex conditions. For example,//go:build plan9 && amd64would include a file only when building for Plan 9 on an AMD64 architecture. (digitalocean.com, medium.com) - The Go project itself has historical ties to Plan 9, with some of its core designers having worked on Plan 9 at Bell Labs, and elements like Go's assembler and early compiler bootstrapping being influenced by Plan 9's toolchain. (go.dev, youtube.com)