My project structure:
MyProj
app
benchmarks
Investigate.hs
src
test
Example1.hs -- defines module Example1
My current package.yaml
(simplified):
name: proj
dependencies:
- base >= 4.7 && < 5
library:
source-dirs: src
executables:
proj-exe:
main: Main.hs
source-dirs: app
dependencies: proj
tests:
proj-test:
main: Spec.hs
source-dirs: test
dependencies: proj
benchmarks:
investigate:
main: benchmarks/Investigate.hs
dependencies: proj
I want to import a module (say Example1
that lives under test, and use it in Investigate.hs
under benchmarks. I am using stack
, so how exactly do I configure my package.yaml
?
CodePudding user response:
The obvious thing to do is to add an additional internal (private) library block to your package and put the shared code there, where it can be added to the dependencies
of both your tests and benchmarks.
Or you could make a separate folder of utility code for tests and benchmarks, and then include that folder in the source-dirs
list of both; there's nothing stopping a single folder being referenced in the source-dirs
of multiple components (although the downside is any files within will be compiled separately for each component, not compiled once and then shared).
I don't use stack, so I don't know precisely how either of these things manifest in their package.yaml syntax, but they are features of cabal-the-library, so I'm pretty sure stack should support them.
CodePudding user response:
Notice that when you use dependecies: proj
you are saying "This executable depends on the library proj"; and library proj lives within src
as you specified. If you want to use a function in benchmarks and test suites, you should add it to a module within the library source directories: src
. Below your package.yaml
with comments. I also included a wrong line at the end so it is clear why you must put the functionality under src
.
name: proj # This is the name of the library. Generally one library per project. You can use you `stack.yaml` to define more.
dependencies:
- base >= 4.7 && < 5
library: # Here is where the library functionality lives.
source-dirs: src
executables: # This is your executable
proj-exe:
main: Main.hs
source-dirs: app # Living under app directory
dependencies: proj # Which depends on the library. Which lives under src
tests: # This is your test suit
proj-test:
main: Spec.hs
source-dirs: test # Living under test directory
dependencies: proj # Which depends on the library. Which lives under src
benchmarks: # This is your benchmark. If you want to use a function living in
investigate: # the test folder, you would add it to the dependencies... but that's not allowed!!
main: benchmarks/Investigate.hs
dependencies:
- proj # dependency on the library proj living under src
- proj-test # This isn't allow because proj-test is not a library, is a test suite