Skip to content

Commit 5b58500

Browse files
committed
"stack ghci" now does package hiding when non-local targets are specified
See #361 (comment)
1 parent 19da707 commit 5b58500

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

ChangeLog.md

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Other enhancements:
115115
be optimal yet. The terminal width can be overriden with the
116116
new `--terminal-width` command-line option (this works even on
117117
non-POSIX).
118+
* Passing non local packages as targets to `stack ghci` will now
119+
cause them to be used as `-package` args along with package
120+
hiding.
118121

119122
Bug fixes:
120123

src/Stack/Ghci.hs

+61-21
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ data GhciOpts = GhciOpts
6666
, ghciMainIs :: !(Maybe Text)
6767
, ghciLoadLocalDeps :: !Bool
6868
, ghciSkipIntermediate :: !Bool
69-
, ghciHidePackages :: !Bool
69+
, ghciHidePackages :: !(Maybe Bool)
7070
, ghciNoBuild :: !Bool
7171
, ghciOnlyMain :: !Bool
7272
} deriving Show
@@ -138,31 +138,19 @@ ghci opts@GhciOpts{..} = do
138138
return (targetMap, Just (fileInfo, extraFiles))
139139
-- Get a list of all the local target packages.
140140
localTargets <- getAllLocalTargets opts inputTargets mainIsTargets sourceMap
141+
-- Get a list of all the non-local target packages.
142+
nonLocalTargets <- getAllNonLocalTargets inputTargets
141143
-- Check if additional package arguments are sensible.
142144
addPkgs <- checkAdditionalPackages ghciAdditionalPackages
143145
-- Build required dependencies and setup local packages.
144146
stackYaml <- view stackYamlL
145147
buildDepsAndInitialSteps opts (map (packageNameText . fst) localTargets)
146-
when (M.null inputTargets && isNothing mfileTargets) $
147-
prettyWarn $ vsep
148-
[ flow "No targets specified, so ghci will not use any options from your package.yaml / *.cabal files."
149-
, ""
150-
, flow "Potential ways to resolve this:"
151-
, bulletedList
152-
[ fillSep
153-
[ flow "If you want to use the package.yaml / *.cabal package in the current directory, use"
154-
, styleShell "stack init"
155-
, flow "to create a new stack.yaml."
156-
]
157-
, flow "Add to the 'packages' field of" <+> display stackYaml
158-
]
159-
, ""
160-
]
148+
targetWarnings stackYaml localTargets nonLocalTargets mfileTargets
161149
-- Load the list of modules _after_ building, to catch changes in unlisted dependencies (#1180)
162150
pkgs <- getGhciPkgInfos buildOptsCLI sourceMap addPkgs (fmap fst mfileTargets) localTargets
163151
checkForIssues pkgs
164152
-- Finally, do the invocation of ghci
165-
runGhci opts localTargets mainIsTargets pkgs (maybe [] snd mfileTargets)
153+
runGhci opts localTargets mainIsTargets pkgs (maybe [] snd mfileTargets) (nonLocalTargets ++ addPkgs)
166154

167155
preprocessTargets :: HasEnvConfig env => BuildOptsCLI -> [Text] -> RIO env (Either [Path Abs File] (Map PackageName Target))
168156
preprocessTargets buildOptsCLI rawTargets = do
@@ -286,6 +274,14 @@ getAllLocalTargets GhciOpts{..} targets0 mainIsTargets sourceMap = do
286274
]
287275
return (directlyWanted ++ extraLoadDeps)
288276

