LoginSignup
2

More than 3 years have passed since last update.

「.swiftlint.yml」のルールが機能しなくハマったこと

Last updated at Posted at 2020-02-22

.swiftlint.ymlにline_lengthのルールを記載したのに反映されないことがありハマりました。
AppDelegateなどでコメントのwarningがでていたのでLintに以下のルールを追加しましたが、反映されませんでした。

.swiftlint.yml
line_length:
  warning: 300
  ignores_comments: true

SwiftLintのバージョンが古いとか、RULEに半角スペースが3つあるとかでも反映されないような現象があるみたいですが、
私の場合は違いました。

試したこと

  • Homebrewをupgradeしてswiftlintの入れ直し。
  • YAMLが正しく書いているか確認。
  • swiftlintのupgrade / swiftlintのuninstall

結論

.swiftlint.ymlxxx.xcodeprojと同じ階層にないと反映されない。
私の場合、githubからcloneしてきたので以下のディレクトリの構成になっていました。

before
SampleProject
    ├── SampleProject
    │   ├── SampleProject/...
    │   ├── SampleProject.xcodeproj
    │   └── SampleProjectTests
    ├── .swiftlint.yml
    ├── README.md
    ├── .gitignore
    └── .git

以下のようにルートディレクトリにxxx.xcode.proj.swiftlint.ymlを同じ階層に置けばルールが適応されました。

after
SampleProject
    ├── SampleProject/...
    ├── SampleProject.xcodeproj
    ├── SampleProjectTests
    ├── .swiftlint.yml
    ├── README.md
    ├── .gitignore
    └── .git

cloneしてからプロジェクトファイルを作るとプロジェクト名のディレクトリがダブるので、
プロジェクトファイルを作成してから、git clone http://xxxxxx.com/xxxxxx.git .をした方がよさそうと思った。
最後の.はリポジトリと同名のディレクトリを作らないためにつける。

参考

How to use swiftlint command and Xcode Script when .swiftlint.yml file not in the same folder with project.xcodeproj? · Issue #2673 · realm/SwiftLint
https://github.com/realm/SwiftLint/issues/2673

git clone でディレクトリを作らない | 株式会社Orfool
https://orfool.com/programing/2206/

SwiftLintを試してみた - Qiita
https://qiita.com/ushisantoasobu/items/b494c9cf7d78a968b373

SwiftLintのdisabled_rules一覧(実例付き) - Qiita
https://qiita.com/akatsuki174/items/6da27f7ccdd0572f8927

SwiftLintの運用ノウハウ - Qiita
https://qiita.com/shtnkgm/items/6dd756aa14926736c6f5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2