Skip to content

Commit 3fa15cd

Browse files
author
AndrewRademacher
committed
Removed legacy GHCi rendering functions.
1 parent 3aae8ed commit 3fa15cd

3 files changed

Lines changed: 16 additions & 68 deletions

File tree

src/Stack/Ghci.hs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ module Stack.Ghci
1616
, ghci
1717

1818
-- TODO: Address what should and should not be exported.
19-
, renderLegacyGhciScript
2019
, renderScriptGhci
2120
, renderScriptIntero
2221
) where
@@ -35,7 +34,6 @@ import Data.Either
3534
import Data.Function
3635
import Data.List
3736
import Data.List.Extra (nubOrd)
38-
import Data.List.Split (splitOn)
3937
import Data.Map.Strict (Map)
4038
import qualified Data.Map.Strict as M
4139
import Data.Maybe
@@ -64,7 +62,6 @@ import Stack.Ghci.Script
6462
import Stack.Package
6563
import Stack.Types
6664
import Stack.Types.Internal
67-
import System.FilePath (takeBaseName)
6865
import Text.Read (readMaybe)
6966

7067
#ifndef WINDOWS
@@ -142,25 +139,8 @@ ghci opts@GhciOpts{..} = do
142139
$logWarn
143140
("The following GHC options are incompatible with GHCi and have not been passed to it: " <>
144141
T.unwords (map T.pack (nubOrd omittedOpts)))
145-
allModules <- checkForDuplicateModules ghciNoLoadModules pkgs
142+
mainFile <- figureOutMainFile bopts mainIsTargets targets pkgs
146143
oiDir <- objectInterfaceDir bconfig
147-
(modulesToLoad, mainFile) <- if ghciNoLoadModules then return ([], Nothing) else do
148-
mmainFile <- figureOutMainFile bopts mainIsTargets targets pkgs
149-
modulesToLoad <- case mmainFile of
150-
Just mainFile -> do
151-
let (_, mfDirs, mfName) = filePathPieces mainFile
152-
mainPathPieces = map toFilePath mfDirs ++ [takeBaseName (toFilePath mfName)]
153-
liftM catMaybes $ forM allModules $ \mn -> do
154-
let matchesModule = splitOn "." mn `isSuffixOf` mainPathPieces
155-
if matchesModule
156-
then do
157-
$logWarn $ "Warning: Omitting load of module " <> T.pack mn <>
158-
", because it matches the filepath of the Main target, " <>
159-
T.pack (toFilePath mainFile)
160-
return Nothing
161-
else return (Just mn)
162-
Nothing -> return allModules
163-
return (modulesToLoad, mmainFile)
164144
let odir =
165145
[ "-odir=" <> toFilePathNoTrailingSep oiDir
166146
, "-hidir=" <> toFilePathNoTrailingSep oiDir ]
@@ -182,6 +162,7 @@ ghci opts@GhciOpts{..} = do
182162
if ghciNoLoadModules
183163
then execGhci macrosOptions
184164
else do
165+
checkForDuplicateModules pkgs
185166
scriptPath <- writeGhciScript tmpDirectory (renderScriptGhci pkgs mainFile)
186167
execGhci (macrosOptions ++ ["-ghci-script=" <> toFilePath scriptPath])
187168

@@ -201,17 +182,6 @@ writeGhciScript tmpDirectory script = do
201182
scriptPath = tmpDirectory </> $(mkRelFile "ghci-script")
202183
scriptFilePath = toFilePath scriptPath
203184

204-
renderLegacyGhciScript :: [String] -> Maybe (Path b t) -> String
205-
renderLegacyGhciScript modulesToLoad mainFile =
206-
let loadModules = ":add" <> case unwords (map quoteFileName modulesToLoad) of
207-
[] -> ""
208-
xs -> " " <> xs
209-
addMainFile = maybe "" ((":add " <>) . quoteFileName . toFilePath) mainFile
210-
bringIntoScope = ":module +" <> case unwords modulesToLoad of
211-
[] -> ""
212-
xs -> " " <> xs
213-
in unlines [loadModules,addMainFile,bringIntoScope]
214-
215185
findOwningPackageForMain :: [GhciPkgInfo] -> Path Abs File -> Maybe GhciPkgInfo
216186
findOwningPackageForMain pkgs mainFile =
217187
find (\pkg -> toFilePath (ghciPkgDir pkg) `isPrefixOf` toFilePath mainFile) pkgs
@@ -554,15 +524,14 @@ borderedWarning f = do
554524
$logWarn ""
555525
return x
556526