277+
getAllNonLocalTargets
278+
:: Map PackageName Target
279+
-> RIO env [PackageName]
280+
getAllNonLocalTargets targets = do
281+
let isNonLocal (TargetAll Dependency) = True
282+
isNonLocal _ = False
283+
return $ map fst $ filter (isNonLocal . snd) (M.toList targets)
284+
289285
buildDepsAndInitialSteps :: HasEnvConfig env => GhciOpts -> [Text] -> RIO env ()
290286
buildDepsAndInitialSteps GhciOpts{..} targets0 = do
291287
let targets = targets0 ++ map T.pack ghciAdditionalPackages
@@ -317,14 +313,24 @@ runGhci
317313
-> Maybe (Map PackageName Target)
318314
-> [GhciPkgInfo]
319315
-> [Path Abs File]
316+
-> [PackageName]
320317
-> RIO env ()
321-
runGhci GhciOpts{..} targets mainIsTargets pkgs extraFiles = do
318+
runGhci GhciOpts{..} targets mainIsTargets pkgs extraFiles exposePackages = do
322319
config <- view configL
323320
wc <- view $ actualCompilerVersionL.whichCompilerL
324-
let pkgopts = hidePkgOpt ++ genOpts ++ ghcOpts
325-
hidePkgOpt = if null pkgs || not ghciHidePackages then [] else ["-hide-all-packages"]
321+
let pkgopts = hidePkgOpts ++ genOpts ++ ghcOpts
322+
shouldHidePackages =
323+
case ghciHidePackages of
324+
-- Default to not hiding anything if there's nothing to
325+
-- expose.
326+
Nothing -> not (null pkgs && null exposePackages)
327+
Just x -> x
328+
hidePkgOpts =
329+
if shouldHidePackages
330+
then "-hide-all-packages" : concatMap (\n -> ["-package", packageNameString n]) exposePackages
331+
else []
326332
oneWordOpts bio
327-
| ghciHidePackages = bioOneWordOpts bio ++ bioPackageFlags bio
333+
| shouldHidePackages = bioOneWordOpts bio ++ bioPackageFlags bio
328334
| otherwise = bioOneWordOpts bio
329335
genOpts = nubOrd (concatMap (concatMap (oneWordOpts . snd) . ghciPkgOpts) pkgs)
330336
(omittedOpts, ghcOpts) = partition badForGhci $
@@ -700,6 +706,40 @@ checkForDuplicateModules pkgs = do
700706
M.toList $ M.fromListWith (++) $
701707
concatMap (\pkg -> map ((, [ghciPkgName pkg]) . C.display) (S.toList (ghciPkgModules pkg))) pkgs
702708

709+
targetWarnings
710+
:: HasRunner env
711+
=> Path Abs File
712+
-> [(PackageName, (Path Abs File, Target))]
713+
-> [PackageName]
714+
-> Maybe (Map PackageName (Set (Path Abs File)), [Path Abs File])
715+
-> RIO env ()
716+
targetWarnings stackYaml localTargets nonLocalTargets mfileTargets = do
717+
unless (null nonLocalTargets) $
718+
prettyWarnL
719+
[ flow "Some targets"
720+
, parens $ fillSep $ punctuate "," $ map (styleGood . display) nonLocalTargets
721+
, flow "are not local packages, and so cannot be directly loaded."
722+
, flow "In future versions of stack, this might be supported - see"
723+
, styleUrl "https://github.com/commercialhaskell/stack/issues/1441"
724+
, "."
725+
, flow "It can still be useful to specify these, as they will be passed to ghci via -package flags."
726+
]
727+
when (null localTargets && isNothing mfileTargets) $
728+
prettyWarn $ vsep
729+
[ flow "No local targets specified, so ghci will not use any options from your package.yaml / *.cabal files."
730+
, ""
731+
, flow "Potential ways to resolve this:"
732+
, bulletedList
733+
[ fillSep
734+
[ flow "If you want to use the package.yaml / *.cabal package in the current directory, use"
735+
, styleShell "stack init"
736+
, flow "to create a new stack.yaml."
737+
]
738+
, flow "Add to the 'packages' field of" <+> display stackYaml
739+
]
740+
, ""
741+
]
742+
703743
-- Adds in intermediate dependencies between ghci targets. Note that it
704744
-- will return a Lib component for these intermediate dependencies even
705745
-- if they don't have a library (but that's fine for the usage within

src/Stack/Options/GhciParser.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ ghciOptsParser = GhciOpts
5252
<*> switch (long "load-local-deps" <> help "Load all local dependencies of your targets")
5353
-- TODO: deprecate this? probably useless.
5454
<*> switch (long "skip-intermediate-deps" <> help "Skip loading intermediate target dependencies" <> internal)
55-
<*> boolFlags True "package-hiding" "package hiding" idm
55+
<*> optional (boolFlags True "package-hiding" "package hiding" idm)
5656
<*> switch (long "no-build" <> help "Don't build before launching GHCi" <> internal)
5757
<*> switch (long "only-main" <> help "Only load and import the main module. If no main module, no modules will be loaded.")

0 commit comments

Comments
 (0)