Repository navigation
Closed
Description
cabal v2-repl scriptfile.hs outside project to load a scriptfile.hs into repl (GHCi).
Suppose I have a script file like:
{- cabal:
build-depends: base, splitmix
-}
import Data.List (unfoldr)
import Data.Word (Word64)
import System.Random.SplitMix (nextWord64, mkSMGen)
numbers :: [Word64]
numbers = unfoldr (Just . nextWord64) (mkSMGen 42)
main :: IO ()
main = print $ take 10 numberscabal v2-run -v0 scriptfile.hs works perfectly, however it takes quite a lot of time, 10s on my machine!
% time cabal v2-run -v0 ExRun.hs
[1275548033995301424,10417309031967933079,2112719111588962399,2726445820918627087,4609004260007739600,11074269206337254866,9733109294540808081,6450629194222476823,8084222243108663947,16253064923614931039]
cabal v2-run -v0 ExRun.hs 10,70s user 1,01s system 99% cpu 11,724 total
Slowness is one issue, after all there's compiling involved. It however makes development of scriptfiles extremely painful.
Yet, if the following worked, the development would be a lot nicer (repl ftw):
% cabal v2-repl ExRun.hs
Resolving dependencies...
Build profile: -w ghc-8.6.5 -O1
In order, the following will be built (use -v for more details):
- fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
...
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( ExRun.hs, interpreted )
Ok, one module loaded.
*Main>
*Main> main
[1275548033995301424,10417309031967933079,2112719111588962399,2726445820918627087,4609004260007739600,11074269206337254866,9733109294540808081,6450629194222476823,8084222243108663947,16253064923614931039]
worked, it would be super great.
For now, I try to cabal install --lib the dependencies, and use raw ghci.
Reacted by Emily and neurallambda
Reacted by Emily and Rihards Krišlauks
Activity
mpickering commentedon Dec 14, 2019
I'm interested in this feature. Could you give some pointers about how hard it would be to implement? (cc @fendor)
fendor commentedon Apr 30, 2020
I intend to work on this.
The implementation of the scriptfile feature seems to be mainly in
CmdInstall. It sets up a fake project by essentially writing a *.cabal file to a temporary location and then building this fake project.First, we should move implementation details from
CmdInstallto its own module, proposing:CmdScript.hs.The implementation itself seems trivial (we will see), the only difficulty I anticipate is to detect changes in the meta-data section of the script on reload (e.g.
:rin the ghci session).phadej commentedon Apr 30, 2020
The fake package stuff is better put into
Distribution.Client.FakePackagesomething like that.CmdScriptwould be confusing as there could becabal scriptfunctionality (but it's part ofrun).phadej commentedon Apr 30, 2020
It's worth to make clear to one self how
cabal v2-build,cabal v2-runandcabal v2-replbehave outside and inside project. Perfectly these won't have different behaviors / cli interfaces, and I think the easiest way to get there is to abstract common parts more heavily.fendor commentedon Apr 30, 2020
so, the first implementation was actually rather trivial.
The current mechanism is to copy the file to some
/tmp/cabal-repl.directory and generating a very basicfake-package.cabal. So, we can just open a repl session in that project context. (Although, admittedly, it doesnt make sense to set the cwd to/tmp/cabal-repl.but that is a task for later).The hard problem is to use
:rin the repl session. I would expect that this re-compiles changes, such as source changes and changes to the meta-data.However, since the source file is copied, neither of these changes can be detected. I had the following ideas:
Script.hsto/tmp/cabal-repl.*/Main.hscabal.projectandfake-package.cabalto/tmp/cabal-repl.*/Now use semantically the invocation
cabal repl Script.hs --project-file /tmp/cabal-repl.*/cabal.projectAre there more options? Can we hook somehow into the reload command of the repl-session to automatically get notified about changes to
Script.hs?Btw, stack manages to do both, on
:rchanges to the source file and meta-data are reloaded appropriately.they are quite similar indeed!
phadej commentedon May 1, 2020
It is ok to not support updating on meta-data change.
cabal repldoesn't update anything when you update.cabalfile either. Changing meta-data might change everything in GHCi setup. In other word: let's not try hook anything to:rcommand. I say that's out-of-scope.One way to approach this is maybe to make
cabal repl Script.hsto load current project and theScript.hsfirst. Then no-project case (this issue) would becabal repl /absolute/path/to/Script.hsin the fake project context.fendor commentedon May 1, 2020
So, workflow is roughly as follows
cabal repl Script.hs:fake-package.cabalwith the build-depends andmain-is: Main.hs.Main.hsas well as<cwd>/Script.hsand addScript.hsto the repl and set the working dir correctly. Changes toScript.hsare detected on:r.What I dont like about this is that two files are loaded,
Main.hsand<cwd>/Script.hs. It seems I cant setmain-is: <cwd>/Script.hs, according to the docs, and I admit that I prefer it that way.However, then we have to set some
main-isand load this file oncabal repl Script.hs.A work-around I have been thinking about:
librarystanza instead ofexecutableinfake-package.cabal.executableGrammar, so that does not really workAny other good way of handling this?
neongreen commentedon Jan 1, 2021
This would be absolutely lovely to have.
georgefst commentedon Jan 1, 2021
It really would.
Just to point out, seeing as it hasn't been explicitly linked to this issue, that due to #6999, a
.ghc.environmentfile gets created in$TMPwhen a cabal script is run, but it's swiftly deleted. Putting that file alongside the script would otherwise allow us to open a repl with the correct environment by just runningghci.Add script support to cabal repl
Update documentation for better script support
Add script support to cabal repl
Update documentation for better script support
Add script support to cabal repl
Update documentation for better script support
Add script support to cabal repl
Update documentation for better script support
Better support for running scripts. (#7851)