Skip to content

Cannot run a script using cabal run which depends on a local package with cabal-install 3.8 #8562

Closed

Description

@LaurentRDC

Description

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 the following script with cabal v2-run:

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

main = pure ()

The command cabal run scripts/MyScript.hs with cabal-install 3.6.2.0 works as expected. However, with cabal=install 3.8.1.0 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

To reproduce

I have set up a repository to show this behavior:

$ git clone https://github.com/LaurentRDC/cabal-3.8-script-issue && cd cabal-3.8-script-issue
$ ghcup set cabal 3.6.2.0
$ cabal v2-run scripts/MyScript.hs
$ ghcup set cabal 3.8.1.0
$ cabal v2-run scripts/MyScript.hs

Expected behavior

I'm expecting to be able to compile and run scripts/MyScript.hs

System information

  • Ubuntu 18.04
  • cabal-install version 3.8.1.0
  • ghc 9.4.2

Activity

  1. changed the title [-]Cannot run a script using `cabal run` which depends on a local package[/-] [+]Cannot run a script using `cabal run` which depends on a local package with cabal-install 3.8[/+] on Oct 30, 2022
  2. gbaz commented on Oct 30, 2022

    @gbaz
    Collaborator

    I suspect that the regression is likely due to refactors induced by #7851

  3. ulysses4ever commented on Oct 30, 2022

    @ulysses4ever
    Collaborator

    Yes, let’s page in @bacchanalia.

  4. bacchanalia commented on Oct 31, 2022

    @bacchanalia
    Collaborator

    I'll try to take a look at it next week.

  5. dschrempf commented on Nov 17, 2022

    @dschrempf

    I also have this problem. I tried adding a cabal.project file referencing the local package (using packages: ..) to no avail.

  6. LaurentRDC commented on Nov 26, 2022

    @LaurentRDC
    ContributorAuthor

    @bacchanalia if you don't have time to take a look, would you be able to orient me so I can see what's going on? I.e. which Cabal module is responsible for determining if local packages are in-scope?

    Unfortunately, all commits from #7851 were squashed, so I can't use git-bisect to pinpoint where exactly is the regression

  7. Mikolaj commented on Nov 29, 2022

    @Mikolaj
    Member

    @LaurentRDC: the commits seem preserved in the PR, so presumably also on the bacchanalia:master branch. Is it any help?

  8. ulysses4ever commented on Nov 29, 2022

    @ulysses4ever
    Collaborator

    I understand the desire to get back the feature that suddenly disappeared. But when I think about it in isolation of cabal history, it feels to me that scripts are meant to be self contained, and I consider them using a cabal.project a bug rather than a feature. Scripts support (a part of) cabal.project fields inside themselves not for nothing: for me, it's a clear indication that you're not supposed to have an external cabal.project.

    Overall, this ticket sounds to me like a feature request to extend (back) abilities of scripts beyond what they are meant to be. It's a fine request, of course, but if not for the history, I'd probably prefer this ability to be controlled by a flag, which, by default, would ignore nearby project files. But then, there's "if not for the history bit", so perhaps it should be the other way around.

    Another thought: if you really need a setup sophisticated enough to reach beyond basic project file capabilities supported in the script header, then you might as well bite the bullet and turn your code into a proper package. Sometimes, people dislike creating .cabal files, but with cabal init this is very-very easy. Maybe you'd want to give it a try while waiting for this feature request to be acted upon.

  9. fgaz commented on Nov 29, 2022

    @fgaz
    Member

    I agree with @ulysses4ever. You could add a cabal.project to your project, with something like packages: my-lib.cabal scripts/scripts.cabal, and have a scripts package under the scripts directory with one executable per script. then you can cabal run scripts:myscript.
    I think cabal scripts are mostly useful outside of projects

  10. dschrempf commented on Nov 29, 2022

    @dschrempf

    In this case, I think it would be useful to specify specific versions/revisions of script dependencies (i.e., versions not available on Hackage but only on Gitlab or GitHub, or somewhere else). Is this possible in a cabal script?

  11. LaurentRDC commented on Dec 1, 2022

    @LaurentRDC
    ContributorAuthor

    agree with @ulysses4ever. You could add a cabal.project to your project, with something like packages: my-lib.cabal scripts/scripts.cabal, and have a scripts package under the scripts directory with one executable per script. then you can cabal run scripts:myscript.
    I think cabal scripts are mostly useful outside of projects

    Thank you @fgaz , that's a great suggestion which works well. I'm satisfied.

  12. fgaz commented on Dec 1, 2022

    @fgaz
    Member

    @dschrempf see #8024. packages should work as long as you provide absolute paths, and it looks like source-repository-package doesn't work but I think it should.

  13. ulysses4ever commented on Dec 1, 2022

    @ulysses4ever
    Collaborator

    So, is resolution here: won't fix because doesn't align with the idea of scripts? It may still be a reasonable feature request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions