お布団宇宙ねこ

にゃーん

Yesodに入門しようとして一番始めにyesod-binを入れて失敗した話

Yesod に入門をしようとして 公式サイト の手順を無視したら失敗した話です。

何をしたのか

Yesod を使うために必要なパッケージをインストールするところから始めました。

$ stack install yesod-bin cabal-install --install-ghc
...

Copied executables to /Users/hoge/.local/bin:
- cabal
- yesod
- yesod-ar-wrapper
- yesod-ghc-wrapper
- yesod-ld-wrapper

yesod コマンドを使えるように .local/bin のパスを通しておきます。

.zshrc

export PATH="$PATH:$HOME/.local/bin"

あとはプロジェクトを作成してビルドしました。ビルドが完了したら stack exec -- yesod devel でサーバを起動することができるはずでした。

$ stack new hello-yesod yesod-sqlite
$ cd hello-yesod
$ stack build
$ stack exec -- yesod devel
Yesod devel server. Type 'quit' to quit
Application can be accessed at:

...

Resolving dependencies...
Configuring hello-yesod-0.0.0...
ERROR: Yesod has been compiled with a different GHC version, please reinstall yesod-bin
yesod: ExitFailure 1

失敗しました(´・_・`)

なぜ失敗したのか

ブログタイトルにも書きましたが、 stack install yesod-bin を一番始めにやったのがよくなかったようです。

stack exec -- yesod devel を実行したときに出たエラーメッセージを見てみると

ERROR: Yesod has been compiled with a different GHC version, please reinstall yesod-bin

と書かれています。すでに yesod-bin がプロジェクトで使用されている GHC とは違うバージョンでビルドされていて、それが原因のようです。

実は stack install yesod-bin を実行したログの最初のほうで、グローバルプロジェクトの stack.yaml が読み込まれていて、そこに書かれた resolver でビルドされていたようです。

Run from outside a project, using implicit global project config
Using latest snapshot resolver: lts-7.0
Writing implicit global project config file to: /Users/hoge/.stack/global-project/stack.yaml

確認してみると確かにバージョンが違いました。

~/.stack/global-project/stack.yaml

...
resolver: lts-7.0

hello-yesod/stack.yaml

...
resolver: lts-6.18
...

解決策

再ビルドしてみる

再インストールするのは時間がかかるので、とりあえずプロジェクトのディレクトリまで移動してから再ビルドしてみます。

$ ls .stack/snapshots/x86_64-osx/lts-7.0/8.0.1/installed-packages
.                      ..                     cabal-install-1.24.0.0 yesod-bin-1.4.18.3

$ stack build yesod-bin-1.4.18.3

ビルドが終わったところで再度サーバを起動してみます。

$ stack exec -- yesod devel
Yesod devel server. Type 'quit' to quit
Application can be accessed at:

...

Resolving dependencies...
Configuring hello-yesod-0.0.0...
Forcing recompile for ./Model.hs because of config/models

...

Rebuilding application... (using cabal)
Starting development server...
<command line>: cannot satisfy -package-key main
    (use -v for more information)
Exit code: ExitFailure 1

ダメでした(´・_・`)

インストールし直す

仕方ないので一度インストールしたパッケージたちを消してから、公式サイト の手順に沿ってインストールし直します。

(念のため cabel も一緒に消しておきます)

$ rm -rf ~/.stack/snapshots/x86_64-osx/lts-7.0
$ cd ~/.local/bin
$ rm cabel
$ rm yesod
$ rm yesod-*

先にプロジェクトを作成してから yesod-bin をインストールします。

$ stack new hello-yesod yesod-sqlite
$ cd hello-yesod
$ stack build yesod-bin cabal-install --install-ghc
$ stack build

ビルドが終わったらサーバを起動してみます。

$ stack exec -- yesod devel
Yesod devel server. Type 'quit' to quit

...

Rebuilding application... (using cabal)
Starting development server...
Starting devel application
Migrating: CREATE TABLE "user"("id" INTEGER PRIMARY KEY,"ident" VARCHAR NOT NULL,"password" VARCHAR NULL,CONSTRAINT "unique_user" UNIQUE ("ident"))

...

先ほどとは違い DB のマイグレーションまで実行されました。そして localhost:3000 を開いてみるとようやく Scaffold されたサイトを見ることができました。

まとめ

公式サイトの手順には基本従いましょう(´・_・`)

参考文献