@@ -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
@@ -112,6 +109,9 @@ data PathTree a = PathTree_Node
112109 (Map FilePath (PathTree a)) -- Branches to deeper leaves
113110 deriving (Eq, Ord, Show, Functor, Foldable, Traversable)
114111
112+emptyPathTree :: PathTree a
113+emptyPathTree = PathTree_Node Nothing mempty
114+
115115-- | 2D ASCII drawing of a 'PathTree'. Adapted from Data.Tree.draw.
116116drawPathTree :: (a -> Text) -> PathTree a -> Text
117117drawPathTree showA (PathTree_Node _ ts0) = T.intercalate "\n" $ goForest (Map.toList ts0)
@@ -151,7 +151,7 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
151151 liftIO $ createDirectoryIfMissing True $ takeDirectory $ root </> profileBaseName
152152 putLog Debug $ "Storing profiled data under base name of " <> T.pack (root </> profileBaseName)
153153 freePort <- getFreePort
154− runProcess_ $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
154+ (_, _, _, ph) <- createProcess_ "runProfExe" $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
155155 [ show freePort
156156 , T.unpack assets
157157 , profileBaseName
@@ -159,16 +159,17 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
159159 , "-po" <> profileBaseName
160160 ] <> rtsFlags
161161 <> [ "-RTS" ]
162+ void $ waitForProcess ph
162163
163164run :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
164165run root interpretPaths = do
165166 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
167+ withGhciScript pkgs $ \dotGhciPath -> do
168+ assets <- findProjectAssets root
169+ putLog Debug $ "Assets impurely loaded from: " <> assets
170+ ghciArgs <- getGhciSessionSettings pkgs root
171+ freePort <- getFreePort
172+ runGhcid root True (ghciArgs <> mkGhciScriptArg dotGhciPath) pkgs $ Just $ unwords
172173 [ "Obelisk.Run.run"
173174 , show freePort
174175 , "(Obelisk.Run.runServeAsset " ++ show assets ++ ")"
@@ -179,21 +180,21 @@ run root interpretPaths = do
179180runRepl :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
180181runRepl root interpretPaths = do
181182 pkgs <- getParsedLocalPkgs root interpretPaths
182− ghciArgs <- getGhciSessionSettings pkgs "." True
183− withGhciScriptArgs pkgs $ \dotGhciArgs ->
184− runGhciRepl root pkgs (ghciArgs <> dotGhciArgs)
183+ ghciArgs <- getGhciSessionSettings pkgs "."
184+ withGhciScript pkgs $ \dotGhciPath ->
185+ runGhciRepl root pkgs (ghciArgs <> mkGhciScriptArg dotGhciPath)
185186
186187runWatch :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
187188runWatch root interpretPaths = do
188189 pkgs <- getParsedLocalPkgs root interpretPaths
189− ghciArgs <- getGhciSessionSettings pkgs root True
190− withGhciScriptArgs pkgs $ \dotGhciArgs ->
191− runGhcid root True (ghciArgs <> dotGhciArgs) pkgs Nothing
190+ ghciArgs <- getGhciSessionSettings pkgs root
191+ withGhciScript pkgs $ \dotGhciPath ->
192+ runGhcid root True (ghciArgs <> mkGhciScriptArg dotGhciPath) pkgs Nothing
192193
193−exportGhciConfig :: MonadObelisk m => Bool -> FilePath -> PathTree Interpret -> m [String]
194−exportGhciConfig useRelativePaths root interpretPaths = do
194+exportGhciConfig :: MonadObelisk m => FilePath -> PathTree Interpret -> m [String]
195+exportGhciConfig root interpretPaths = do
195196 pkgs <- getParsedLocalPkgs root interpretPaths
196− getGhciSessionSettings pkgs "." useRelativePaths
197+ getGhciSessionSettings pkgs "."
197198
198199nixShellForInterpretPaths :: MonadObelisk m => Bool -> String -> FilePath -> PathTree Interpret -> Maybe String -> m ()
199200nixShellForInterpretPaths isPure shell root interpretPaths cmd = do
@@ -304,16 +305,15 @@ parseCabalPackage
304305 -> m (Maybe CabalPackageInfo)
305306parseCabalPackage dir = parseCabalPackage' dir >>= \case
306307 Left err -> Nothing <$ putLog Error err
307− Right (Just (warnings, pkgInfo)) -> do
308+ Right (warnings, pkgInfo) -> do
308309 for_ warnings $ putLog Warning . T.pack . show
309310 pure $ Just pkgInfo
310− Right Nothing -> pure Nothing
311311
312312-- | Like 'parseCabalPackage' but returns errors and warnings directly so as to avoid 'MonadObelisk'.
313313parseCabalPackage'
314314 :: (MonadIO m)
315315 => FilePath -- ^ Package directory
316− -> m (Either T.Text (Maybe ([Dist.PWarning], CabalPackageInfo)))
316+ -> m (Either T.Text ([Dist.PWarning], CabalPackageInfo))
317317parseCabalPackage' pkg = runExceptT $ do
318318 (cabalContents, packageFile, packageName) <- guessCabalPackageFile pkg >>= \case
319319 Left GuessPackageFileError_NotFound -> throwError $ "No .cabal or package.yaml file found in " <> T.pack pkg
@@ -341,7 +341,7 @@ parseCabalPackage' pkg = runExceptT $ do
341341 case condLibrary <$> result of
342342 Right (Just condLib) -> do
343343 let (_, lib) = simplifyCondTree evalConfVar condLib
344− pure $ Just $ (warnings,) $ CabalPackageInfo
344+ pure $ (warnings,) $ CabalPackageInfo
345345 { _cabalPackageInfo_packageName = T.pack packageName
346346 , _cabalPackageInfo_packageFile = packageFile
347347 , _cabalPackageInfo_packageRoot = takeDirectory packageFile
@@ -353,19 +353,18 @@ parseCabalPackage' pkg = runExceptT $ do
353353 , _cabalPackageInfo_defaultLanguage =
354354 defaultLanguage $ libBuildInfo lib
355355 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
356− , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
357356 }
358− Right Nothing -> pure Nothing
357+ Right Nothing -> throwError "Haskell package has no library component"
359358 Left (_, errors) ->
360359 throwError $ T.pack $ "Failed to parse " <> packageFile <> ":\n" <> unlines (map show errors)
361360
362361parsePackagesOrFail :: (MonadObelisk m, Foldable f) => f FilePath -> m (NE.NonEmpty CabalPackageInfo)
363362parsePackagesOrFail dirs' = do
364− packageInfos' <- fmap catMaybes $ for dirs $ \dir -> do
363+ (pkgDirErrs, packageInfos') <- fmap partitionEithers $ for dirs $ \dir -> do
365364 flip fmap (parseCabalPackage dir) $ \case
366365 Just packageInfo
367− | _cabalPackageInfo_buildable packageInfo -> Just packageInfo
368− _ -> Nothing
366+ | _cabalPackageInfo_buildable packageInfo -> Right packageInfo
367+ _ -> Left dir
369368
370369 -- Sort duplicate packages such that we prefer shorter paths, but fall back to alphabetical ordering.
371370 let packagesByName = Map.map (NE.sortBy $ comparing $ \p -> let n = _cabalPackageInfo_packageFile p in (length n, n))
@@ -387,6 +386,9 @@ parsePackagesOrFail dirs' = do
387386 "No valid, buildable packages found" <> (if null dirs then "" else " in " <> intercalate ", " dirs)
388387 Just xs -> pure xs
389388
389+ unless (null pkgDirErrs) $
390+ putLog Warning $ T.pack $ "Failed to find buildable packages in " <> intercalate ", " pkgDirErrs
391+
390392 pure packageInfos
391393 where
392394 dirs = toList dirs'
@@ -394,29 +396,13 @@ parsePackagesOrFail dirs' = do
394396packageInfoToNamePathMap :: Foldable f => f CabalPackageInfo -> Map Text FilePath
395397packageInfoToNamePathMap = Map.fromList . map (_cabalPackageInfo_packageName &&& _cabalPackageInfo_packageRoot) . toList
396398
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−
412399-- | Create ghci configuration to load the given packages
413400withGhciScript
414401 :: (MonadObelisk m, Foldable f)
415− => [String] -- ^ Commands to prefix to file
416− -> f CabalPackageInfo -- ^ List of packages to load into ghci
402+ => f CabalPackageInfo -- ^ List of packages to load into ghci
417403 -> (FilePath -> m ()) -- ^ Action to run with the path to generated temporary .ghci
418404 -> m ()
419−withGhciScript preCommands (toList -> packageInfos) f =
405+withGhciScript (toList -> packageInfos) f =
420406 withSystemTempDirectory "ob-ghci" $ \fp -> do
421407 let dotGhciPath = fp </> ".ghci"
422408 liftIO $ writeFile dotGhciPath dotGhci
@@ -428,8 +414,7 @@ withGhciScript preCommands (toList -> packageInfos) f =
428414 , [ "Backend" | "backend" `Set.member` packageNames ]
429415 , [ "Frontend" | "frontend" `Set.member` packageNames ]
430416 ]
431− dotGhci = unlines $
432− preCommands <>
417+ dotGhci = unlines
433418 [ if null modulesToLoad then "" else ":load " <> unwords modulesToLoad
434419 , "import qualified Obelisk.Run"
435420 , "import qualified Frontend"
@@ -442,9 +427,8 @@ getGhciSessionSettings
442427 :: (MonadObelisk m, Foldable f)
443428 => f CabalPackageInfo -- ^ List of packages to load into ghci
444429 -> FilePath -- ^ All paths will be relative to this path
445− -> Bool -- ^ Use relative paths
446430 -> m [String]
447−getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
431+getGhciSessionSettings (toList -> packageInfos) pathBase = do
448432 -- N.B. ghci settings do NOT support escaping in any way. To minimize the likelihood that
449433 -- paths-with-spaces ruin our day, we first canonicalize everything, and then relativize
450434 -- all paths to 'pathBase'.
@@ -454,15 +438,13 @@ getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
454438 (pkgFiles, pkgSrcPaths :: [NonEmpty FilePath]) <- fmap unzip $ liftIO $ for packageInfos $ \pkg -> do
455439 canonicalSrcDirs <- traverse canonicalizePath $ (_cabalPackageInfo_packageRoot pkg </>) <$> _cabalPackageInfo_sourceDirs pkg
456440 canonicalPkgFile <- canonicalizePath $ _cabalPackageInfo_packageFile pkg
457− pure (canonicalPkgFile `relativeTo'` canonicalPathBase, (`relativeTo'` canonicalPathBase) <$> canonicalSrcDirs)
441+ pure (canonicalPkgFile `relativeTo` canonicalPathBase, (`relativeTo` canonicalPathBase) <$> canonicalSrcDirs)
458442
459443 pure
460444 $ baseGhciOptions
461445 <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
462446 <> concatMap (\p -> ["-optF", p]) pkgFiles
463447 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]
464− where
465− relativeTo' = if useRelativePaths then relativeTo else const
466448
467449baseGhciOptions :: [String]
468450baseGhciOptions =
@@ -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
@@ -551,4 +537,4 @@ relativeTo dir base
551537 $ zipDefaultWith Nothing Nothing (,)
552538 (map Just $ splitDirectories base)
553539 (map Just $ splitDirectories dir)
554− where collapse = foldr (</>) ""
540+ where collapse = foldr (</>) ""