Running a script using a local library with Cabal

Hello all,

Let’s say I have a project with an executable and a library. The repository looks like:

app/
   app/Main.hs
lib/
   lib/MyLib.hs
scripts/
   scripts/MyScript.hs
my-lib.cabal

I’d like to run a script (scripts/MyScript) which looks like this:

import qualified MyLib

main = pure () -- Imagine doing something with the Foo library

The documentation for cabal run shows how to include dependencies in the script file:

#!/usr/bin/env cabal
{- cabal:
build-depends: base
             , my-lib
-}
import qualified MyLib

main = pure () -- Imagine doing something with the Foo library

However, when I run cabal run scripts/MyScript.hs, I get an error like this:

Error: cabal: Could not resolve dependencies:
[__0] trying: fake-package-0 (user goal)
[__1] unknown package: my-lib (dependency of fake-package)
[__1] fail (backjumping, conflict set: fake-package, my-lib)
After searching the rest of the dependency tree exhaustively, these were the
goals I've had most trouble fulfilling: fake-package, my-lib

Any ideas on how to make cabal see foo? I understand I could do something like cabal install --lib. Would I not need to re-install whenever I update the foo package?

2 Likes

I cannot reproduce this. It is working fine for me.

(It also might be a good idea to use a different name for your example package, because foo already exists on Hackage.)

Edit: I’m using cabal version:

cabal-install version 3.6.2.0
compiled using version 3.6.3.0 of the Cabal library
2 Likes

Good suggestion, I’ve edited the post.

Interestingly, just like @jaror said, it works on cabal 3.6.2.0, but not when I’m using cabal 3.8.1.0. Looks like a regression.

One thing that I did notice is that I get the same error if I first change into the scripts directory. I was able to fix that by adding this:

#!/usr/bin/env cabal
{- cabal:
build-depends: base
             , my-lib
-}
{- project:
packages: ../my-lib.cabal
-}
import qualified MyLib

main = pure () -- Imagine doing something with the Foo library

Maybe that does work with cabal 3.8?

Or alternatively you can add a cabal.project file (in the root directory of your project) containing:

packages: .

The additional project clause doesn’t help with cabal 3.8. I’ll file a ticket in the cabal repo and maybe I can figure out what has changed.

1 Like

Tracked in this ticket: cabal/issues/8562.

1 Like