-
Notifications
You must be signed in to change notification settings - Fork 697
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
Environment files get written to $TMP when using cabal scripts #6999
Comments
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 |
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 Perhaps the solution to #6977 will solve this as a side-effect... |
Ok so this is an actual bug (I don't think anyone wants cabal to put the env file in /tmp by default) |
this issue may have been resolved by #7851, since script builds are now cached on disk |
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 |
I'll need to build Cabal |
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
So thanks @bacchanalia! |
Incidentally, I've found that the existence of a |
I'm glad to hear my improvements help you! I did some testing, and it looks now like the only thing written to The environment file gets written to the cache dir now even with This looks to me like another issue that will only be resolved when #6977 is. |
Additionally, as far as I can tell based on recursive file watching, |
Hi, nowadays
|
Thanks to useful pointer from haskell/cabal#6999 (comment). File watching actually doesn't work, as the same hash is reused for the same script (based on location?) even after edits. This is much cleaner anyway.
Thanks @jneira! That gets me back to having a working workaround: #!/usr/bin/env cabal
{-# LANGUAGE GHC2021 #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -threaded #-}
{- cabal:
build-depends:
base,
bytestring,
filepath-bytestring,
rawfilepath,
unix,
-}
{- | This will find a GHC environment file created by a cabal script, and copy it in to the current directory.
It is the workaround mentioned [here](https://github.com/haskell/cabal/issues/6999).
-}
module Main (main) where
import Data.ByteString.Char8 (
isInfixOf,
isPrefixOf,
lines,
putStrLn,
unlines,
)
import Data.ByteString.RawFilePath (readFile, writeFile)
import Data.Foldable (find, for_)
import RawFilePath (listDirectory, proc, readProcessWithExitCode)
import System.Exit (exitFailure)
import System.FilePath.ByteString (takeFileName, (</>))
import System.Posix.ByteString (getArgs)
import Prelude hiding (lines, putStrLn, readFile, unlines, writeFile)
main :: IO ()
main = do
scriptFile <-
getArgs >>= \case
[scriptFile] -> pure scriptFile
_ -> putStrLn "Provide a cabal script file" >> exitFailure
(_exitCode, out, _err) <-
readProcessWithExitCode $
proc "cabal" ["build", "-v", scriptFile, "--write-ghc-environment-files=always"]
p <- case find ("script-builds" `isInfixOf`) $ lines out of -- TODO this may be a brittle heuristic
Just p -> pure p
Nothing -> putStrLn "Failed to parse cabal output" >> exitFailure
envFiles <- filter (".ghc.environment." `isPrefixOf`) <$> listDirectory p
if null envFiles
then putStrLn "No environment files found" >> exitFailure
else putStrLn "Found environment files:"
for_ envFiles \envFile -> do
putStrLn envFile
contents <- readFile $ p </> envFile
writeFile (takeFileName envFile)
. unlines
. filter (not . ("package-db dist-newstyle" `isPrefixOf`))
$ lines contents |
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. |
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
andrunghc
would just work, andhaskell-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 tocabal install --package-env . --lib
, but it would be nice to have a proper solution.The text was updated successfully, but these errors were encountered: