<p dir="ltr">Cabal-helper looks like just the ticket, thanks!</p>
<div class="gmail_quote">On 11 Mar 2016 12:32, "Daniel Gröber" <<a href="mailto:dxld@darkboxed.org">dxld@darkboxed.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Fri, Mar 11, 2016 at 09:27:40AM +0000, David Turner wrote:<br>
> I've also briefly contemplated using the Cabal library to read my .cabal<br>
> file and work out what to do, but I'm unsure that this would work nicely<br>
> under stack. At least, I'm not sure quite what to do with all the package<br>
> databases and other stuff that stack does for you.<br>
<br>
Using the Cabal library to do this works very well in general, we do<br>
this in ghc-mod these days. The only way I've seen problems creep up<br>
so far is if cabal-install is linked against a different version of<br>
Cabal than your program. If your analysis program doesn't have to work<br>
on the same cabal dist directory as cabal-install (like ghc-mod does)<br>
then that's a non problem though.<br>
<br>
Anyways we've wrapped all the ugly stuff to work around this problem<br>
into a library called cabal-helper:<br>
<a href="http://hackage.haskell.org/package/cabal-helper/docs/Distribution-Helper.html" rel="noreferrer" target="_blank">http://hackage.haskell.org/package/cabal-helper/docs/Distribution-Helper.html</a><br>
<br>
Here's a little example program using that library which should do<br>
what you want:<br>
<br>
```<br>
import System.Environment<br>
import System.Directory<br>
import System.Process<br>
import Distribution.Helper<br>
import Data.Char<br>
import Data.List<br>
<br>
main = do<br>
  [project_dir] <- getArgs<br>
  setCurrentDirectory project_dir<br>
  dist_dir <- dropWhileEnd isSpace <$> readProcess "stack" ["path", "--dist-dir"] ""<br>
  print =<< (runQuery (defaultQueryEnv "." dist_dir) $ ghcOptions)<br>
<br>
```<br>
<br>
--Daniel</blockquote></div>