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
{{ message }}
Repository navigation
Environment files get written to $TMP when using cabal scripts #6999
Running cabal run --write-ghc-environment-files=always on a cabal script doesn't actually write a GHC environment file.
It would be great if it did, since it would mean that after the first run, straightforward calls to ghc and runghc would just work, and haskell-language-server, for one, would be able to load the file without requiring any setup.
Is there any fundamental reason why this shouldn't just work?
I've actually ended up writing a hacky little script to parse the build-depends field and pass all the package names to cabal install --package-env . --lib, but it would be nice to have a proper solution.
Does adding --package-env=/absolute/path/to/env work? It might be possible that cabal is writing the env file in the temporary "fake package" directory
--package-env doesn't seem to be a valid flag to any command other than install.
But you're right that the environment file does get written to disk, albeit temporarily. I've now scripted a slightly less hacky solution based on recursively watching /tmp and copying the file out.
Perhaps the solution to #6977 will solve this as a side-effect...
changed the title [-]Environment files get written to /tmp when using cabal scripts[/-][+]Environment files get written to $TMP when using cabal scripts[/+]on Jul 30, 2020
hmm maybe the title should be changed but iiuc the issue goals were:
being able to load the script with a plain ghci/ghc
to make easier hls load the script
Afaik #7851 does not resolve 1 but it does for 2, as a simple cabal cradle would be able to load it with cabal repl script.hs @georgefst what do you think about closing?
I don't think this can really be closed, as the issue still stands.
But the improvements to scripts do make it a lesser issue. Indeed, for my use case, the desire to generate environment files entirely disappears, since the benefits of calling ghc/ghci directly no longer apply:
We can get a REPL with the right environment using cabal repl
build and run take only a fraction of a second when no work needs to be done
Incidentally, I've found that the existence of a cabal.project file (say, a totally empty one) is enough to avoid this behaviour. Naively, I would've thought that scripts ignore cabal.project files by default, but this is evidently not the case. Note that when a cabal.project file is picked up, dist-newstyle also ends up in the current directory, rather than /tmp.
I did some testing, and it looks now like the only thing written to dist-newtype when a cabal.project file is present is dist-newstyle/cache/config. The reason we read the cabal.project file is to determine whether a given target passed to cabal run is a regular target or a script. In theory in unambiguous cases (the script path contains characters that are not valid in a regular target) we could bypass reading the project file, but we don't do so currently.
The environment file gets written to the cache dir now even with cabal.project file present, so in some sense the script improvements made things worse in that the work around no longer works.
This looks to me like another issue that will only be resolved when #6977 is.
The environment file gets written to the cache dir now even with cabal.project file present, so in some sense the script improvements made things worse in that the work around no longer works.
Additionally, as far as I can tell based on recursive file watching, cabal-3.8.1.0 run Script.hs --write-ghc-environment-files=always no longer results in an environment file being written anywhere in /tmp (or anywhere else I can think of i.e. the current directory), which unfortunately breaks my other workaround.
But the improvements to scripts do make it a lesser issue. Indeed, for my use case, the desire to generate environment files entirely disappears, since the benefits of calling ghc/ghci directly no longer apply:
Actually, this isn't quite true. Environment files are still the easiest way to get Cabal scripts to load in HLS: haskell/haskell-language-server#111.
Activity
fgaz commentedon Jul 30, 2020
Does adding --package-env=/absolute/path/to/env work? It might be possible that cabal is writing the env file in the temporary "fake package" directory
georgefst commentedon Jul 30, 2020
--package-envdoesn't seem to be a valid flag to any command other thaninstall.But you're right that the environment file does get written to disk, albeit temporarily. I've now scripted a slightly less hacky solution based on recursively watching
/tmpand copying the file out.Perhaps the solution to #6977 will solve this as a side-effect...
fgaz commentedon Jul 30, 2020
Ok so this is an actual bug (I don't think anyone wants cabal to put the env file in /tmp by default)
[-]Cabal scripts and environment files[/-][+]Environment files get written to /tmp when using cabal scripts[/+][-]Environment files get written to /tmp when using cabal scripts[/-][+]Environment files get written to $TMP when using cabal scripts[/+]bacchanalia commentedon Feb 15, 2022
this issue may have been resolved by #7851, since script builds are now cached on disk
jneira commentedon Feb 15, 2022
hmm maybe the title should be changed but iiuc the issue goals were:
Afaik #7851 does not resolve 1 but it does for 2, as a simple cabal cradle would be able to load it with
cabal repl script.hs@georgefst what do you think about closing?
georgefst commentedon Feb 15, 2022
I'll need to build Cabal
HEADand investigate in the coming days, to see what the ergonomics are now like.georgefst commentedon Feb 27, 2022
I don't think this can really be closed, as the issue still stands.
But the improvements to scripts do make it a lesser issue. Indeed, for my use case, the desire to generate environment files entirely disappears, since the benefits of calling
ghc/ghcidirectly no longer apply:cabal replbuildandruntake only a fraction of a second when no work needs to be doneSo thanks @bacchanalia!
georgefst commentedon Feb 27, 2022
Incidentally, I've found that the existence of a
cabal.projectfile (say, a totally empty one) is enough to avoid this behaviour. Naively, I would've thought that scripts ignorecabal.projectfiles by default, but this is evidently not the case. Note that when acabal.projectfile is picked up,dist-newstylealso ends up in the current directory, rather than/tmp.bacchanalia commentedon Feb 27, 2022
I'm glad to hear my improvements help you!
I did some testing, and it looks now like the only thing written to
dist-newtypewhen acabal.projectfile is present isdist-newstyle/cache/config. The reason we read thecabal.projectfile is to determine whether a given target passed to cabal run is a regular target or a script. In theory in unambiguous cases (the script path contains characters that are not valid in a regular target) we could bypass reading the project file, but we don't do so currently.The environment file gets written to the cache dir now even with
cabal.projectfile present, so in some sense the script improvements made things worse in that the work around no longer works.This looks to me like another issue that will only be resolved when #6977 is.
georgefst commentedon Jan 3, 2023
Additionally, as far as I can tell based on recursive file watching,
cabal-3.8.1.0 run Script.hs --write-ghc-environment-files=alwaysno longer results in an environment file being written anywhere in/tmp(or anywhere else I can think of i.e. the current directory), which unfortunately breaks my other workaround.jneira commentedon Jan 4, 2023
Hi, nowadays
cabal runbuild products are placed in$CABALDIR/script-builds/$hash, in my tests the env file is also there:Update cabal script environment hack for Cabal 3.8
georgefst commentedon Jan 5, 2023
Thanks @jneira! That gets me back to having a working workaround:
georgefst commentedon Sep 22, 2023
Actually, this isn't quite true. Environment files are still the easiest way to get Cabal scripts to load in HLS: haskell/haskell-language-server#111.