Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LaurentRDC opened this issue Oct 30, 2022 · 12 comments

Comments

@LaurentRDC
Copy link

LaurentRDC commented Oct 30, 2022

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
@LaurentRDC LaurentRDC 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 Oct 30, 2022
@gbaz
Copy link
Collaborator

gbaz commented Oct 30, 2022

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

@ulysses4ever
Copy link
Collaborator

Yes, let’s page in @bacchanalia.

@bacchanalia
Copy link
Collaborator

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

@dschrempf
Copy link

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

@LaurentRDC
Copy link
Author

@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

@Mikolaj
Copy link
Member

Mikolaj commented Nov 29, 2022

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

@ulysses4ever
Copy link
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.

@fgaz
Copy link
Member

fgaz commented Nov 29, 2022

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

@dschrempf
Copy link

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?

@LaurentRDC
Copy link
Author

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.

@fgaz
Copy link
Member

fgaz commented Dec 1, 2022

@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.

@gbaz gbaz closed this as completed Dec 1, 2022
@ulysses4ever
Copy link
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...

georgefst added a commit to georgefst/monpad that referenced this issue Oct 18, 2023
georgefst added a commit to georgefst/monpad that referenced this issue Oct 28, 2023
We still have to run the build script with GHC 9.4, since we need to override `dhall`, and can't override `source-repository-package` for scripts (haskell/cabal#8562, haskell/cabal#8024 (comment)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants