@@ -14,7 +14,7 @@ module Obelisk.Command.Run where
1414import Control.Arrow ((&&&))
1515import Control.Exception (Exception, bracket)
1616import Control.Lens (ifor, (.~), (&))
17−import Control.Monad (filterM)
17+import Control.Monad (filterM, unless, void)
1818import Control.Monad.Except (runExceptT, throwError)
1919import Control.Monad.IO.Class (liftIO)
2020import Control.Monad.Reader (MonadIO)
@@ -25,14 +25,12 @@ import Data.Default (def)
2525import Data.Either (partitionEithers)
2626import Data.Foldable (fold, for_, toList)
2727import Data.Functor.Identity (runIdentity)
28−import Data.List (intercalate)
2928import Data.List.NonEmpty (NonEmpty)
3029import qualified Data.List.NonEmpty as NE
3130import Data.Map (Map)
3231import qualified Data.Map as Map
3332import qualified Data.Map.Monoidal as MMap
34−import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
35−import Data.Ord (comparing)
33+import Data.Maybe
3634import Data.Set (Set)
3735import qualified Data.Set as Set
3836import Data.String.Here.Interpolated (i)
@@ -46,17 +44,17 @@ import Distribution.Compiler (CompilerFlavor(..))
4644import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
4745import Distribution.Parsec.ParseResult (runParseResult)
4846import qualified Distribution.System as Dist
49−import Distribution.Types.BuildInfo (buildable, cppOptions, defaultExtensions, defaultLanguage, hsSourceDirs, options)
50−import Distribution.Types.CondTree (simplifyCondTree)
51−import Distribution.Types.GenericPackageDescription (ConfVar (Arch, Impl, OS), condLibrary)
52−import Distribution.Types.Library (libBuildInfo)
53−import Distribution.Utils.Generic (toUTF8BS, readUTF8File)
47+import Distribution.Types.BuildInfo
48+import Distribution.Types.CondTree
49+import Distribution.Types.GenericPackageDescription
50+import Distribution.Types.Library
51+import Distribution.Utils.Generic
5452import qualified Distribution.Parsec.Common as Dist
5553import qualified Hpack.Config as Hpack
5654import qualified Hpack.Render as Hpack
5755import qualified Hpack.Yaml as Hpack
58−import Language.Haskell.Extension (Extension, Language)
59−import qualified Network.Socket as Socket
56+import Language.Haskell.Extension
57+import Network.Socket hiding (Debug)
6058import System.Directory
6159import System.Environment (getExecutablePath)
6260import System.FilePath
@@ -66,18 +64,19 @@ import System.IO.Temp (withSystemTempDirectory)
6664import Obelisk.App (MonadObelisk)
6765import Obelisk.CliApp (
6866 Severity (..),
67+ createProcess_,
6968 failWith,
7069 proc,
7170 putLog,
7271 readCreateProcessWithExitCode,
7372 readProcessAndLogStderr,
74− runProcess_,
7573 setCwd,
7674 setDelegateCtlc,
75+ waitForProcess,
7776 withSpinner,
7877 )
7978import Obelisk.Command.Nix
80−import Obelisk.Command.Project (nixShellWithoutPkgs, withProjectRoot, findProjectAssets, bashEscape)
79+import Obelisk.Command.Project (nixShellWithoutPkgs, withProjectRoot, findProjectAssets)
8180import Obelisk.Command.Thunk (attrCacheFileName)
8281import Obelisk.Command.Utils (findExePath, ghcidExePath)
8382
@@ -94,11 +93,9 @@ data CabalPackageInfo = CabalPackageInfo
9493 -- ^ List of globally set languages of the library component
9594 , _cabalPackageInfo_compilerOptions :: [(CompilerFlavor, [String])]
9695 -- ^ List of compiler-specific options (e.g., the "ghc-options" field of the cabal file)
97− , _cabalPackageInfo_cppOptions :: [String]
98− -- ^ List of CPP (C Preprocessor) options (e.g. the "cpp-options" field of the cabal file)
9996 }
10097
101−-- | 'Bool' with a better name for its purpose.
98+-- | 'Bool' with a better name for it's purpose.
10299data Interpret = Interpret_Interpret | Interpret_NoInterpret deriving (Eq, Ord, Show)
103100
104101textInterpret :: Interpret -> Text
@@ -151,7 +148,7 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
151148 liftIO $ createDirectoryIfMissing True $ takeDirectory $ root </> profileBaseName
152149 putLog Debug $ "Storing profiled data under base name of " <> T.pack (root </> profileBaseName)
153150 freePort <- getFreePort
154− runProcess_ $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
151+ (_, _, _, ph) <- createProcess_ "runProfExe" $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
155152 [ show freePort
156153 , T.unpack assets
157154 , profileBaseName
@@ -159,16 +156,17 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
159156 , "-po" <> profileBaseName
160157 ] <> rtsFlags
161158 <> [ "-RTS" ]
159+ void $ waitForProcess ph
162160
163161run :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
164162run root interpretPaths = do
165163 pkgs <- getParsedLocalPkgs root interpretPaths
166− assets <- findProjectAssets root
167− putLog Debug $ "Assets impurely loaded from: " <> assets
168− ghciArgs <- getGhciSessionSettings pkgs root True
169− freePort <- getFreePort
170− withGhciScriptArgs pkgs $ \dotGhciArgs -> do
171− runGhcid root True (ghciArgs <> dotGhciArgs) pkgs $ Just $ unwords
164+ withGhciScript pkgs $ \dotGhciPath -> do
165+ assets <- findProjectAssets root
166+ putLog Debug $ "Assets impurely loaded from: " <> assets
167+ ghciArgs <- getGhciSessionSettings pkgs root True
168+ freePort <- getFreePort
169+ runGhcid root True (ghciArgs <> mkGhciScriptArg dotGhciPath) pkgs $ Just $ unwords
172170 [ "Obelisk.Run.run"
173171 , show freePort
174172 , "(Obelisk.Run.runServeAsset " ++ show assets ++ ")"
@@ -180,15 +178,15 @@ runRepl :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
180178runRepl root interpretPaths = do
181179 pkgs <- getParsedLocalPkgs root interpretPaths
182180 ghciArgs <- getGhciSessionSettings pkgs "." True
183− withGhciScriptArgs pkgs $ \dotGhciArgs ->
184− runGhciRepl root pkgs (ghciArgs <> dotGhciArgs)
181+ withGhciScript pkgs $ \dotGhciPath ->
182+ runGhciRepl root pkgs (ghciArgs <> mkGhciScriptArg dotGhciPath)
185183
186184runWatch :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
187185runWatch root interpretPaths = do
188186 pkgs <- getParsedLocalPkgs root interpretPaths
189187 ghciArgs <- getGhciSessionSettings pkgs root True
190− withGhciScriptArgs pkgs $ \dotGhciArgs ->
191− runGhcid root True (ghciArgs <> dotGhciArgs) pkgs Nothing
188+ withGhciScript pkgs $ \dotGhciPath ->
189+ runGhcid root True (ghciArgs <> mkGhciScriptArg dotGhciPath) pkgs Nothing
192190
193191exportGhciConfig :: MonadObelisk m => Bool -> FilePath -> PathTree Interpret -> m [String]
194192exportGhciConfig useRelativePaths root interpretPaths = do
@@ -304,16 +302,15 @@ parseCabalPackage
304302 -> m (Maybe CabalPackageInfo)
305303parseCabalPackage dir = parseCabalPackage' dir >>= \case
306304 Left err -> Nothing <$ putLog Error err
307− Right (Just (warnings, pkgInfo)) -> do
305+ Right (warnings, pkgInfo) -> do
308306 for_ warnings $ putLog Warning . T.pack . show
309307 pure $ Just pkgInfo
310− Right Nothing -> pure Nothing
311308
312309-- | Like 'parseCabalPackage' but returns errors and warnings directly so as to avoid 'MonadObelisk'.
313310parseCabalPackage'
314311 :: (MonadIO m)
315312 => FilePath -- ^ Package directory
316− -> m (Either T.Text (Maybe ([Dist.PWarning], CabalPackageInfo)))
313+ -> m (Either T.Text ([Dist.PWarning], CabalPackageInfo))
317314parseCabalPackage' pkg = runExceptT $ do
318315 (cabalContents, packageFile, packageName) <- guessCabalPackageFile pkg >>= \case
319316 Left GuessPackageFileError_NotFound -> throwError $ "No .cabal or package.yaml file found in " <> T.pack pkg
@@ -341,7 +338,7 @@ parseCabalPackage' pkg = runExceptT $ do
341338 case condLibrary <$> result of
342339 Right (Just condLib) -> do
343340 let (_, lib) = simplifyCondTree evalConfVar condLib
344− pure $ Just $ (warnings,) $ CabalPackageInfo
341+ pure $ (warnings,) $ CabalPackageInfo
345342 { _cabalPackageInfo_packageName = T.pack packageName
346343 , _cabalPackageInfo_packageFile = packageFile
347344 , _cabalPackageInfo_packageRoot = takeDirectory packageFile
@@ -353,19 +350,18 @@ parseCabalPackage' pkg = runExceptT $ do
353350 , _cabalPackageInfo_defaultLanguage =
354351 defaultLanguage $ libBuildInfo lib
355352 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
356− , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
357353 }
358− Right Nothing -> pure Nothing
354+ Right Nothing -> throwError "Haskell package has no library component"
359355 Left (_, errors) ->
360356 throwError $ T.pack $ "Failed to parse " <> packageFile <> ":\n" <> unlines (map show errors)
361357
362358parsePackagesOrFail :: (MonadObelisk m, Foldable f) => f FilePath -> m (NE.NonEmpty CabalPackageInfo)
363359parsePackagesOrFail dirs' = do
364− packageInfos' <- fmap catMaybes $ for dirs $ \dir -> do
360+ (pkgDirErrs, packageInfos') <- fmap partitionEithers $ for dirs $ \dir -> do
365361 flip fmap (parseCabalPackage dir) $ \case
366362 Just packageInfo
367− | _cabalPackageInfo_buildable packageInfo -> Just packageInfo
368− _ -> Nothing
363+ | _cabalPackageInfo_buildable packageInfo -> Right packageInfo
364+ _ -> Left dir
369365
370366 -- Sort duplicate packages such that we prefer shorter paths, but fall back to alphabetical ordering.
371367 let packagesByName = Map.map (NE.sortBy $ comparing $ \p -> let n = _cabalPackageInfo_packageFile p in (length n, n))
@@ -387,6 +383,9 @@ parsePackagesOrFail dirs' = do
387383 "No valid, buildable packages found" <> (if null dirs then "" else " in " <> intercalate ", " dirs)
388384 Just xs -> pure xs
389385
386+ unless (null pkgDirErrs) $
387+ putLog Warning $ T.pack $ "Failed to find buildable packages in " <> intercalate ", " pkgDirErrs
388+
390389 pure packageInfos
391390 where
392391 dirs = toList dirs'
@@ -394,29 +393,13 @@ parsePackagesOrFail dirs' = do
394393packageInfoToNamePathMap :: Foldable f => f CabalPackageInfo -> Map Text FilePath
395394packageInfoToNamePathMap = Map.fromList . map (_cabalPackageInfo_packageName &&& _cabalPackageInfo_packageRoot) . toList
396395
397−-- Like 'withGhciScript' but provides the precise ghci arguments to add to a ghci session
398−withGhciScriptArgs
399− :: (MonadObelisk m, Foldable f)
400− => f CabalPackageInfo -- ^ List of packages to load into ghci
401− -> ([String] -> m ()) -- ^ Action to run with the extra ghci arguments
402− -> m ()
403−withGhciScriptArgs packageInfos f = withGhciScript loadPreludeManually packageInfos $ \fp -> f ["-XNoImplicitPrelude", "-ghci-script", fp]
404− where
405− -- These lines must be first and allow the session to support a custom Prelude when @-XNoImplicitPrelude@
406− -- is passed to the ghci session.
407− loadPreludeManually =
408− [ ":add Prelude" -- @:add@ is used because it's less noisy when there is no custom Prelude
409− , ":set -XImplicitPrelude" -- Turn the default setting on
410− ]
411−
412396-- | Create ghci configuration to load the given packages
413397withGhciScript
414398 :: (MonadObelisk m, Foldable f)
415− => [String] -- ^ Commands to prefix to file
416− -> f CabalPackageInfo -- ^ List of packages to load into ghci
399+ => f CabalPackageInfo -- ^ List of packages to load into ghci
417400 -> (FilePath -> m ()) -- ^ Action to run with the path to generated temporary .ghci
418401 -> m ()
419−withGhciScript preCommands (toList -> packageInfos) f =
402+withGhciScript (toList -> packageInfos) f =
420403 withSystemTempDirectory "ob-ghci" $ \fp -> do
421404 let dotGhciPath = fp </> ".ghci"
422405 liftIO $ writeFile dotGhciPath dotGhci
@@ -428,8 +411,7 @@ withGhciScript preCommands (toList -> packageInfos) f =
428411 , [ "Backend" | "backend" `Set.member` packageNames ]
429412 , [ "Frontend" | "frontend" `Set.member` packageNames ]
430413 ]
431− dotGhci = unlines $
432− preCommands <>
414+ dotGhci = unlines
433415 [ if null modulesToLoad then "" else ":load " <> unwords modulesToLoad
434416 , "import qualified Obelisk.Run"
435417 , "import qualified Frontend"
@@ -482,7 +464,7 @@ runGhciRepl root (toList -> packages) ghciArgs =
482464 -- NOTE: We do *not* want to use $(staticWhich "ghci") here because we need the
483465 -- ghc that is provided by the shell in the user's project.
484466 nixShellWithoutPkgs root True False (packageInfoToNamePathMap packages) "ghc" $
485− Just $ unwords $ fmap bashEscape $ "ghci" : ghciArgs
467+ Just $ unwords $ "ghci" : ghciArgs -- TODO: Shell escape
486468
487469-- | Run ghcid
488470runGhcid
@@ -495,28 +477,32 @@ runGhcid
495477 -> m ()
496478runGhcid root chdirToRoot ghciArgs (toList -> packages) mcmd =
497479 nixShellWithoutPkgs root True chdirToRoot (packageInfoToNamePathMap packages) "ghc" $
498− Just $ unwords $ fmap bashEscape $ ghcidExePath : opts
480+ Just $ unwords $ ghcidExePath : opts -- TODO: Shell escape
499481 where
500− opts = concat
501− [ ["-W"]
502− , ["--outputfile=ghcid-output.txt"]
503− , map (\x -> "--reload=" <> x) reloadFiles
504− , map (\x -> "--restart=" <> x) restartFiles
505− , maybe [] (\cmd -> ["--test=" <> cmd]) mcmd
506− , ["--command=" <> unwords ("ghci" : ghciArgs)]
507− ]
482+ opts =
483+ [ "-W"
484+ , "--outputfile=ghcid-output.txt"
485+ ] <> map (\x -> "--reload='" <> x <> "'") reloadFiles
486+ <> map (\x -> "--restart='" <> x <> "'") restartFiles
487+ <> testCmd
488+ <> ["--command='" <> unwords ("ghci" : ghciArgs) <> "'"] -- TODO: Shell escape
489+ testCmd = maybeToList (flip fmap mcmd $ \cmd -> "--test='" <> cmd <> "'") -- TODO: Shell escape
490+
508491 adjustRoot x = if chdirToRoot then makeRelative root x else x
509492 reloadFiles = map adjustRoot [root </> "config"]
510493 restartFiles = map (adjustRoot . _cabalPackageInfo_packageFile) packages
511494
512−getFreePort :: MonadIO m => m Socket.PortNumber
513−getFreePort = liftIO $ Socket.withSocketsDo $ do
514− addr:_ <- Socket.getAddrInfo (Just Socket.defaultHints) (Just "127.0.0.1") (Just "0")
515− bracket (open addr) Socket.close Socket.socketPort
495+mkGhciScriptArg :: FilePath -> [String]
496+mkGhciScriptArg dotGhci = ["-ghci-script", dotGhci]
497+
498+getFreePort :: MonadIO m => m PortNumber
499+getFreePort = liftIO $ withSocketsDo $ do
500+ addr:_ <- getAddrInfo (Just defaultHints) (Just "127.0.0.1") (Just "0")
501+ bracket (open addr) close socketPort
516502 where
517503 open addr = do
518− sock <- Socket.socket (Socket.addrFamily addr) (Socket.addrSocketType addr) (Socket.addrProtocol addr)
519− Socket.bind sock (Socket.addrAddress addr)
504+ sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
505+ bind sock (addrAddress addr)
520506 return sock
521507
522508