Skip to content

Commit

Permalink
'stack ghci': build project before launching GHCi (closes #1065, fixes
Browse files Browse the repository at this point in the history
…#1180)

Optimistically, launch GHCi anyway even if the build fails.
  • Loading branch information
borsboom committed Oct 31, 2015
1 parent 8bc7265 commit 29eaedb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
6 changes: 4 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ Other enhancements:
* Added a `stack config set resolver RESOLVER` command. Part of work on [#115](https://github.com/commercialhaskell/stack/issues/115)
* `stack setup` can now install GHCJS on windows. See [#1145](https://github.com/commercialhaskell/stack/issues/1145) and [#749](https://github.com/commercialhaskell/stack/issues/749)
* `stack hpc report` command added, which generates reports for HPC tix files
* `stack ghci` builds the project before launching GHCi. If the build fails, optimistically launch GHCi anyway [#1065](https://github.com/commercialhaskell/stack/issues/1065)

Bug fixes:

* Haddocks are now copied for dependencies [#1105](https://github.com/commercialhaskell/stack/issues/1105)
* Fix: Haddocks not copied for dependencies [#1105](https://github.com/commercialhaskell/stack/issues/1105)
* Fix: Global options did not work consistently after subcommand [#519](https://github.com/commercialhaskell/stack/issues/519)
* Fix: 'stack ghci' doesn't notice that a module got deleted [#1180](https://github.com/commercialhaskell/stack/issues/1180)
* Rebuild when cabal file is changed
* Global options now work consistently after subcommand [#519](https://github.com/commercialhaskell/stack/issues/519)

## v0.1.6.0

Expand Down
23 changes: 18 additions & 5 deletions src/Stack/Ghci.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
module Stack.Ghci (GhciOpts(..),GhciPkgInfo(..), ghciSetup, ghci) where

import Control.Monad.Catch
import Control.Exception.Enclosed (tryAny)
import Control.Monad.IO.Class
import Control.Monad.Logger
import Control.Monad.Reader
Expand Down Expand Up @@ -50,6 +51,7 @@ data GhciOpts = GhciOpts
,ghciNoLoadModules :: !Bool
,ghciAdditionalPackages :: ![String]
,ghciMainIs :: !(Maybe Text)
,ghciBuildFirst :: !(Maybe BuildSubset)
} deriving (Show,Eq)

-- | Necessary information to load a package or its components.
Expand All @@ -70,7 +72,7 @@ ghci
:: (HasConfig r, HasBuildConfig r, HasHttpManager r, MonadMask m, HasLogLevel r, HasTerminal r, HasEnvConfig r, MonadReader r m, MonadIO m, MonadThrow m, MonadLogger m, MonadCatch m, MonadBaseControl IO m)
=> GhciOpts -> m ()
ghci GhciOpts{..} = do
(targets,mainIsTargets,pkgs) <- ghciSetup ghciMainIs ghciTargets
(targets,mainIsTargets,pkgs) <- ghciSetup ghciBuildFirst ghciMainIs ghciTargets
bconfig <- asks getBuildConfig
mainFile <- figureOutMainFile mainIsTargets targets pkgs
wc <- getWhichCompiler
Expand Down Expand Up @@ -171,10 +173,11 @@ figureOutMainFile mainIsTargets targets0 packages =
-- information to load that package/components.
ghciSetup
:: (HasConfig r, HasHttpManager r, HasBuildConfig r, MonadMask m, HasTerminal r, HasLogLevel r, HasEnvConfig r, MonadReader r m, MonadIO m, MonadThrow m, MonadLogger m, MonadCatch m, MonadBaseControl IO m)
=> Maybe Text
=> Maybe BuildSubset
-> Maybe Text
-> [Text]
-> m (Map PackageName SimpleTarget, Maybe (Map PackageName SimpleTarget), [GhciPkgInfo])
ghciSetup mainIs stringTargets = do
ghciSetup mbuildFirst mainIs stringTargets = do
(_,_,targets) <-
parseTargetsFromBuildOpts
AllowNoTargets
Expand Down Expand Up @@ -215,12 +218,22 @@ ghciSetup mainIs stringTargets = do
return (Just (name, (cabalfp, simpleTargets)))
Nothing -> return Nothing
else return Nothing
-- Try to build, but optimistically launch GHCi anyway if it fails (#1065)
case (mbuildFirst, M.null realTargets) of
(Just _, False) -> do
eres <- tryAny $ build (const (return ())) Nothing bopts
case eres of
Left err -> do
$logError $ T.pack (show err)
$logWarn "Warning: build failed, but optimistically launching GHCi anyway"
Right () -> return ()
_ -> return ()
-- Load the list of modules _after_ building, to catch changes in unlisted dependencies (#1180)
let localLibs = [name | (name, (_, target)) <- locals, hasLocalComp isCLib target]
infos <-
forM locals $
\(name,(cabalfp,component)) ->
makeGhciPkgInfo sourceMap installedMap localLibs name cabalfp component
unless (M.null realTargets) (build (const (return ())) Nothing bopts)
return (realTargets, mainIsTargets, infos)
where
makeBuildOpts targets =
Expand All @@ -235,7 +248,7 @@ ghciSetup mainIs stringTargets = do
, boptsBenchmarkOpts = (boptsBenchmarkOpts base)
{ beoDisableRun = True
}
, boptsBuildSubset = BSOnlyDependencies
, boptsBuildSubset = fromMaybe BSAll mbuildFirst
}
where
base = defaultBuildOpts
Expand Down
2 changes: 1 addition & 1 deletion src/Stack/Ide.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ide
-> [String] -- ^ GHC options.
-> m ()
ide targets useropts = do
(_realTargets,_,pkgs) <- ghciSetup Nothing targets
(_realTargets,_,pkgs) <- ghciSetup (Just BSAll) Nothing targets
pwd <- getWorkingDir
(pkgopts,_srcfiles) <-
liftM mconcat $ forM pkgs $ getPackageOptsAndTargetFiles pwd
Expand Down
26 changes: 16 additions & 10 deletions src/Stack/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,10 @@ buildOptsParser cmd =
help "Fetch packages necessary for the build immediately, useful with --dry-run")

buildSubset =
flag' BSOnlySnapshot
(long "only-snapshot" <>
help "Only build packages for the snapshot database, not the local database")
<|> flag' BSOnlyDependencies
(long "only-dependencies" <>
help "Only build packages that are dependencies of targets on the command line")
<|> flag' BSOnlyDependencies
flag' BSOnlyDependencies
(long "dependencies-only" <>
help "A synonym for --only-dependencies")
<|> pure BSAll
<|> buildSubsetParser "" ""

fileWatch' =
flag' FileWatch
Expand Down Expand Up @@ -470,8 +464,7 @@ ghciOptsParser = GhciOpts
(strOption (long "with-ghc" <>
metavar "GHC" <>
help "Use this command for the GHC to run"))
<*> switch (long "no-load" <>
help "Don't load modules on start-up")
<*> (not <$> boolFlags True "load" "load modules on start-up" idm)
<*> packagesParser
<*> optional
(textOption
Expand All @@ -480,6 +473,8 @@ ghciOptsParser = GhciOpts
help "Specify which target should contain the main \
\module to load, such as for an executable for \
\test suite or benchmark."))
<*> (flag' Nothing (long "no-build" <> help "Don't build before launching GHCi") <|>
(Just <$> buildSubsetParser "build-" " before launching GHCi"))

-- | Parser for exec command
execOptsParser :: Maybe SpecialExecCmd -> Parser ExecOpts
Expand Down Expand Up @@ -530,6 +525,17 @@ execOptsExtraParser = eoPlainParser <|>
(long "plain" <>
help "Use an unmodified environment (only useful with Docker)")

-- | Parser for BuildSubset options
buildSubsetParser :: String -> String -> Parser BuildSubset
buildSubsetParser longPrefix helpSuffix =
flag' BSOnlySnapshot
(long (longPrefix ++ "only-snapshot") <>
help ("Only build packages for the snapshot database, not the local database" ++ helpSuffix))
<|> flag' BSOnlyDependencies
(long (longPrefix ++ "only-dependencies") <>
help ("Only build packages that are dependencies of targets on the command line" ++ helpSuffix))
<|> pure BSAll

-- | Parser for global command-line options.
globalOptsParser :: Bool -> Parser GlobalOptsMonoid
globalOptsParser isSub =
Expand Down
2 changes: 1 addition & 1 deletion src/Stack/Types/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ data BuildSubset
-- ^ Only install packages in the snapshot database, skipping
-- packages intended for the local database.
| BSOnlyDependencies
deriving Show
deriving (Show, Eq)

-- | Configuration for building.
data BuildOpts =
Expand Down
2 changes: 1 addition & 1 deletion src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ packagesCmd () go@GlobalOpts{..} =
targetsCmd :: Text -> GlobalOpts -> IO ()
targetsCmd target go@GlobalOpts{..} =
withBuildConfig go $
do (_realTargets,_,pkgs) <- ghciSetup Nothing [target]
do (_realTargets,_,pkgs) <- ghciSetup (Just BSAll) Nothing [target]
pwd <- getWorkingDir
targets <-
fmap
Expand Down

0 comments on commit 29eaedb

Please sign in to comment.