Techioz Blog

Github CI/CD 作成時の Rails 7 ESbuild エラー

概要

非常に基本的な CI/CD を作成しました。初めてで未経験です。

これは私の ci.yml のコピーです

name: Tests

on:
  pull_request:
    branches:
      - "*"
  push:
    branches:
      - "main"

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
        ports: ["5432:5432"]

    steps:
      - uses: actions/checkout@v4
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
      - name: Run Tests
        env:
          DATABASE_URL: postgres://postgres:password@localhost:5432/test
          RAILS_ENV: test
        run: |
          bin/rails test:prepare
          bin/rails db:test:prepare
          bin/rails test

これは問題なく動作しているように見えますが、このプロセスのどこかで次のエラーがスローされます。

Run bin/rails test:prepare
yarn install v1.22.21
[1/4] Resolving packages...
[2/4] Fetching packages...
warning [email protected]: The engine "pnpm" appears to be invalid.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 9.18s.
yarn run v1.22.21
$ esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets
Error: R] Could not resolve "stimulus-clipboard"

    app/javascript/application.js:8:22:
      8 │ import Clipboard from 'stimulus-clipboard'
        ╵                       ~~~~~~~~~~~~~~~~~~~~

  You can mark the path "stimulus-clipboard" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle.

この問題を調査するには、ESbuild にこの「スティミュラス クリップボード」を通知する必要があります。 esbuild.config.js を作成してコードを追加しようとしましたが、うまくいきませんでした。 「rails stimulus:manifest:update」も実行してみましたが、どちらも成功しませんでした。

このエラーを解決し、パッケージを除外するか適切に含めることで CI を使用しようとしています。

解決策

これに対する答えは、「stimulus-clipboard」が正しくインストールされていないことがわかりました。

プロジェクトのルート ディレクトリにあるyarn.lock ファイルを表示して、これを確認しました。そのファイルで「クリップボード」を検索したところ、結果が 0 件あり、存在しないことが確認されました。 「yarn add stimulus-clipboard」を使用すると、正しく追加され、再構築されました。

これは、今後同様の問題が発生した場合の一般的なトラブルシューティング プロセスとなるはずです。