@@ -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, unless, void)
17+import Control.Monad (filterM)
1818import Control.Monad.Except (runExceptT, throwError)
1919import Control.Monad.IO.Class (liftIO)
2020import Control.Monad.Reader (MonadIO)
@@ -25,12 +25,14 @@ import Data.Default (def)
2525import Data.Either (partitionEithers)
2626import Data.Foldable (fold, for_, toList)
2727import Data.Functor.Identity (runIdentity)
28+import Data.List (intercalate)
2829import Data.List.NonEmpty (NonEmpty)
2930import qualified Data.List.NonEmpty as NE
3031import Data.Map (Map)
3132import qualified Data.Map as Map
3233import qualified Data.Map.Monoidal as MMap
33−import Data.Maybe
34+import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
35+import Data.Ord (comparing)
3436import Data.Set (Set)
3537import qualified Data.Set as Set
3638import Data.String.Here.Interpolated (i)
@@ -44,17 +46,17 @@ import Distribution.Compiler (CompilerFlavor(..))
4446import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
4547import Distribution.Parsec.ParseResult (runParseResult)
4648import qualified Distribution.System as Dist
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
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)
5254import qualified Distribution.Parsec.Common as Dist
5355import qualified Hpack.Config as Hpack
5456import qualified Hpack.Render as Hpack
5557import qualified Hpack.Yaml as Hpack
56−import Language.Haskell.Extension
57−import Network.Socket hiding (Debug)
58+import Language.Haskell.Extension (Extension, Language)
59+import qualified Network.Socket as Socket
5860import System.Directory
5961import System.Environment (getExecutablePath)
6062import System.FilePath
@@ -64,19 +66,18 @@ import System.IO.Temp (withSystemTempDirectory)
6466import Obelisk.App (MonadObelisk)
6567import Obelisk.CliApp (
6668 Severity (..),
67− createProcess_,
6869 failWith,
6970 proc,
7071 putLog,
7172 readCreateProcessWithExitCode,
7273 readProcessAndLogStderr,
74+ runProcess_,
7375 setCwd,
7476 setDelegateCtlc,
75− waitForProcess,
7677 withSpinner,
7778 )
7879import Obelisk.Command.Nix
79−import Obelisk.Command.Project (nixShellWithPkgs, withProjectRoot, findProjectAssets)
80+import Obelisk.Command.Project (nixShellWithoutPkgs, withProjectRoot, findProjectAssets, bashEscape)
8081import Obelisk.Command.Thunk (attrCacheFileName)
8182import Obelisk.Command.Utils (findExePath, ghcidExePath)
8283
@@ -93,9 +94,11 @@ data CabalPackageInfo = CabalPackageInfo
9394 -- ^ List of globally set languages of the library component
9495 , _cabalPackageInfo_compilerOptions :: [(CompilerFlavor, [String])]
9596 -- ^ 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)
9699 }
97100
98−-- | 'Bool' with a better name for it's purpose.
101+-- | 'Bool' with a better name for its purpose.
99102data Interpret = Interpret_Interpret | Interpret_NoInterpret deriving (Eq, Ord, Show)
100103
101104textInterpret :: Interpret -> Text
@@ -109,9 +112,6 @@ data PathTree a = PathTree_Node
109112 (Map FilePath (PathTree a)) -- Branches to deeper leaves
110113 deriving (Eq, Ord, Show, Functor, Foldable, Traversable)
111114
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− (_, _, _, 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") $
155155 [ show freePort
156156 , T.unpack assets
157157 , profileBaseName
@@ -159,16 +159,16 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
159159 , "-po" <> profileBaseName
160160 ] <> rtsFlags
161161 <> [ "-RTS" ]
162− void $ waitForProcess ph
163162
164163run :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
165164run root interpretPaths = do
166165 pkgs <- getParsedLocalPkgs root interpretPaths
167− withGhciScript pkgs root $ \dotGhciPath -> do
168− freePort <- getFreePort
169− assets <- findProjectAssets root
170− putLog Debug $ "Assets impurely loaded from: " <> assets
171− runGhcid root True 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
172172 [ "Obelisk.Run.run"
173173 , show freePort
174174 , "(Obelisk.Run.runServeAsset " ++ show assets ++ ")"
@@ -179,24 +179,26 @@ run root interpretPaths = do
179179runRepl :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
180180runRepl root interpretPaths = do
181181 pkgs <- getParsedLocalPkgs root interpretPaths
182− withGhciScript pkgs "." $ \dotGhciPath ->
183− runGhciRepl root pkgs dotGhciPath
182+ ghciArgs <- getGhciSessionSettings pkgs "." True
183+ withGhciScriptArgs pkgs $ \dotGhciArgs ->
184+ runGhciRepl root pkgs (ghciArgs <> dotGhciArgs)
184185
185186runWatch :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
186187runWatch root interpretPaths = do
187188 pkgs <- getParsedLocalPkgs root interpretPaths
188− withGhciScript pkgs root $ \dotGhciPath ->
189− runGhcid root True dotGhciPath pkgs Nothing
189+ ghciArgs <- getGhciSessionSettings pkgs root True
190+ withGhciScriptArgs pkgs $ \dotGhciArgs ->
191+ runGhcid root True (ghciArgs <> dotGhciArgs) pkgs Nothing
190192
191−exportGhciConfig :: MonadObelisk m => FilePath -> PathTree Interpret -> m [String]
192−exportGhciConfig root interpretPaths = do
193+exportGhciConfig :: MonadObelisk m => Bool -> FilePath -> PathTree Interpret -> m [String]
194+exportGhciConfig useRelativePaths root interpretPaths = do
193195 pkgs <- getParsedLocalPkgs root interpretPaths
194− getGhciSessionSettings pkgs "."
196+ getGhciSessionSettings pkgs "." useRelativePaths
195197
196198nixShellForInterpretPaths :: MonadObelisk m => Bool -> String -> FilePath -> PathTree Interpret -> Maybe String -> m ()
197199nixShellForInterpretPaths isPure shell root interpretPaths cmd = do
198200 pkgs <- getParsedLocalPkgs root interpretPaths
199− nixShellWithPkgs root isPure False (packageInfoToNamePathMap pkgs) shell cmd
201+ nixShellWithoutPkgs root isPure False (packageInfoToNamePathMap pkgs) shell cmd
200202
201203-- | Like 'getLocalPkgs' but also parses them and fails if any of them can't be parsed.
202204getParsedLocalPkgs :: MonadObelisk m => FilePath -> PathTree Interpret -> m (NonEmpty CabalPackageInfo)
@@ -302,15 +304,16 @@ parseCabalPackage
302304 -> m (Maybe CabalPackageInfo)
303305parseCabalPackage dir = parseCabalPackage' dir >>= \case
304306 Left err -> Nothing <$ putLog Error err
305− Right (warnings, pkgInfo) -> do
307+ Right (Just (warnings, pkgInfo)) -> do
306308 for_ warnings $ putLog Warning . T.pack . show
307309 pure $ Just pkgInfo
310+ Right Nothing -> pure Nothing
308311
309312-- | Like 'parseCabalPackage' but returns errors and warnings directly so as to avoid 'MonadObelisk'.
310313parseCabalPackage'
311314 :: (MonadIO m)
312315 => FilePath -- ^ Package directory
313− -> m (Either T.Text ([Dist.PWarning], CabalPackageInfo))
316+ -> m (Either T.Text (Maybe ([Dist.PWarning], CabalPackageInfo)))
314317parseCabalPackage' pkg = runExceptT $ do
315318 (cabalContents, packageFile, packageName) <- guessCabalPackageFile pkg >>= \case
316319 Left GuessPackageFileError_NotFound -> throwError $ "No .cabal or package.yaml file found in " <> T.pack pkg
@@ -338,7 +341,7 @@ parseCabalPackage' pkg = runExceptT $ do
338341 case condLibrary <$> result of
339342 Right (Just condLib) -> do
340343 let (_, lib) = simplifyCondTree evalConfVar condLib
341− pure $ (warnings,) $ CabalPackageInfo
344+ pure $ Just $ (warnings,) $ CabalPackageInfo
342345 { _cabalPackageInfo_packageName = T.pack packageName
343346 , _cabalPackageInfo_packageFile = packageFile
344347 , _cabalPackageInfo_packageRoot = takeDirectory packageFile
@@ -350,18 +353,19 @@ parseCabalPackage' pkg = runExceptT $ do
350353 , _cabalPackageInfo_defaultLanguage =
351354 defaultLanguage $ libBuildInfo lib
352355 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
356+ , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
353357 }
354− Right Nothing -> throwError "Haskell package has no library component"
358+ Right Nothing -> pure Nothing
355359 Left (_, errors) ->
356360 throwError $ T.pack $ "Failed to parse " <> packageFile <> ":\n" <> unlines (map show errors)
357361
358362parsePackagesOrFail :: (MonadObelisk m, Foldable f) => f FilePath -> m (NE.NonEmpty CabalPackageInfo)
359363parsePackagesOrFail dirs' = do
360− (pkgDirErrs, packageInfos') <- fmap partitionEithers $ for dirs $ \dir -> do
364+ packageInfos' <- fmap catMaybes $ for dirs $ \dir -> do
361365 flip fmap (parseCabalPackage dir) $ \case
362366 Just packageInfo
363− | _cabalPackageInfo_buildable packageInfo -> Right packageInfo
364− _ -> Left dir
367+ | _cabalPackageInfo_buildable packageInfo -> Just packageInfo
368+ _ -> Nothing
365369
366370 -- Sort duplicate packages such that we prefer shorter paths, but fall back to alphabetical ordering.
367371 let packagesByName = Map.map (NE.sortBy $ comparing $ \p -> let n = _cabalPackageInfo_packageFile p in (length n, n))
@@ -383,9 +387,6 @@ parsePackagesOrFail dirs' = do
383387 "No valid, buildable packages found" <> (if null dirs then "" else " in " <> intercalate ", " dirs)
384388 Just xs -> pure xs
385389
386− unless (null pkgDirErrs) $
387− putLog Warning $ T.pack $ "Failed to find buildable packages in " <> intercalate ", " pkgDirErrs
388−
389390 pure packageInfos
390391 where
391392 dirs = toList dirs'
@@ -393,41 +394,57 @@ parsePackagesOrFail dirs' = do
393394packageInfoToNamePathMap :: Foldable f => f CabalPackageInfo -> Map Text FilePath
394395packageInfoToNamePathMap = Map.fromList . map (_cabalPackageInfo_packageName &&& _cabalPackageInfo_packageRoot) . toList
395396
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+
396412-- | Create ghci configuration to load the given packages
397413withGhciScript
398414 :: (MonadObelisk m, Foldable f)
399− => f CabalPackageInfo -- ^ List of packages to load into ghci
400− -> FilePath -- ^ All paths written to the .ghci file will be relative to this path
415+ => [String] -- ^ Commands to prefix to file
416+ -> f CabalPackageInfo -- ^ List of packages to load into ghci
401417 -> (FilePath -> m ()) -- ^ Action to run with the path to generated temporary .ghci
402418 -> m ()
403−withGhciScript (toList -> packageInfos) pathBase f = do
404− ghciSettings <- getGhciSessionSettings packageInfos pathBase
405− let
419+withGhciScript preCommands (toList -> packageInfos) f =
420+ withSystemTempDirectory "ob-ghci" $ \fp -> do
421+ let dotGhciPath = fp </> ".ghci"
422+ liftIO $ writeFile dotGhciPath dotGhci
423+ f dotGhciPath
424+ where
406425 packageNames = Set.fromList $ map _cabalPackageInfo_packageName packageInfos
407426 modulesToLoad = mconcat
408427 [ [ "Obelisk.Run" | "obelisk-run" `Set.member` packageNames ]
409428 , [ "Backend" | "backend" `Set.member` packageNames ]
410429 , [ "Frontend" | "frontend" `Set.member` packageNames ]
411430 ]
412− dotGhci = unlines
413− [ ":set " <> unwords ghciSettings -- TODO: Shell escape
414− , if null modulesToLoad then "" else ":load " <> unwords modulesToLoad
431+ dotGhci = unlines $
432+ preCommands <>
433+ [ if null modulesToLoad then "" else ":load " <> unwords modulesToLoad
415434 , "import qualified Obelisk.Run"
416435 , "import qualified Frontend"
417− , "import qualified Backend" ]
418− withSystemTempDirectory "ob-ghci" $ \fp -> do
419− let dotGhciPath = fp </> ".ghci"
420− liftIO $ writeFile dotGhciPath dotGhci
421− f dotGhciPath
436+ , "import qualified Backend"
437+ ]
422438
423439-- | Builds a list of options to pass to ghci or set in .ghci file that configures
424440-- the preprocessor and source includes.
425441getGhciSessionSettings
426442 :: (MonadObelisk m, Foldable f)
427443 => f CabalPackageInfo -- ^ List of packages to load into ghci
428− -> FilePath -- ^ All paths written to the .ghci file will be relative to this path
444+ -> FilePath -- ^ All paths will be relative to this path
445+ -> Bool -- ^ Use relative paths
429446 -> m [String]
430−getGhciSessionSettings (toList -> packageInfos) pathBase = do
447+getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
431448 -- N.B. ghci settings do NOT support escaping in any way. To minimize the likelihood that
432449 -- paths-with-spaces ruin our day, we first canonicalize everything, and then relativize
433450 -- all paths to 'pathBase'.
@@ -437,66 +454,69 @@ getGhciSessionSettings (toList -> packageInfos) pathBase = do
437454 (pkgFiles, pkgSrcPaths :: [NonEmpty FilePath]) <- fmap unzip $ liftIO $ for packageInfos $ \pkg -> do
438455 canonicalSrcDirs <- traverse canonicalizePath $ (_cabalPackageInfo_packageRoot pkg </>) <$> _cabalPackageInfo_sourceDirs pkg
439456 canonicalPkgFile <- canonicalizePath $ _cabalPackageInfo_packageFile pkg
440− pure (canonicalPkgFile `relativeTo` canonicalPathBase, (`relativeTo` canonicalPathBase) <$> canonicalSrcDirs)
457+ pure (canonicalPkgFile `relativeTo'` canonicalPathBase, (`relativeTo'` canonicalPathBase) <$> canonicalSrcDirs)
441458
442459 pure
443− $ ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
460+ $ baseGhciOptions
461+ <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
444462 <> concatMap (\p -> ["-optF", p]) pkgFiles
445463 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]
464+ where
465+ relativeTo' = if useRelativePaths then relativeTo else const
466+
467+baseGhciOptions :: [String]
468+baseGhciOptions =
469+ [ "-ignore-dot-ghci"
470+ , "-no-user-package-db"
471+ , "-package-env", "-"
472+ ]
446473
447474-- | Run ghci repl
448475runGhciRepl
449476 :: (MonadObelisk m, Foldable f)
450477 => FilePath -- ^ Path to project root
451− -> f CabalPackageInfo
452− -> FilePath -- ^ Path to .ghci
478+ -> f CabalPackageInfo -- ^ Packages to keep unbuilt
479+ -> [String] -- ^ GHCi arguments
453480 -> m ()
454−runGhciRepl root (toList -> packages) dotGhci =
481+runGhciRepl root (toList -> packages) ghciArgs =
455482 -- NOTE: We do *not* want to use $(staticWhich "ghci") here because we need the
456483 -- ghc that is provided by the shell in the user's project.
457− nixShellWithPkgs root True False (packageInfoToNamePathMap packages) "ghc" $ Just $ "ghci " <> makeBaseGhciOptions dotGhci -- TODO: Shell escape
484+ nixShellWithoutPkgs root True False (packageInfoToNamePathMap packages) "ghc" $
485+ Just $ unwords $ fmap bashEscape $ "ghci" : ghciArgs
458486
459487-- | Run ghcid
460488runGhcid
461489 :: (MonadObelisk m, Foldable f)
462490 => FilePath -- ^ Path to project root
463491 -> Bool -- ^ Should we chdir to root when running this process?
464− -> FilePath -- ^ Path to .ghci
465− -> f CabalPackageInfo
492+ -> [String] -- ^ GHCi arguments
493+ -> f CabalPackageInfo -- ^ Packages to keep unbuilt
466494 -> Maybe String -- ^ Optional command to run at every reload
467495 -> m ()
468−runGhcid root chdirToRoot dotGhci (toList -> packages) mcmd =
469− nixShellWithPkgs root True chdirToRoot (packageInfoToNamePathMap packages) "ghc" $ Just $ unwords $ ghcidExePath : opts -- TODO: Shell escape
496+runGhcid root chdirToRoot ghciArgs (toList -> packages) mcmd =
497+ nixShellWithoutPkgs root True chdirToRoot (packageInfoToNamePathMap packages) "ghc" $
498+ Just $ unwords $ fmap bashEscape $ ghcidExePath : opts
470499 where
471− opts =
472− [ "-W"
473− , "--command='ghci -ignore-dot-ghci " <> makeBaseGhciOptions dotGhci <> "' "
474− , "--outputfile=ghcid-output.txt"
475− ] <> map (\x -> "--reload='" <> x <> "'") reloadFiles
476− <> map (\x -> "--restart='" <> x <> "'") restartFiles
477− <> testCmd
478− testCmd = maybeToList (flip fmap mcmd $ \cmd -> "--test='" <> cmd <> "'") -- TODO: Shell escape
479−
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+ ]
480508 adjustRoot x = if chdirToRoot then makeRelative root x else x
481509 reloadFiles = map adjustRoot [root </> "config"]
482510 restartFiles = map (adjustRoot . _cabalPackageInfo_packageFile) packages
483511
484−makeBaseGhciOptions :: FilePath -> String
485−makeBaseGhciOptions dotGhci =
486− unwords
487− [ "-no-user-package-db"
488− , "-package-env -"
489− , "-ghci-script " <> dotGhci
490− ]
491−
492−getFreePort :: MonadIO m => m PortNumber
493−getFreePort = liftIO $ withSocketsDo $ do
494− addr:_ <- getAddrInfo (Just defaultHints) (Just "127.0.0.1") (Just "0")
495− 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
496516 where
497517 open addr = do
498− sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
499− bind sock (addrAddress addr)
518+ sock <- Socket.socket (Socket.addrFamily addr) (Socket.addrSocketType addr) (Socket.addrProtocol addr)
519+ Socket.bind sock (Socket.addrAddress addr)
500520 return sock
501521
502522
@@ -531,4 +551,4 @@ relativeTo dir base
531551 $ zipDefaultWith Nothing Nothing (,)
532552 (map Just $ splitDirectories base)
533553 (map Just $ splitDirectories dir)
534− where collapse = foldr (</>) ""
554+ where collapse = foldr (</>) ""