Skip to content

support for cabal/stack scripts #111

Open

Description

@georgefst

Certain upcoming features (repl, runghc) are likely to make cabal scripts a much more attractive option.

I, for one, would like to switch all of my turtle scripts to use them, rather than (as at present) having them all in a single cabal project. But I wouldn't want to lose the IDE support...

Would there be any major difficulty in supporting these? Is this actually more of a question for hie-bios?

Activity

  1. fendor commented on May 15, 2020

    @fendor
    Collaborator

    Supporting cabal script is a matter of implementing the issue: haskell/cabal#6149

    That is why stack scripts are already supported via hie-bios.

    I started to implement it, but there are a few roadbumps that makes it not as easy as hoped.

  2. added
    type: enhancement
    on May 15, 2020
  3. added a commit that references this issue on Dec 27, 2020
  4. changed the title [-]Cabal scripts[/-] [+]cabal/stack scripts[/+] on Dec 29, 2020
  5. changed the title [-]cabal/stack scripts[/-] [+]support for cabal/stack scripts[/+] on Dec 29, 2020
  6. jneira commented on Dec 29, 2020

    @jneira
    Member

    As commented in https://github.com/haskell/ghcide/issues/162 and here, there is a workaround to make stack scripts work:

    Stack scripts work in a "hacky" way.
    You have to add an explicit hie.yaml file with the contents:

    cradle:
      stack:
        component: "File.hs" # your local stack script
    

    Works fine for loading and modifying the stack script.
    However, if you modify the script section, these changes wont be detected, e.g. you have to restart ghcide/hls.

    The reason this works is that you can do stack repl File.hs and the session is loaded correctly.

    by @fendor

  7. jneira commented on Dec 29, 2020

    @jneira
    Member

    Related issue in hi-bios: haskell/hie-bios#217

  8. fendor commented on Dec 29, 2020

    @fendor
    Collaborator

    Afaik, stack repl File.hs ceased to work for stack scripts, therefore it does not work anymore with HLS :(
    I dont remember in which stack version it used to work (before 1.9.3?) and I cant find any related stack issues for it

  9. georgefst commented on Dec 29, 2020

    @georgefst
    CollaboratorAuthor

    FWIW, I have been making a lot of use of Cabal scripts, using cabal install --package-env . --lib [...]. It's not ideal, but I can live with it for now. Doesn't even require a cradle (EDIT: an explicit hie.yaml - you know what I mean). I don't think this worked when I opened the issue, but I can't remember why.

    If it weren't for haskell/cabal#6999 things would actually be rather nice.

  10. jneira commented on Feb 24, 2021

    @jneira
    Member

    @Ailrun this one would be related with #692? (so it would be included for gsoc as well in the same pack)

  11. Ailrun commented on Feb 24, 2021

    @Ailrun
    Member

    Would it be better to create a new combined issue for GSOC?

  12. jneira commented on Feb 24, 2021

    @jneira
    Member

    Mmm not sure being only 2, it is clear in each one that are related

  13. 12 remaining items

  14. dschrempf commented on Sep 29, 2022

    @dschrempf
    Collaborator

    I see. In thin case I advocate for improving the discovery mechanism. Thanks for explaining things to me!

  15. georgefst commented on Sep 29, 2022

    @georgefst
    CollaboratorAuthor

    This actually worked for me wit hls-1.8 and ghc924

    Not working for me unfortunately, with the same tools (Cabal 3.8.1.0, Linux).

    It would be great though, if the somewhat useless hie.yaml file could be omitted!

    I think "useless" is an exaggeration, as @fendor has said, but I agree that we should strive to make it unnecessary. I was also going to suggest parsing the bang line. There are potentially a lot of edge cases, but recognising #!/usr/bin/env cabal would be a start.

  16. dschrempf commented on Sep 30, 2022

    @dschrempf
    Collaborator

    Not working for me unfortunately, with the same tools (Cabal 3.8.1.0, Linux).

    It could be that this works for me because the script is within a project that provides the required libraries.

  17. georgefst commented on Dec 30, 2022

    @georgefst
    CollaboratorAuthor

    Just to confirm, no one's actually got this to work properly by specifying a hie.yaml, right? It seems unlikely that it's possible, right now, since HLS runs cabal exec -v0 -- ghc --print-libdir, which complains about not finding a *.cabal or cabal.project file.

    So does that mean we're still blocked on haskell/cabal#7500? Or is there, in principal, some easy way to extract the environment information from cabal repl?

  18. fendor commented on Dec 30, 2022

    @fendor
    Collaborator

    The former can be fixed via the linked PR.
    I don't think there is currently a way to get the options from cabal repl. However, we could tweak hie-bios to have a cli interface to get the options if and only if you specify the ghc version you want to use for scripts. Such a cli interface, could also be used to implement proper support... PRs welcome, won't be able to get to it the next couple of weeks, but I think the POC might be much simpler now.

  19. georgefst commented on Oct 27, 2023

    @georgefst
    CollaboratorAuthor

    I think the reason for the confusion above is that, in addition to the hie.yaml, a cabal.project file is currently required (whose contents don't seem to matter much). @fendor pointed this out in #3572.

    So loading scripts does now work, though the workarounds required are quite inconvenient if one has a lot of scripts.

  20. georgefst commented on May 29, 2026

    @georgefst
    CollaboratorAuthor

    a cabal.project file is currently required

    I don't know when this changed, but this is seemingly no longer needed! The boilerplate hie.yaml still is though.

  21. subterfugue commented on Jul 4, 2026

    @subterfugue

    @georgefst Do you have any idea about how that came to be? It looks like two alternatives to that PR that was mentioned might elide the need of a cabal.project file: one for cabal v2-path, and another for cabal target. The latter requires a cabal.project, but the former doesn't. Could that be what HLS uses now to determine the flags, or is it something else?

    Putting that aside, is modifying hie-bios to have a CLI interface to get the required flags still the main way to go about it? It was stated earlier in the discussion that HLS doesn't know how to differentiate between stack scripts and normal files, but cabal run has the ability to do that, for example, as does stack. Is there no way for them to expose the methods they use to check and use it in HLS, or just shell out to them instead if that's not what's already being done? Sorry for my ignorance, I'm not sure about how any of these projects work.

  22. georgefst commented on Jul 4, 2026

    @georgefst
    CollaboratorAuthor

    No idea, sorry. I don't think I've been personally involved in the implementation of any of this.

  23. fendor commented on Jul 7, 2026

    @fendor
    Collaborator

    Could that be what HLS uses now to determine the flags, or is it something else?

    It could be!

    Putting that aside, is modifying hie-bios to have a CLI interface to get the required flags still the main way to go about it?

    I don't think so, doesn't it almost work now out of the box, the only thing required being a

    cradle:
      cabal:

    We could try to refine the session guessing in HLS, if the module has a cabal script marker such as

    #!/usr/bin/env cabal
    {- cabal:
    build-depends:
      base,
      haskell-say ^>=1.0.0.0
    -}
    

    then we infer the correct cradle. Such changes would be in ghcide/session-loader/Development/IDE/Session/Implicit.hs, perhaps it is easy enough to do now?

    How does cabal check for these script markers in Haskell files?

  24. upsole commented on Sep 7, 2026

    @upsole

    Looks like cabal runs a simple parsing function to figure out the script metadata.

    We could use something similar to build a predicate and scan start_dir for a file that matches as the lowest priority option in this cradle guesser. simpleCabalCradle seems to do the trick.

    Would love to avoid the scan but it seems like loadImplicitCradle only ever gets called with a directory.

    What do you think?

  25. wz1000 commented on Sep 7, 2026

    @wz1000
    Collaborator

    I suspect if you could get cabal repl myscript.hs to work, then HLS would "just work" with the file as well.

  26. fendor commented on Sep 7, 2026

    @fendor
    Collaborator

    It would still require an explicit hie.yaml to tell hie-bios that it should interpreter it as a cabal cradle, so I am wondering whether we should add some limited parsing to hie-bios, e.g. bang pattern parsing to decide which cradle to use.

  27. upsole commented on Sep 12, 2026

    @upsole

    As far as I can tell, the logic deciding what happens in the absence of hie.yaml lives in ghcide/session-loader/Development/IDE/Session/Implicit.hs, so that's been my first guess as to where the parsing should happen. Do you mean you'd rather have this handled completely by hie-bios ?

    I've ran locally with this and it results on hie-bios compiling the script with v2-repl.

    inferCradleTree :: FilePath -> MaybeT IO (CradleTree a, FilePath)
    inferCradleTree start_dir =
           maybeItsBios
       -- If we have both a config file (cabal.project/stack.yaml) and a work dir
       -- (dist-newstyle/.stack-work), prefer that
       <|> (cabalExecutable >> cabalConfigDir start_dir >>= \dir -> cabalWorkDir dir >> pure (simpleCabalCradle dir))
       <|> (stackExecutable >> stackConfigDir start_dir >>= \dir -> stackWorkDir dir >> stackCradle dir)
       -- If we have a cabal.project OR we have a .cabal and dist-newstyle, prefer cabal
       <|> (cabalExecutable >> (cabalConfigDir start_dir <|> cabalFileAndWorkDir) <&> simpleCabalCradle)
       -- If we have a stack.yaml, use stack
       <|> (stackExecutable >> stackConfigDir start_dir >>= stackCradle)
       -- If we have a cabal file, use cabal
       <|> (cabalExecutable >> cabalFileDir start_dir <&> simpleCabalCradle)
       -- If we have a haskell cabal script, use cabal
       <|> (maybeHoldsCabalScript start_dir <&> simpleCabalCradle)

    Stack is another story as the currently existing functions seem to assume a stack.yaml being present.

    It's possibly too blunt of a solution as I can imagine I'm missing a lot of context

  28. fendor commented on Sep 13, 2026

    @fendor
    Collaborator

    Yes, I think that is a viable solution. I would like to see this check in hie-bios as well, since I often use hie-bios to debug cradles.

    So, the shebang parser should be in hie-bios and HLS should use it here as well.

    stack scripts don't need a stack.yaml, iirc, this would be completely counterproductive. https://docs.haskellstack.org/en/stable/commands/script_command/

    In theory (and practice), scripts could be polyglots for cabal and stack, e.g. this:

    #! /usr/bin/env stack
    {- stack script
       --snapshot lts-24.43
       --package haskell-say
    -}
    {- cabal:
    build-depends:
      base,
      haskell-say ^>=1.0.0.0
    -}
    
    import HaskellSay
    
    main :: IO ()
    main = haskellSay "Test"
    

    can be run with both stack Main.hs and cabal run Main.hs (or cabal repl Main.hs).

    Question is, do we want to support this, what is the cabal spec for when the magic executable stanza is accepted in the comment, or should we rely purely on the shebang line?

    It does look like both tools only need the magic comment block, so perhaps our inference could be just reading the shebang line to decide whether this is a cabal or stack script.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    build tool: cabalbuild tool: stackcan-workaroundstatus: blockedtype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions