@@ -14,24 +14,25 @@ module Obelisk.Command.Run where
1414import Control.Arrow ((&&&))
1515import Control.Exception (Exception, bracket)
1616import Control.Lens (ifor, (.~), (&))
17−import Control.Monad (filterM, unless, void)
17+import Control.Monad (filterM)
1818import Control.Monad.Except (runExceptT, throwError)
1919import Control.Monad.IO.Class (liftIO)
2020import Control.Monad.Reader (MonadIO)
2121import Data.Bifoldable (bifoldr1)
2222import Data.Bifunctor (bimap)
23−import qualified Data.ByteString.UTF8 as BSU
2423import Data.Coerce (coerce)
2524import Data.Default (def)
2625import Data.Either (partitionEithers)
2726import Data.Foldable (fold, for_, toList)
2827import Data.Functor.Identity (runIdentity)
28+import Data.List (intercalate)
2929import Data.List.NonEmpty (NonEmpty)
3030import qualified Data.List.NonEmpty as NE
3131import Data.Map (Map)
3232import qualified Data.Map as Map
3333import qualified Data.Map.Monoidal as MMap
34−import Data.Maybe
34+import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
35+import Data.Ord (comparing)
3536import Data.Set (Set)
3637import qualified Data.Set as Set
3738import Data.String.Here.Interpolated (i)
@@ -45,40 +46,38 @@ import Distribution.Compiler (CompilerFlavor(..))
4546import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
4647import Distribution.Parsec.ParseResult (runParseResult)
4748import qualified Distribution.System as Dist
48−import Distribution.Types.BuildInfo
49−import Distribution.Types.CondTree
50−import Distribution.Types.GenericPackageDescription
51−import Distribution.Types.Library
52−import Distribution.Utils.Generic
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)
5354import qualified Distribution.Parsec.Common as Dist
5455import qualified Hpack.Config as Hpack
5556import qualified Hpack.Render as Hpack
5657import qualified Hpack.Yaml as Hpack
57−import Language.Haskell.Extension
58−import Network.Socket hiding (Debug)
58+import Language.Haskell.Extension (Extension, Language)
59+import qualified Network.Socket as Socket
5960import System.Directory
6061import System.Environment (getExecutablePath)
6162import System.FilePath
6263import qualified System.Info
6364import System.IO.Temp (withSystemTempDirectory)
64−import Text.ShellEscape (Bash, bash)
6565
6666import Obelisk.App (MonadObelisk)
6767import Obelisk.CliApp (
6868 Severity (..),
69− createProcess_,
7069 failWith,
7170 proc,
7271 putLog,
7372 readCreateProcessWithExitCode,
7473 readProcessAndLogStderr,
74+ runProcess_,
7575 setCwd,
7676 setDelegateCtlc,
77− waitForProcess,
7877 withSpinner,
7978 )
8079import Obelisk.Command.Nix
81−import Obelisk.Command.Project (nixShellWithoutPkgs, withProjectRoot, findProjectAssets)
80+import Obelisk.Command.Project (nixShellWithoutPkgs, withProjectRoot, findProjectAssets, bashEscape)
8281import Obelisk.Command.Thunk (attrCacheFileName)
8382import Obelisk.Command.Utils (findExePath, ghcidExePath)
8483
@@ -95,9 +94,11 @@ data CabalPackageInfo = CabalPackageInfo
9594 -- ^ List of globally set languages of the library component
9695 , _cabalPackageInfo_compilerOptions :: [(CompilerFlavor, [String])]
9796 -- ^ 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)
9899 }
99100
100−-- | 'Bool' with a better name for it's purpose.
101+-- | 'Bool' with a better name for its purpose.
101102data Interpret = Interpret_Interpret | Interpret_NoInterpret deriving (Eq, Ord, Show)
102103
103104textInterpret :: Interpret -> Text
@@ -111,9 +112,6 @@ data PathTree a = PathTree_Node
111112 (Map FilePath (PathTree a)) -- Branches to deeper leaves
112113 deriving (Eq, Ord, Show, Functor, Foldable, Traversable)
113114
114−emptyPathTree :: PathTree a
115−emptyPathTree = PathTree_Node Nothing mempty
116−
117115-- | 2D ASCII drawing of a 'PathTree'. Adapted from Data.Tree.draw.
118116drawPathTree :: (a -> Text) -> PathTree a -> Text
119117drawPathTree showA (PathTree_Node _ ts0) = T.intercalate "\n" $ goForest (Map.toList ts0)
@@ -153,7 +151,7 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
153151 liftIO $ createDirectoryIfMissing True $ takeDirectory $ root </> profileBaseName
154152 putLog Debug $ "Storing profiled data under base name of " <> T.pack (root </> profileBaseName)
155153 freePort <- getFreePort
156− (_, _, _, ph) <- createProcess_ "runProfExe" $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
154+ runProcess_ $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
157155 [ show freePort
158156 , T.unpack assets
159157 , profileBaseName
@@ -161,17 +159,16 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
161159 , "-po" <> profileBaseName
162160 ] <> rtsFlags
163161 <> [ "-RTS" ]
164− void $ waitForProcess ph
165162
166163run :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
167164run root interpretPaths = do
168165 pkgs <- getParsedLocalPkgs root interpretPaths
169− withGhciScript pkgs $ \dotGhciPath -> do
170− assets <- findProjectAssets root
171− putLog Debug $ "Assets impurely loaded from: " <> assets
172− ghciArgs <- getGhciSessionSettings pkgs root True
173− freePort <- getFreePort
174− runGhcid root True (ghciArgs <> mkGhciScriptArg dotGhciPath) pkgs $ Just $ unwords
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
175172 [ "Obelisk.Run.run"
176173 , show freePort
177174 , "(Obelisk.Run.runServeAsset " ++ show assets ++ ")"
@@ -183,22 +180,22 @@ runRepl :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
183180runRepl root interpretPaths = do
184181 pkgs <- getParsedLocalPkgs root interpretPaths
185182 ghciArgs <- getGhciSessionSettings pkgs "." True
186− withGhciScript pkgs $ \dotGhciPath ->
187− runGhciRepl root pkgs (ghciArgs <> mkGhciScriptArg dotGhciPath)
183+ withGhciScriptArgs pkgs $ \dotGhciArgs ->
184+ runGhciRepl root pkgs (ghciArgs <> dotGhciArgs)
188185
189186runWatch :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
190187runWatch root interpretPaths = do
191188 pkgs <- getParsedLocalPkgs root interpretPaths
192189 ghciArgs <- getGhciSessionSettings pkgs root True
193− withGhciScript pkgs $ \dotGhciPath ->
194− runGhcid root True (ghciArgs <> mkGhciScriptArg dotGhciPath) pkgs Nothing
190+ withGhciScriptArgs pkgs $ \dotGhciArgs ->
191+ runGhcid root True (ghciArgs <> dotGhciArgs) pkgs Nothing
195192
196193exportGhciConfig :: MonadObelisk m => Bool -> FilePath -> PathTree Interpret -> m [String]
197194exportGhciConfig useRelativePaths root interpretPaths = do
198195 pkgs <- getParsedLocalPkgs root interpretPaths
199196 getGhciSessionSettings pkgs "." useRelativePaths
200197
201−nixShellForInterpretPaths :: MonadObelisk m => Bool -> String -> FilePath -> PathTree Interpret -> Maybe [Bash] -> m ()
198+nixShellForInterpretPaths :: MonadObelisk m => Bool -> String -> FilePath -> PathTree Interpret -> Maybe String -> m ()
202199nixShellForInterpretPaths isPure shell root interpretPaths cmd = do
203200 pkgs <- getParsedLocalPkgs root interpretPaths
204201 nixShellWithoutPkgs root isPure False (packageInfoToNamePathMap pkgs) shell cmd
@@ -307,15 +304,16 @@ parseCabalPackage
307304 -> m (Maybe CabalPackageInfo)
308305parseCabalPackage dir = parseCabalPackage' dir >>= \case
309306 Left err -> Nothing <$ putLog Error err
310− Right (warnings, pkgInfo) -> do
307+ Right (Just (warnings, pkgInfo)) -> do
311308 for_ warnings $ putLog Warning . T.pack . show
312309 pure $ Just pkgInfo
310+ Right Nothing -> pure Nothing
313311
314312-- | Like 'parseCabalPackage' but returns errors and warnings directly so as to avoid 'MonadObelisk'.
315313parseCabalPackage'
316314 :: (MonadIO m)
317315 => FilePath -- ^ Package directory
318− -> m (Either T.Text ([Dist.PWarning], CabalPackageInfo))
316+ -> m (Either T.Text (Maybe ([Dist.PWarning], CabalPackageInfo)))
319317parseCabalPackage' pkg = runExceptT $ do
320318 (cabalContents, packageFile, packageName) <- guessCabalPackageFile pkg >>= \case
321319 Left GuessPackageFileError_NotFound -> throwError $ "No .cabal or package.yaml file found in " <> T.pack pkg
@@ -343,7 +341,7 @@ parseCabalPackage' pkg = runExceptT $ do
343341 case condLibrary <$> result of
344342 Right (Just condLib) -> do
345343 let (_, lib) = simplifyCondTree evalConfVar condLib
346− pure $ (warnings,) $ CabalPackageInfo
344+ pure $ Just $ (warnings,) $ CabalPackageInfo
347345 { _cabalPackageInfo_packageName = T.pack packageName
348346 , _cabalPackageInfo_packageFile = packageFile
349347 , _cabalPackageInfo_packageRoot = takeDirectory packageFile
@@ -355,18 +353,19 @@ parseCabalPackage' pkg = runExceptT $ do
355353 , _cabalPackageInfo_defaultLanguage =
356354 defaultLanguage $ libBuildInfo lib
357355 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
356+ , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
358357 }
359− Right Nothing -> throwError "Haskell package has no library component"
358+ Right Nothing -> pure Nothing
360359 Left (_, errors) ->
361360 throwError $ T.pack $ "Failed to parse " <> packageFile <> ":\n" <> unlines (map show errors)
362361
363362parsePackagesOrFail :: (MonadObelisk m, Foldable f) => f FilePath -> m (NE.NonEmpty CabalPackageInfo)
364363parsePackagesOrFail dirs' = do
365− (pkgDirErrs, packageInfos') <- fmap partitionEithers $ for dirs $ \dir -> do
364+ packageInfos' <- fmap catMaybes $ for dirs $ \dir -> do
366365 flip fmap (parseCabalPackage dir) $ \case
367366 Just packageInfo
368− | _cabalPackageInfo_buildable packageInfo -> Right packageInfo
369− _ -> Left dir
367+ | _cabalPackageInfo_buildable packageInfo -> Just packageInfo
368+ _ -> Nothing
370369
371370 -- Sort duplicate packages such that we prefer shorter paths, but fall back to alphabetical ordering.
372371 let packagesByName = Map.map (NE.sortBy $ comparing $ \p -> let n = _cabalPackageInfo_packageFile p in (length n, n))
@@ -388,9 +387,6 @@ parsePackagesOrFail dirs' = do
388387 "No valid, buildable packages found" <> (if null dirs then "" else " in " <> intercalate ", " dirs)
389388 Just xs -> pure xs
390389
391− unless (null pkgDirErrs) $
392− putLog Warning $ T.pack $ "Failed to find buildable packages in " <> intercalate ", " pkgDirErrs
393−
394390 pure packageInfos
395391 where
396392 dirs = toList dirs'
@@ -398,13 +394,29 @@ parsePackagesOrFail dirs' = do
398394packageInfoToNamePathMap :: Foldable f => f CabalPackageInfo -> Map Text FilePath
399395packageInfoToNamePathMap = Map.fromList . map (_cabalPackageInfo_packageName &&& _cabalPackageInfo_packageRoot) . toList
400396
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+
401412-- | Create ghci configuration to load the given packages
402413withGhciScript
403414 :: (MonadObelisk m, Foldable f)
404− => f CabalPackageInfo -- ^ List of packages to load into ghci
415+ => [String] -- ^ Commands to prefix to file
416+ -> f CabalPackageInfo -- ^ List of packages to load into ghci
405417 -> (FilePath -> m ()) -- ^ Action to run with the path to generated temporary .ghci
406418 -> m ()
407−withGhciScript (toList -> packageInfos) f =
419+withGhciScript preCommands (toList -> packageInfos) f =
408420 withSystemTempDirectory "ob-ghci" $ \fp -> do
409421 let dotGhciPath = fp </> ".ghci"
410422 liftIO $ writeFile dotGhciPath dotGhci
@@ -416,7 +428,8 @@ withGhciScript (toList -> packageInfos) f =
416428 , [ "Backend" | "backend" `Set.member` packageNames ]
417429 , [ "Frontend" | "frontend" `Set.member` packageNames ]
418430 ]
419− dotGhci = unlines
431+ dotGhci = unlines $
432+ preCommands <>
420433 [ if null modulesToLoad then "" else ":load " <> unwords modulesToLoad
421434 , "import qualified Obelisk.Run"
422435 , "import qualified Frontend"
@@ -469,7 +482,7 @@ runGhciRepl root (toList -> packages) ghciArgs =
469482 -- NOTE: We do *not* want to use $(staticWhich "ghci") here because we need the
470483 -- ghc that is provided by the shell in the user's project.
471484 nixShellWithoutPkgs root True False (packageInfoToNamePathMap packages) "ghc" $
472− Just $ fmap (bash . BSU.fromString) $ "ghci" : ghciArgs
485+ Just $ unwords $ fmap bashEscape $ "ghci" : ghciArgs
473486
474487-- | Run ghcid
475488runGhcid
@@ -482,7 +495,7 @@ runGhcid
482495 -> m ()
483496runGhcid root chdirToRoot ghciArgs (toList -> packages) mcmd =
484497 nixShellWithoutPkgs root True chdirToRoot (packageInfoToNamePathMap packages) "ghc" $
485− Just $ fmap (bash . BSU.fromString) $ ghcidExePath : opts
498+ Just $ unwords $ fmap bashEscape $ ghcidExePath : opts
486499 where
487500 opts = concat
488501 [ ["-W"]
@@ -496,17 +509,14 @@ runGhcid root chdirToRoot ghciArgs (toList -> packages) mcmd =
496509 reloadFiles = map adjustRoot [root </> "config"]
497510 restartFiles = map (adjustRoot . _cabalPackageInfo_packageFile) packages
498511
499−mkGhciScriptArg :: FilePath -> [String]
500−mkGhciScriptArg dotGhci = ["-ghci-script", dotGhci]
501−
502−getFreePort :: MonadIO m => m PortNumber
503−getFreePort = liftIO $ withSocketsDo $ do
504− addr:_ <- getAddrInfo (Just defaultHints) (Just "127.0.0.1") (Just "0")
505− bracket (open addr) close socketPort
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
506516 where
507517 open addr = do
508− sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
509− bind sock (addrAddress addr)
518+ sock <- Socket.socket (Socket.addrFamily addr) (Socket.addrSocketType addr) (Socket.addrProtocol addr)
519+ Socket.bind sock (Socket.addrAddress addr)
510520 return sock
511521
512522