1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

背景

バックエンドに CI を整備したが・・なんと、CI でしかチェックしない人が出現した

ってことで、VSCode の共有設定(.vscode/)に Ruff の自動実行を仕込んだ。

概要

  • .vscode/settings.jsonPython 保存時の Ruff 自動実行を設定(format + lint fix + import 整理)
  • .vscode/extensions.jsonRuff 拡張を「推奨拡張」として登録して、チームに配布
  • 設計方針として、ローカルで動かさない人がいても品質は守られるようにした(CI 側が本丸なので)

とはいえ・・・・

推奨拡張はあくまで「おすすめ通知」なので、拡張を入れてない人の手元では、自動実行は一切起きないんですけどね :crying_cat_face:

保存時に Ruff を自動実行する設定

VSCode はワークスペース直下の .vscode/settings.json をリポジトリにコミットすると、その設定をチーム全員で共有できる。ここに Python 用の設定を入れた。

.vscode/settings.json
{
  "[python]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.codeActionsOnSave": {
      "source.fixAll.ruff": "explicit",
      "source.organizeImports.ruff": "explicit"
    }
  }
}

やってることは3つ。

  • editor.formatOnSave: true … 保存時に自動整形
  • editor.defaultFormatter: charliermarsh.ruff … 整形に Ruff を使う
  • editor.codeActionsOnSave … 保存時に Ruff の自動修正(fixAll)と import 並べ替え(organizeImports)を実行

これで、Python ファイルを保存するたびに「整形 → lint 自動修正 → import 整理」が一気にかかる。CI でやってることが、手元で保存した瞬間に終わってる状態。

"explicit"
codeActionsOnSave の値。"explicit" は「Ctrl+S などで明示的に保存したときだけ実行する」という意味。自動保存(afterDelay)では走らないので、保存のたびに勝手に import が動いて驚く、みたいなことが起きにくい。

言語ごとに formatter を分けてる
このプロジェクトはフロントエンドが TypeScript なので、[typescript] などには Biome を、[python] には Ruff を、と言語ブロックごとに defaultFormatter を割り当ててる。混在リポジトリでも衝突しない。

推奨拡張としてチームに配る

上の設定は charliermarsh.ruff(Ruff の VSCode 拡張)が入ってる前提。なので、その拡張を**「推奨拡張」としてリポジトリに登録**した。

.vscode/extensions.json
{
  "recommendations": [
    // Biome: Frontend / JSON formatter + linter
    "biomejs.biome",
    // Ruff: Python linter + formatter(保存時に自動 format/lint)
    "charliermarsh.ruff"
  ]
}

.vscode/extensions.json に書いておくと、そのリポジトリを開いた人に VSCode が「おすすめ拡張あるよ、入れる?」と通知してくれる。チームで拡張を揃えるための仕組み。

インストール済みかどうかは、コマンドパレットの「Extensions: Show Recommended Extensions」で確認できる。推奨拡張は VSCode の拡張ビューに「ワークスペースの推奨事項」として一覧表示される。

あとがき

推奨拡張は、あくまで「おすすめ」の通知が出るだけ。強制インストールじゃない。

なので、
ぶっちゃけ・・・ 誰も入れてなかった :laughing:

嘘のようなホントの話。

え? mention してましたよね?って思ったけど・・・ :sweat_smile:

とはいえ、CI gate で引っかかった人からは相談が来るので、それで教えて広めていきましたとさ

CI Gate を required にしておいて、ほんとよかった :sweat:

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?