LoginSignup
12
9

More than 5 years have passed since last update.

stack install package しても stack buildでCould not find moduleが出る

Last updated at Posted at 2018-01-18

結論

stack install
したパッケージ名を
./package.yaml

dependencies
に追加する。

package.yaml
.
.
.
description:         Please see the README on Github at <https://github.com/githubuser/qiita#readme>

dependencies:
- base >= 4.7 && < 5
- http-client # 追加

library:
  source-dirs: src
.
.
.

ただし、これが正しいか不明

エラー

大体のコマンド
stack new qiita
cd qiita
stack build
stack ghc
stack exec qiita-exec
// Main.sh編集
stack install http-client
stack build
app/Main.hs
module Main where

import Lib

import Network.HTTP.Client
main = do

/root/haskell/qiita/app/Main.hs:9:1: error:
    Could not find module ‘Network.HTTP.Client’
    Use -v to see a list of the files searched for.
  |
9 | import Network.HTTP.Simple
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^

問題点

import Hogeとパッケージ名が一致していない

これは自分が悪い。
stack install Network
が見つからずに焦るのが第一コケ。
https://hackage.haskell.org/
が慣れてないせいでパッケージ名見つけたり階層登れなかったりするのが第二。

dependenciesらしきもの多すぎ問題

package.yaml

dependencies,executables.dependencies

stack.yaml
packages,extra-deps

プロジェクト名.cabal

library.build-depends
executable.build-depends

追記場所指示らしきもの多すぎ問題

これもresolvercabelを理解せず突っ走っている自分が悪いし!
前後の文脈を読まず目的らしいところのみを読んでいるからなんですが!(よって記事が悪いというわけでもなくてですね…)

本気で Haskell したい人向けの Stack チュートリアル - Qiita #依存関係の追加

base パッケージに含まれていないモジュールを import して使いたいというのは実際の開発においても頻繁に起こります。

今回の場合は先ほどのエラーメッセージから array パッケージをインストールすれば良いことがわかっています。

package.yaml ファイルの librarydependencies を追記します。

package.yaml
library:
  source-dirs: src
  dependencies:
  - array

そして次のコマンドでプロジェクトをビルドするだけで、stack は自動的に array パッケージ及び、その依存関係をインストールしてくれます。

$ stack build

これをすると
Error: While constructing the build plan, the following exceptions were encountered:
array must match -any, but the stack configuration has no specified version
みたいな別のエラーに返歌します。

Haskellのビルドツール"stack"の紹介 - Qiita #プロジェクトをビルドする

どのパッケージを解決するのに失敗したのか、なぜ失敗したのか、そして我々はどうするべきかまでを、懇切丁寧に教えてくれます。stack.yamlのextra-depsへ何を追加したらいいのかがyamlのフォーマットで出力されるので、大体はこれをコピペして貼り付ければビルドが通るようになります。便利すぎる。

Haskell Stack でライブラリを安定させる | Netsphere Laboratories

依存ライブラリの追加

直接、依存するライブラリは hello-world.cabal に追加する。

executable hello-world-exe
  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , hello-world
                       -- ここに追加
                     , persistent
  default-language:    Haskell2010

これをすると手動で編集しやがったな!という警告がでます。あとは先のエラーと同じ

Warning: WARNING: ~/qiita/qiita.cabal was modified manually. Ignoring package.yaml in favor of cabal file. If you want to use package.yaml instead of the
         cabal file, then please delete the cabal file.

Error: While constructing the build plan, the following exceptions were encountered:

In the dependencies for qiita-0.1.0.0:
    array must match -any, but the stack configuration has no specified version

ここらへんを検索ワード変えながらなんども訪れて一人混乱していた。

最終的に

stack/GUIDE.md at master · commercialhaskell/stack#Adding dependencies

を見てそうした。
extra-depsへの追記とかも書いてあるが、まだわからない。


Haskell Platform, cabel, stackが混じったり、
install, dependencies, packageといった単純な単語でうまくググれなかったせいかと。

何度も書きますが一番の原因はとりあえず動かすことを目的にしているからですが。

また触りだしたときに同じつまづきをしそうなので記録です。

どうして自動で追記してくれないのか。
…これはyarnになれすぎなだけで、npmcomposerはオプションが必要だった気もする。
それでもstack install -hにはそれらしいオプションなさそうだし…

12
9
3

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
12
9