557-
checkForDuplicateModules :: (MonadThrow m, MonadLogger m) => Bool -> [GhciPkgInfo] -> m [String]
558-
checkForDuplicateModules noLoadModules pkgs = do
527+
checkForDuplicateModules :: (MonadThrow m, MonadLogger m) => [GhciPkgInfo] -> m ()
528+
checkForDuplicateModules pkgs = do
559529
unless (null duplicates) $ do
560530
borderedWarning $ do
561531
$logWarn "The following modules are present in multiple packages:"
562532
forM_ duplicates $ \(mn, pns) -> do
563533
$logWarn (" * " <> T.pack mn <> " (in " <> T.intercalate ", " (map packageNameText pns) <> ")")
564-
unless noLoadModules $ throwM LoadingDuplicateModules
565-
return (map fst allModules)
534+
throwM LoadingDuplicateModules
566535
where
567536
duplicates, allModules :: [(String, [PackageName])]
568537
duplicates = filter (not . null . tail . snd) allModules
@@ -635,13 +604,6 @@ setScriptPerms fp = do
635604
]
636605
#endif
637606

638-
filePathPieces :: Path Abs File -> (Path Abs Dir, [Path Rel Dir], Path Rel File)
639-
filePathPieces x0 = go (parent x0, [], filename x0)
640-
where
641-
go (x, dirs, fp)
642-
| parent x == x = (x, dirs, fp)
643-
| otherwise = (parent x, dirname x : dirs, fp)
644-
645607
{- Copied from Stack.Ide, may be useful in the future
646608
647609
-- | Get options and target files for the given package info.
@@ -683,10 +645,3 @@ targetsCmd target go@GlobalOpts{..} =
683645
(mapM (getPackageOptsAndTargetFiles pwd) pkgs)
684646
forM_ targets (liftIO . putStrLn)
685647
-}
686-
687-
-- | Make sure that a filename with spaces in it gets the proper quotes.
688-
quoteFileName :: String -> String
689-
quoteFileName x =
690-
if any (==' ') x
691-
then show x
692-
else x

src/Stack/Ghci/Script.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,28 @@ commandToBuilder (Add modules)
8282
| otherwise =
8383
fromText ":add "
8484
<> (mconcat $ intersperse (fromText " ")
85-
$ fmap (stringUtf8 . mconcat . intersperse "." . components)
85+
$ fmap (stringUtf8 . quoteFileName . mconcat . intersperse "." . components)
8686
$ S.toAscList modules)
8787
<> fromText "\n"
8888

8989
commandToBuilder (AddFile path) =
90-
fromText ":add " <> stringUtf8 (toFilePath path) <> fromText "\n"
90+
fromText ":add " <> stringUtf8 (quoteFileName (toFilePath path)) <> fromText "\n"
9191

9292
commandToBuilder (CdGhc path) =
93-
fromText ":cd-ghc " <> stringUtf8 (toFilePath path) <> fromText "\n"
93+
fromText ":cd-ghc " <> stringUtf8 (quoteFileName (toFilePath path)) <> fromText "\n"
9494

9595
commandToBuilder (Module modules)
9696
| S.null modules = fromText ":module +\n"
9797
| otherwise =
9898
fromText ":module + "
9999
<> (mconcat $ intersperse (fromText " ")
100-
$ fmap (stringUtf8 . mconcat . intersperse "." . components)
100+
$ fmap (stringUtf8 . quoteFileName . mconcat . intersperse "." . components)
101101
$ S.toAscList modules)
102102
<> fromText "\n"
103+
104+
-- | Make sure that a filename with spaces in it gets the proper quotes.
105+
quoteFileName :: String -> String
106+
quoteFileName x =
107+
if any (==' ') x
108+
then show x
109+
else x

src/test/Stack/GhciSpec.hs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import qualified Data.ByteString.Lazy as LBS
99
import qualified Data.Map as M
1010
import qualified Data.Set as S
1111
import Data.Text (Text)
12-
import qualified Data.Text as T
1312
import qualified Data.Text.Encoding as T
1413
import Distribution.ModuleName
1514
import Stack.Types.Package
@@ -26,19 +25,6 @@ spec :: Spec
2625
spec = do
2726
describe "GHCi" $ do
2827
describe "Script rendering" $ do
29-
describe "should render legacy GHCi scripts" $ do
30-
it "should render legacy script when given project:exe" $ do
31-
renderLegacyGhciScript [] (Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
32-
`shouldBe` T.unpack ghciLegacyScript_projectWithMain
33-
34-
it "should render legacy script when given project" $ do
35-
renderLegacyGhciScript ["Lib.A"] (Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
36-
`shouldBe` T.unpack ghciLegacyScript_projectWithLibAndMain
37-
38-
it "should render legacy script when given multiple project:lib" $ do
39-
renderLegacyGhciScript ["Lib.A", "Lib.B"] Nothing
40-
`shouldBe` T.unpack ghciLegacyScript_multipleProjectsWithLib
41-
4228
describe "should render GHCi scripts" $ do
4329
it "with one library package" $ do
4430
let res = scriptToLazyByteString $ renderScriptGhci packages_singlePackage Nothing

0 commit comments

Comments
 (0)