Skip to content

fix(ci): fail when yarn.lock does not match package.json - #67

Merged
shin4488 merged 1 commit into
mainfrom
fix/ci-frozen-lockfile
Oct 24, 2026
Merged

shin4488 merged 1 commit into
mainfrom
fix/ci-frozen-lockfile

Conversation

@shin4488

@shin4488 shin4488 commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

CI の依存のインストールで、yarn.lock が package.json と合っていないときにジョブが失敗するようにしました。

変更

  • ui-test.yml・lint-format.yml の依存のインストールを yarn install --frozen-lockfile だけにしました。
    • ui-test の yarn install --immutable は、Yarn 1 が知らないフラグとして無視し、yarn.lock を書き換えて成功していました。
    • lint-and-format は、--frozen-lockfile が失敗すると yarn install --immutable に切り替わり、同じく成功していました。
  • README の Development Setup と AGENTS.md に、依存の管理に Yarn 1 を使うことと、依存を変えたら yarn.lock も一緒にコミットすること(合っていないと CI が失敗すること)を書きました。

例として、package.json の react-ga4 を ^3.0.1 から ^3.0.0 に変え、yarn.lock を変えずに push した場合の結果です(GitHub Actions で確かめた結果。下の「検証」を参照)。

ジョブ 変更前 変更後
ui-test 成功(yarn.lock を書き換えてテストまで実行) Install dependencies で失敗
lint-and-format 成功(--frozen-lockfile が失敗した後、--immutable で書き換えて続行) Install dependencies で失敗

失敗時の出力は error Your lockfile needs to be updated, but yarn was run with `--frozen-lockfile`. です。

Yarn の版

各環境で使われる Yarn を確かめ、すべて 1.22.22 でした。packageManager は AGENTS.md の方針どおり追加していません。npm の yarn パッケージの latest は 1.22.22 で、Corepack が選ぶ版もこれになります。

環境 Yarn を決めている仕組み 確かめた版
CI corepack enable の後、package.json に packageManager がないため Corepack の既定の Yarn 1 を使う 1.22.22(ジョブのログの yarn install v1.22.22)
開発コンテナ node:24-bookworm イメージに入っている Yarn(/usr/local/bin/yarn → /opt/yarn-v1.22.22)。Corepack は有効にしていない 1.22.22
ホスト(README の手順) Corepack の既定の Yarn 1 1.22.22(Node 24.20.0、Corepack 0.35.0)

開発コンテナ(docker-compose.yml)と README のホストの手順は、もともと yarn install --frozen-lockfile で、CI と同じコマンドになりました。

変更した仕様・設計

CI の依存のインストールの流れを次のように変えました。

flowchart TD
    subgraph before["変更前"]
        U1["ui-test: yarn install --immutable"] --> U2["Yarn 1 はフラグを無視し、<br>yarn.lock を書き換えて成功"]
        L1["lint-and-format: yarn install --frozen-lockfile"] -->|合っている| L3["成功"]
        L1 -->|yarn.lock の更新が必要| L2["yarn install --immutable で<br>yarn.lock を書き換えて成功"]
    end
    subgraph after["変更後(両ジョブ)"]
        A1["yarn install --frozen-lockfile"] -->|合っている| A2["lint・format・test に進む"]
        A1 -->|yarn.lock の更新が必要| A3["ジョブが失敗"]
    end
Loading

Yarn 1 の --frozen-lockfile は、package.json から依存を消して yarn.lock に項目が残っている場合は失敗しません(下の表の「依存を削除」)。このときインストールされるパッケージは package.json のとおりで、残った項目は使われません。

検証

lockfile がずれた状態(ローカル)

開発コンテナ(Yarn 1.22.22)で、package.json と yarn.lock の写しを作り、package.json だけを変えて各コマンドを実行しました。値は終了コードと、実行後の yarn.lock が元と同じかどうかです。

package.json の状態 変更前の ui-test 変更前の lint-and-format 変更後(両ジョブ)
変更なし 0・同じ 0・同じ 0・同じ
範囲を変更(react-ga4 を ^3.0.0) 0・書き換え 0・書き換え 1・同じ
依存を追加(left-pad を 1.3.0) 0・書き換え 0・書き換え 1・同じ
依存を削除(react-ga4) 0・書き換え 0・同じ 0・同じ

lockfile がずれた状態(GitHub Actions)

この PR とは別の一時ブランチに、上の「範囲を変更」のコミットと、そのブランチへの push で CI が動くようにトリガーを足したコミットを push しました。確認後に一時ブランチは削除しました。

一時ブランチの基点 ui-test lint-and-format
main(変更前) 成功 成功
この PR のコミット(変更後) Install dependencies で失敗 Install dependencies で失敗

そのほか

  • 開発コンテナで yarn typecheck・yarn lint・yarn format・yarn test(26 件):成功
  • この PR の CI(ui-test・lint-and-format):yarn install --frozen-lockfile で依存を入れ、成功
  • .prettierignore で .github と README.md は Prettier の対象外のため、ワークフローは YAML として読み込み、各ジョブのコマンドが yarn install --frozen-lockfile になっていることを確かめました。
  • 変更した文書のリンク(README.md#development-setup)を確かめました。
  • yarn build は実行していません。アプリのコードとビルドの設定は変えていないためです。

🤖 Generated with Claude Code

Both workflows install with Yarn 1, which Corepack selects because package.json has no packageManager field. Yarn 1 ignores the unknown --immutable flag, so ui-test rewrote yarn.lock and passed. lint-and-format fell back to the same command when --frozen-lockfile failed, so it passed as well.

Install with `yarn install --frozen-lockfile` alone in both workflows, the same command the dev container uses. Document in the README and AGENTS.md that the project uses Yarn 1 and that yarn.lock must be committed with package.json changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shin4488
shin4488 merged commit f82f05c into main Oct 24, 2026
5 checks passed
@shin4488
shin4488 deleted the fix/ci-frozen-lockfile branch October 24, 2026 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant