Why is `stack build` altering my .cabal file?

1.3k views Asked by At

I am attempting to build a project which uses Euterpea.

Running stack build I get the following error, suggesting that I need to add Euterpea to the build-depends section of my .cabal file.

$ sb
composition-0.1.0.0: build (lib + exe)
Preprocessing library composition-0.1.0.0...
[2 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-linux-nix/Cabal-1.24.2.0/build/Lib.o )

/home/matthew/backup/composition/composition/src/Lib.hs:5:1: error:
    Failed to load interface for ‘Euterpea’
    It is a member of the hidden package ‘Euterpea-2.0.4’.
    Perhaps you need to add ‘Euterpea’ to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.

--  While building package composition-0.1.0.0 using:
      /home/matthew/.stack/setup-exe-cache/x86_64-linux-nix/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-linux-nix/Cabal-1.24.2.0 build lib:composition exe:composition-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

I add Euterpea there, and the library section of my .cabal file then is the following.

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

However, when I then run stack build again, it gives the same error -- and changes my .cabal file back to what it was originally, with the library section then looking like

library
  hs-source-dirs:
      src
  build-depends:
      base >= 4.7 && < 5
  exposed-modules:
      Lib
  other-modules:
      Paths_composition
  default-language: Haskell2010

Why is stack build altering my cabal file? I have never seen that occurring before.


Side note: Not sure if it is related, but the .cabal file's format appears to be different than it normally does. Here as with previous projects I auto-initialized by running stack new <project-name>. I don't know what I might have done different from previous projects to cause this unexpected behavior of stack build.

2

There are 2 answers

1
YAMAMOTO Yuji On BEST ANSWER

Make sure package.yaml exists in the root of your project directory.

package.yaml is a new file format to improve the syntax of cabal, converted by hpack.

Stack supports hpack as strongly as the stack build command automatically converts package.yaml into a cabal file with hpack command.

So, delete package.yaml or edit package.yaml to add Euterpea package. Editing it would not be so difficult as its format is YAML.

1
FAntony On

I want to add to the YAMAMOTO Yuji's answer. The solution is absolutely right. But I just wanted to add few things, it is not hard to edit the package.yaml.

Step 1 : The trickiest part is finding the correct package name.

Use Hoogle or Stackage to find the package where the module resides. Read more about how to find package name in this post.

Step 2 : Now you have to open the package.yaml file and add the package name. In your case add 'Euterpea' package in the list of dependencies.

dependencies:
...
- your-package-name

Please note that Euterpea package has to be added in a different way. Please read this post for better understanding.

Step 3 : Open project-name.cabal in project root and add required package name under build-depends:

library
  hs-source-dirs:
      src

  build-depends:
      base >= 4.7 && < 5
    , your-package-name

  exposed-modules:
      Lib

Step 4 :Issue stack build to download and build dependencies (or stack ghci if you plan to use it in the REPL)

Hope this works! Happy coding! :)