Stack 自动从 'stack build' 之后的 .cabal 文件中删除依赖项

Stack auto-removes dependency from .cabal file following 'stack build'

我想使用 threepenny-gui 包开发一个新的 Haskell 项目。

我做的第一件事是通过 $ stack new threepennydemo 创建一个堆栈项目。从这里,这里,我做了以下事情:

  1. 我编辑了我的 stack.yaml 文件中的 extra-deps 部分:
# extra-deps: []

extra-deps:
  - threepenny-gui-0.9.0.0
  1. 我从以下位置编辑了 .cabal 文件:
library
  exposed-modules:
      Lib
  other-modules:
      Paths_threepennydemo
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
  default-language: Haskell2010

executable threepennydemo-exe
  main-is: Main.hs
  other-modules:
      Paths_threepennydemo
  hs-source-dirs:
      app
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , threepennydemo
  default-language: Haskell2010

test-suite threepennydemo-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Paths_threepennydemo
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , threepennydemo
  default-language: Haskell2010

library
  exposed-modules:
      Lib
  other-modules:
      Paths_threepennydemo
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
    , threepenny-gui >= 0.9
  default-language: Haskell2010

executable threepennydemo-exe
  main-is: Main.hs
  other-modules:
      Paths_threepennydemo
  hs-source-dirs:
      app
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , threepennydemo
    , threepenny-gui >= 0.9
  default-language: Haskell2010

test-suite threepennydemo-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Paths_threepennydemo
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , threepennydemo
    , threepenny-gui >= 0.9
  default-language: Haskell2010

  1. 我编辑了股票 /app/Main.hs 来自:
module Main where

import Lib

main :: IO ()
main = someFunc

import Graphics.UI.Threepenny
main :: IO ()
main = do
 startGUI defaultConfig showMessage
showMessage :: Window -> UI ()
showMessage window = do
 getBody window #+ [string "Hello, world!"]
 return ()
  1. 我输入命令$ stack build.

从这里,我注意到两件事。首先是我收到以下错误:

Building all executables for `threepennydemo' once. After a successful build of all of them, only specified executables will be rebuilt.
threepennydemo> configure (lib + exe)
Configuring threepennydemo-0.1.0.0...
threepennydemo> build (lib + exe)
Preprocessing library for threepennydemo-0.1.0.0..
Building library for threepennydemo-0.1.0.0..
Preprocessing executable 'threepennydemo-exe' for threepennydemo-0.1.0.0..
Building executable 'threepennydemo-exe' for threepennydemo-0.1.0.0..
[1 of 2] Compiling Main

/Users/my_username/threepennydemo/app/Main.hs:1:1: error:
    Could not load module ‘Graphics.UI.Threepenny’
    It is a member of the hidden package ‘threepenny-gui-0.9.0.0’.
    Perhaps you need to add ‘threepenny-gui’ to the build-depends in your .cabal file.
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
1 | import Graphics.UI.Threepenny
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


--  While building package threepennydemo-0.1.0.0 using:
      /Users/my_username/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.0.1.0_ghc-8.8.4 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.0.1.0 build lib:threepennydemo exe:threepennydemo-exe --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

第二个是我的 .cabal 文件,按照 2. 中提到的进行编辑,自动删除了我对该文件所做的编辑。

为了在设置新的堆栈项目时能够使用第三方库,我缺少什么?

我的困难是由于 stack.yamlpackage.yaml 之间的一些混淆。后者是生成 threepennydemo.cabal 文件的原因。因此,还必须将依赖项指定到 package.yaml 文件。将此依赖项添加到 package.yaml 允许 $ stack build 顺利完成。