プロジェクトルートにある .swift-version ファイルは何か?

はじめに

ルートに .swift-version ファイルがあるプロジェクトを発見することがある。

たとえば、下記のリポジトリ

.swift-version は何のためのファイルか?
わかったことを、ここにまとめる。

1. swiftenv

Swift のバージョン切り替えには swiftenv というツールがある。
これを使用すると .swift-version でプロジェクトの Swift バージョンを指示できる。すなわち、

  • グローバルとプロジェクト、それぞれの Swift のバージョンを分けることができる
  • プロジェクトごとの Swift のバージョンを分けることができる

これは Xcode からビルドする時は特に気にすることはないが、コマンドベースでビルドする時はこの恩恵があるだろう。

ちなみに他の言語だと以下がある。

2. CocoaPods

CocoaPods 1.7.0 Beta! - CocoaPods Blog
上記の CocoaPods アナウンス内の Deprecating .swift-version File の項目が見るべきところ。
簡単な訳と共に見てゆく。

Up until now, most pod authors have been relying on specifying a .swift-version file at the root of their repo in order to successfully publish their pod with the Swift version they officially support. However, this information is never transcribed within the published podspec and therefore during integration none of it is taken into consideration when choosing which Swift version to use.

  • 従来は CocoaPods で配布するライブラリのプロジェクトには .swift-version を入れることで、サポートする Swift バージョンを指示していた。

This can cause numerous issues especially when a new version of Swift is released. This is because a consumer will automatically pick the latest version of Swift to use without the pod author officially claiming that it is supported.

  • しかしこれは、新しい Swift のバージョンがリリースされた時、問題が起きる可能性がある。

We strongly recommend pod authors to migrate over to use the officially supported swift_version DSL within their podspec instead.

  • その代わり、 Swift のバージョンは podspec ファイル内の swift_version で指示することを 強く 勧める。

We also recommend deleting the .swift-version file in your repo unless you use it for other tools such as swiftenv. The swift_version DSL will always take precedence over the .swift-version file.

  • swiftenv などの他のツールで使用しない限り、 swift_version ファイルを削除することを勧める。

Finally, a warning will be displayed during lint that encourages pod authors to migrate away from using the .swift-version file and in a future major release of CocoaPods we plan to completely remove support for it.

  • CocoaPods で配布するライブラリ開発者が .swift-version ファイルを使用をする警告が lint に表示される。 CocoaPods の将来のメジャーリリースでは、サポートは完全に削除する予定である。

まとめ

  • .swift-version はプロジェクトの Swift バージョンを指示するファイルである。
  • 注意点として
    • CocoaPods で配布するライブラリの場合は、 .swift-version は削除した方が良い。
    • 代わりに podspec ファイル内の swift_version でバージョンを指示すること。

個人的にはルートにこのファイルがあると、何かのツールで使うとかでなくても、 使用する Swift バージョンが一目瞭然でわかって、親切な感じがする。