4

I have a Haskell library that I am developing using Stack. As I am developing the library, I like to write small test/experimentation programs that use the library. I keep a collection of these test programs for myself in a directory locally. These test modules are very quick and informal, and not appropriate to include as unit tests in the committed library code. Typically, most of them aren't even maintained and won't compile against the latest version of the library, but I keep them around in case I want to update them later. When I'm working on a test program, I want it to build against my working copy of the library, with any changes that I've made to the library locally.

How should I set up my Stack build environment for this situation? Here are some options I've tried, and the problems with each options.

  1. Two Cabal packages, one Stack configuration. The stack.yaml file lists both packages and defines the build environment for both at once.

    • Problem: The stack.yaml file needs to be included as part of the committed library source code, so that other developers can build the library from source reproducibly. I don't want the public stack.yaml file for my library to include build information for my local test projects.
    • Problem: As far as I know, to make this work I need to have a .cabal file that lists all the executables and modules for my test programs. This is annoying to update whenever I want to throw together a quick experimental script, and will fail to build any of the test programs if I have even a single module that doesn't compile. I can't have a .cabal file with no sections, because Cabal gives "No executables, libraries,tests, or benchmarks found. Nothing to do.", and because this offers nowhere to list build-depends.
  2. Create a Cabal sandbox for the test programs. Use cabal sandbox add-source to add the local library as a package. See also this answer.

    • Problem: Using Cabal sandboxes instead of Stack reintroduces a lot of the dependency problems that Stack is supposed to fix, such as using the system-global GHC instead of the GHC defined by the resolver.
  3. Have a separate stack.yaml for the test programs. Add the library under packages as location: 'C:\Path\To\Local\Library' and set extra-dep: true for that dependency. (See here for more info on this feature.) Don't put any other Cabal packages under packages in the stack.yaml for the test programs. Use stack runghc to invoke test programs within the scope of their stack.yaml.

    • Problem: I just can't get this one to work. Running stack build inside the test program directory gives "Error parsing targets: The project contains no local packages (packages not marked with 'extra-dep')". Running stack runghc acts as if no dependencies are present at all. I don't want to add a Cabal package for the test programs because this has the same problem as option 1 with needing to construct an explicit .cabal file describing the modules to build.
    • Problem: Stack build configuration info that I want to be identical between the library and the test programs has to be copied manually. For example, if I change the resolver in my library's stack.yaml, I also need to change it in the stack.yaml for my test programs separately.
  4. Have a directory inside my working copy of the library that contains all of my test programs. Use stack runghc to invoke test programs in the context of the library.

    • Problem: I'd like the directory with my test programs to be outside of the directory containing my library source code and build configuration, so that I don't have to tell the version control for my library to ignore my test code, and can have my own local version control just for the test programs.
    • Problem: Only works with a single local library dependency. If my test programs need to depend on local working copies of two different libraries with their own stack.yaml files, I'm out of luck.
  5. Add a symbolic link inside my working copy of the library to a separate directory that contains all of my test programs. Navigate through the symlink and use stack runghc to invoke test programs in the context of the library.

    • Problem: Super awkward to use, especially since I'm on Windows and Windows has terrible symlink support.
    • Problem: Still need to tell my version control system to ignore the symlink.
    • Problem: Still only works with a single library dependency.
Community
  • 1
  • 1
Aaron Rotenberg
  • 972
  • 7
  • 22
  • Question to close voters: Is the main reason for voting "too broad" the wording of the question as "How should I...", which appears subjective? The original intention of the question was to ask for the *canonical solution* (i.e. the one that is *officially preferred and recommended by the Stack/Cabal developers*) for this situation, if one exists. Would the "too broad" issue be alleviated if I specified more clearly that this was the intent of the question? – Aaron Rotenberg Apr 29 '17 at 23:38
  • Even though the answers so far do not attempt to offer *canonical* solutions, they have been helpful to me personally, because they suggest options that have fewer drawbacks than the ones I had previously considered. I would appreciate suggestions on how to reword the question to make it less close-worthy without affecting the validity of this type of answer, since the current answers are demonstrably useful (the answer by @Li-yao Xia shows that I am not the only one to run into this problem). – Aaron Rotenberg Apr 30 '17 at 00:17

2 Answers2

2

If only one local library is involved, I use option 4. You can put your tests outside the directory of your library, and either invoke stack from the directory of your library, or using --stack-yaml path/to/library/stack.yaml.

Otherwise, I use option 3, creating a separate stack project without setting extra-dep.

...
packages:
- 'path/to/package1'
- 'path/to/package2'
...

I can't think of a good workaround for the issue of configuration duplication. There would otherwise be conflicts if multiple packages specified different resolvers/package versions.

Li-yao Xia
  • 31,896
  • 2
  • 33
  • 56
1

Edit: Actually a stub library works better, so edited to add.

I think the way to get #3 to work is -- under your scratch program directory -- (1) add . under packages in stack.yaml alongside the location/extra-dep: true package:

packages:
- '.'
- location: ../mylib
  extra-dep: true

(2) create an executable clause in scratch.cabal that points to a stub main program (i.e., a "Hello World" program that compiles but need not do anything) which depends on your library:

executable main
  hs-source-dirs:      src
  main-is:             Stub.hs
  build-depends:       base
                     , mylib
  default-language:    Haskell2010

or a library clause with no exposed modules, again that depends on your mylib library:

library
  hs-source-dirs:      src
  build-depends:       base >= 4.7 && < 5
                     , mylib
  default-language:    Haskell2010

and (3) run stack build in the scratch directory. This should build and register mylib, and now stack runghc Prog1.hs should work fine for running programs that depend on mylib modules.

If you use the executable approach, the stub program is compiled as a side effect but otherwise ignored. If you use the library approach, it looks like the stub library isn't even built; and you then have the option of actually building a scratch library by adding some exposed modules of shared code for your test programs to use, if it's convenient, so the stub library might be best.

None of this solves the problem of keeping stack.yaml info like the resolver version in sync, but it seems to address all the problems you list in 1, 2, 4, and 5. In particular, it should work fine for test programs that depend on multiple local libraries you're developing.

K. A. Buhr
  • 45,621
  • 3
  • 45
  • 71
  • Actually, it looks like with the `library` option, you can have a totally blank `hs-source-dirs` line. Just `hs-source-dirs:` and then a newline. Fancy. – Aaron Rotenberg Apr 30 '17 at 00:07