You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
Reacted by Simon Michael, Rihards Krišlauks, Kristián Balaj, Fraser Lee, Yusuf Bham, Jaro, Jonathan Lee Komar, Enis Bayramoğlu, Berk Özkütük and Sohang Chopra
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.
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
Reacted by Junyoung/"Clare" Jang, Bietola, Vilem Liepelt and Yusuf Bham
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.
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.
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?
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.
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.
@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.
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?
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.
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.
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->MaybeTIO (CradleTreea, 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
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.
Activity
fendor commentedon May 15, 2020
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.
Add changelog boilerplate (#111)
[-]Cabal scripts[/-][+]cabal/stack scripts[/+][-]cabal/stack scripts[/-][+]support for cabal/stack scripts[/+]jneira commentedon Dec 29, 2020
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:
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
jneira commentedon Dec 29, 2020
Related issue in hi-bios: haskell/hie-bios#217
fendor commentedon Dec 29, 2020
Afaik,
stack repl File.hsceased 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
georgefst commentedon Dec 29, 2020
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 requirea cradle(EDIT: an explicithie.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.
jneira commentedon Feb 24, 2021
@Ailrun this one would be related with #692? (so it would be included for gsoc as well in the same pack)
Ailrun commentedon Feb 24, 2021
Would it be better to create a new combined issue for GSOC?
jneira commentedon Feb 24, 2021
Mmm not sure being only 2, it is clear in each one that are related
12 remaining items
dschrempf commentedon Sep 29, 2022
I see. In thin case I advocate for improving the discovery mechanism. Thanks for explaining things to me!
georgefst commentedon Sep 29, 2022
Not working for me unfortunately, with the same tools (Cabal 3.8.1.0, Linux).
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 cabalwould be a start.dschrempf commentedon Sep 30, 2022
It could be that this works for me because the script is within a project that provides the required libraries.
georgefst commentedon Dec 30, 2022
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 runscabal exec -v0 -- ghc --print-libdir, which complains about not finding a*.cabalorcabal.projectfile.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?fendor commentedon Dec 30, 2022
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 tweakhie-biosto 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.georgefst commentedon Oct 27, 2023
I think the reason for the confusion above is that, in addition to the
hie.yaml, acabal.projectfile 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.
georgefst commentedon May 29, 2026
I don't know when this changed, but this is seemingly no longer needed! The boilerplate
hie.yamlstill is though.subterfugue commentedon Jul 4, 2026
@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.projectfile: one forcabal v2-path, and another forcabal target. The latter requires acabal.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-biosto 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, butcabal runhas the ability to do that, for example, as doesstack. 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.georgefst commentedon Jul 4, 2026
No idea, sorry. I don't think I've been personally involved in the implementation of any of this.
fendor commentedon Jul 7, 2026
It could be!
I don't think so, doesn't it almost work now out of the box, the only thing required being a
We could try to refine the session guessing in
HLS, if the module has a cabal script marker such asthen 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?
upsole commentedon Sep 7, 2026
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_dirfor a file that matches as the lowest priority option in this cradle guesser.simpleCabalCradleseems to do the trick.Would love to avoid the scan but it seems like
loadImplicitCradleonly ever gets called with a directory.What do you think?
wz1000 commentedon Sep 7, 2026
I suspect if you could get
cabal repl myscript.hsto work, then HLS would "just work" with the file as well.fendor commentedon Sep 7, 2026
It would still require an explicit
hie.yamlto 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.upsole commentedon Sep 12, 2026
As far as I can tell, the logic deciding what happens in the absence of
hie.yamllives inghcide/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 byhie-bios?I've ran locally with this and it results on
hie-bioscompiling the script withv2-repl.Stack is another story as the currently existing functions seem to assume a
stack.yamlbeing present.It's possibly too blunt of a solution as I can imagine I'm missing a lot of context
fendor commentedon Sep 13, 2026
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 astack.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:
can be run with both
stack Main.hsandcabal run Main.hs(orcabal 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.