Getting your Version
The Paths_foo has your version, but beware
This just a brief note to self, and y’all, on how an app can discover its own version.
Often it is useful to be able to expose your version, possibly in response to --version
request from the command line. Clearly it is desirable that this should not rely on manual intervention, leaving two options:
the Haskell build system makes it available somehow or
the versions get burned into the source as part of the project’s release mechanism.
For smaller projects the first variant is particularly welcome as they are unlikely to have the kind of release mechanism that can ensure the version is reliable made available to the running program.
Yay, the Toolchain has your back, mostly
If your app is called foo
then it will be found in the Paths_foo
module automagically maintained by the build system. (The Paths_foo
module is documented in the Cabal section of past GHC doc sets.)
If you need to use the version in an executable then it will have to list foo
in its Cabal dependencies.
The recipe I use
1. Add Paths_foo
to the library’s exposed modules
2. Import Paths_foo
and Data.Version
Import version
from Paths_foo
and Version
from Data.Version
into any module in the library needing access to the version. Your package’s version
will be of type Version
.
Why?
You can just list Paths_foo
in the other-modules:
of your executable but then ghci
will not be able to follow along.
Using hpack
?
If you don’t need ghci
to see the Paths_foo
then your generated cabal file should always have Paths_foo
listed in other-modules
and you just can import it. If you need ghci to see it will have to be listed in the library’s exposed-modules
.
Got an issue with any of this? Please share or drop me a line (see below).