Skip to content

Commit 42c9b5f

Browse files
committed
Refactor externs diff and make api, fix some review sugs
1 parent 173fd7b commit 42c9b5f

File tree

8 files changed

+191
-139
lines changed

8 files changed

+191
-139
lines changed

app/Command/Compile.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ compile PSCMakeOptions{..} = do
6565
let filePathMap = M.fromList $ map (\(fp, pm) -> (P.getModuleName $ CST.resPartial pm, Right fp)) ms
6666
foreigns <- inferForeignModules filePathMap
6767
let makeActions = buildMakeActions pscmOutputDir filePathMap foreigns pscmUsePrefix
68-
P.make makeActions (map snd ms)
68+
P.make_ makeActions (map snd ms)
6969
printWarningsAndErrors (P.optionsVerboseErrors pscmOpts) pscmJSONErrors moduleFiles makeWarnings makeErrors
7070
exitSuccess
7171

src/Language/PureScript/Interactive.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ make
7979
-> P.Make ([P.ExternsFile], P.Environment)
8080
make ms = do
8181
foreignFiles <- P.inferForeignModules filePathMap
82-
externs <- P.make' (buildActions foreignFiles) (map snd ms)
82+
externs <- P.make (buildActions foreignFiles) (map snd ms)
8383
return (externs, foldl' (flip P.applyExternsFileToEnvironment) P.initEnvironment externs)
8484
where
8585
buildActions :: M.Map P.ModuleName FilePath -> P.MakeActions P.Make

src/Language/PureScript/Make.hs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
module Language.PureScript.Make
2-
(
3-
-- * Make API
4-
rebuildModule
2+
( make
3+
, make_
4+
, rebuildModule
55
, rebuildModule'
6-
, make
7-
, make'
86
, inferForeignModules
97
, module Monad
108
, module Actions
@@ -14,7 +12,7 @@ import Prelude
1412

1513
import Control.Concurrent.Lifted as C
1614
import Control.Exception.Base (onException)
17-
import Control.Monad (foldM, unless, when)
15+
import Control.Monad (foldM, unless, when, void, foldM, unless, when)
1816
import Control.Monad.Error.Class (MonadError(..))
1917
import Control.Monad.IO.Class (MonadIO(..))
2018
import Control.Monad.Supply (evalSupplyT, runSupply, runSupplyT)
@@ -131,57 +129,53 @@ rebuildModuleWithIndex MakeActions{..} exEnv externs m@(Module _ _ moduleName _
131129
Right d -> d
132130

133131
evalSupplyT nextVar'' $ codegen renamed docs exts
134-
-- evaluate $ trace ("\n===== externs: " <> show moduleName <> ":\n" <> show exts) ()
135132
return exts
136133

134+
data MakeOptions = MakeOptions
135+
{ moCollectAllExterns :: Bool
136+
}
137+
137138
-- | Compiles in "make" mode, compiling each module separately to a @.js@ file
138139
-- and an @externs.cbor@ file.
139140
--
140141
-- If timestamps or hashes have not changed, existing externs files can be used
141142
-- to provide upstream modules' types without having to typecheck those modules
142143
-- again.
143144
--
144-
-- This version will collect an return externs only of modules that were used
145-
-- during the build.
145+
-- It collects and returns externs for all modules passed.
146146
make :: forall m. (MonadBaseControl IO m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
147147
=> MakeActions m
148148
-> [CST.PartialResult Module]
149149
-> m [ExternsFile]
150-
make ma ms = makeImp ma ms False
150+
make = make' (MakeOptions {moCollectAllExterns = True})
151151

152152
-- | Compiles in "make" mode, compiling each module separately to a @.js@ file
153153
-- and an @externs.cbor@ file.
154154
--
155-
-- If timestamps or hashes have not changed, existing externs files can be used
156-
-- to provide upstream modules' types without having to typecheck those modules
157-
-- again.
158-
--
159-
-- This version will collect an return all externs of all passed modules.
160-
make' :: forall m. (MonadBaseControl IO m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
155+
-- This version of make returns nothing.
156+
make_ :: forall m. (MonadBaseControl IO m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
161157
=> MakeActions m
162158
-> [CST.PartialResult Module]
163-
-> m [ExternsFile]
164-
make' ma ms = makeImp ma ms True
159+
-> m ()
160+
make_ ma ms = void $ make' (MakeOptions {moCollectAllExterns = False}) ma ms
165161

166-
makeImp :: forall m. (MonadBaseControl IO m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
167-
=> MakeActions m
162+
make' :: forall m. (MonadBaseControl IO m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
163+
=> MakeOptions
164+
-> MakeActions m
168165
-> [CST.PartialResult Module]
169-
-> Bool
170166
-> m [ExternsFile]
171-
makeImp ma@MakeActions{..} ms collectAll = do
167+
make' MakeOptions{..} ma@MakeActions{..} ms = do
172168
checkModuleNames
173169
cacheDb <- readCacheDb
174170

175171
(sorted, graph) <- sortModules Transitive (moduleSignature . CST.resPartial) ms
176-
177-
(buildPlan, newCacheDb) <- BuildPlan.construct ma cacheDb (sorted, graph) collectAll
172+
let opts = BuildPlan.Options {optPreloadAllExterns = moCollectAllExterns}
173+
(buildPlan, newCacheDb) <- BuildPlan.construct opts ma cacheDb (sorted, graph)
178174

179175
let sortedModuleNames = getModuleName . CST.resPartial <$> sorted
180176
let toBeRebuilt = filter (BuildPlan.needsRebuild buildPlan . getModuleName . CST.resPartial) sorted
181177
let totalModuleCount = length toBeRebuilt
182178
for_ toBeRebuilt $ \m -> fork $ do
183-
-- evaluate $ trace ("resPartial:" <> show (CST.resPartial $ m)) ()
184-
-- evaluate $ trace ("resFull:" <> show (CST.resFull $ m)) ()
185179
let moduleName = getModuleName . CST.resPartial $ m
186180
let deps = fromMaybe (internalError "make: module not found in dependency graph.") (lookup moduleName graph)
187181
buildModule buildPlan moduleName totalModuleCount
@@ -225,10 +219,11 @@ makeImp ma@MakeActions{..} ms collectAll = do
225219
fromMaybe (internalError $ "make: module not found in results: " <> T.unpack name)
226220
$ M.lookup mn successes
227221

228-
if collectAll then
229-
pure $ map lookupResult sortedModuleNames
230-
else
231-
pure $ mapMaybe (flip M.lookup successes) sortedModuleNames
222+
pure $
223+
if moCollectAllExterns then
224+
map lookupResult sortedModuleNames
225+
else
226+
mapMaybe (flip M.lookup successes) sortedModuleNames
232227

233228
where
234229
checkModuleNames :: m ()
@@ -275,12 +270,9 @@ makeImp ma@MakeActions{..} ms collectAll = do
275270
case mexterns of
276271
Just (_, depsDiffExterns) -> do
277272
let externs = fst <$> depsDiffExterns
278-
--evaluate $ trace ("diff:" <> show moduleName <> ":" <> show (snd <$> depsDiffExterns)) ()
279-
--evaluate $ trace ("check diff:" <> show moduleName <> ":" <> show (isNothing $ traverse snd depsDiffExterns)) ()
280273
let prevResult = BuildPlan.getPrevResult buildPlan moduleName
281274
let depsDiffs = traverse snd depsDiffExterns
282275
let maySkipBuild moduleIndex
283-
-- Just exts <- BuildPlan.getPrevResult buildPlan moduleName
284276
-- we may skip built only for up-to-date modules
285277
| Just (True, exts) <- prevResult
286278
-- check if no dep's externs have changed
@@ -291,7 +283,6 @@ makeImp ma@MakeActions{..} ms collectAll = do
291283
-- compilation results as actual. If it fails to update timestamp
292284
-- on any of exiting codegen targets, it will run the build process.
293285
updated <- updateOutputTimestamp moduleName
294-
--evaluate $ trace ("updated:" <> show updated <> ":" <> show moduleName) ()
295286
if updated then do
296287
progress $ SkippingModule moduleName moduleIndex
297288
pure $ Just (exts, MultipleErrors [], Just (emptyDiff moduleName))
@@ -311,9 +302,7 @@ makeImp ma@MakeActions{..} ms collectAll = do
311302
env <- C.readMVar (bpEnv buildPlan)
312303
idx <- C.takeMVar (bpIndex buildPlan)
313304
C.putMVar (bpIndex buildPlan) (idx + 1)
314-
-- (exts, warnings) <- do
315-
-- let doBuild = listen $ rebuildModuleWithIndex ma env externs m (Just (idx, cnt))
316-
-- maySkipBuild (Just (idx, cnt)) >>= maybe doBuild pure
305+
317306
(exts, warnings, diff) <- do
318307
let doBuild = do
319308
(exts, warnings) <-

src/Language/PureScript/Make/Actions.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ readCacheDb'
155155
-- ^ The path to the output directory
156156
-> m CacheDb
157157
readCacheDb' outputDir = do
158-
--fromMaybe mempty <$> readJSONFile (cacheDbFile outputDir)
159-
--fromMaybe mempty <$> (fmap fromCacheDbVersioned <$> readJSONFile (cacheDbFile outputDir))
160158
mdb <- readJSONFile (cacheDbFile outputDir)
161159
pure $ fromMaybe mempty $ do
162160
db <- mdb
@@ -268,11 +266,12 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
268266
updateOutputTimestamp mn = do
269267
curTime <- getCurrentTime
270268
ok <- setTimestamp (outputFilename mn externsFileName) curTime
271-
-- then update all actual codegen targets
269+
-- Then update timestamps of all actual codegen targets.
272270
codegenTargets <- asks optionsCodegenTargets
273271
let outputPaths = fmap (targetFilename mn) (S.toList codegenTargets)
274272
results <- traverse (flip setTimestamp curTime) outputPaths
275-
-- if something goes wrong, something failed to update, return Nothing
273+
-- If something goes wrong (any of targets doesn't exit, a file system
274+
-- error), return False.
276275
pure $ and (ok : results)
277276

278277
readExterns :: ModuleName -> Make (FilePath, Maybe ExternsFile)

src/Language/PureScript/Make/BuildPlan.hs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Language.PureScript.Make.BuildPlan
22
( BuildPlan(bpEnv, bpIndex)
33
, BuildJobResult(..)
4+
, Options(..)
45
, construct
56
, getResult
67
, getPrevResult
@@ -130,7 +131,6 @@ getResult
130131
-> ModuleName
131132
-> m (Maybe SuccessResult)
132133
getResult buildPlan moduleName =
133-
-- may bring back first lookup for bpPrebuilt
134134
case M.lookup moduleName (bpBuildJobs buildPlan) of
135135
Just bj ->
136136
buildJobSuccess <$> C.readMVar (bjResult bj)
@@ -146,20 +146,23 @@ getPrevResult :: BuildPlan -> ModuleName -> Maybe (Bool, ExternsFile)
146146
getPrevResult buildPlan moduleName =
147147
fmap pbExternsFile <$> M.lookup moduleName (bpPreviousBuilt buildPlan)
148148

149+
150+
data Options = Options
151+
{ optPreloadAllExterns :: Bool
152+
}
153+
149154
-- | Constructs a BuildPlan for the given module graph.
150155
--
151156
-- The given MakeActions are used to collect various timestamps in order to
152157
-- determine whether a module needs rebuilding.
153158
construct
154159
:: forall m. MonadBaseControl IO m
155-
=> MakeActions m
160+
=> Options
161+
-> MakeActions m
156162
-> CacheDb
157163
-> ([CST.PartialResult Module], [(ModuleName, [ModuleName])])
158-
-> Bool
159-
-- ^ If True will preload all the externs, otherwise will load only needed for
160-
-- the build.
161164
-> m (BuildPlan, CacheDb)
162-
construct MakeActions{..} cacheDb (sorted, graph) preloadAll = do
165+
construct Options{..} MakeActions{..} cacheDb (sorted, graph) = do
163166
let sortedModuleNames = map (getModuleName . CST.resPartial) sorted
164167
rebuildStatuses <- A.forConcurrently sortedModuleNames getRebuildStatus
165168

@@ -177,9 +180,10 @@ construct MakeActions{..} cacheDb (sorted, graph) preloadAll = do
177180
let inBuildDeps = flip S.member allBuildDeps
178181

179182
-- We only need prebuilt results for deps of the modules to be build.
180-
let toLoadPrebuilt
181-
| preloadAll = prebuiltMap
182-
| otherwise = M.filterWithKey (const . inBuildDeps) prebuiltMap
183+
let toLoadPrebuilt =
184+
if optPreloadAllExterns
185+
then prebuiltMap
186+
else M.filterWithKey (const . inBuildDeps) prebuiltMap
183187

184188
-- We will need previously built results for modules to be build
185189
-- to skip rebuilding if deps have not changed.
@@ -203,8 +207,8 @@ construct MakeActions{..} cacheDb (sorted, graph) preloadAll = do
203207
let prebuilt = M.mapMaybe id prebuiltLoad
204208
let previous = M.mapMaybe id prevLoad
205209

206-
-- If for some reason (wrong version, files corruption) loading fails,
207-
-- those modules should be rebuilt too.
210+
-- If for some reason (wrong version, files corruption, etc) prebuilt
211+
-- externs loading fails, those modules should be rebuilt too.
208212
let failedLoads = M.keys $ M.filter isNothing prebuiltLoad
209213
buildJobs <- foldM makeBuildJob M.empty (toBeRebuilt <> failedLoads)
210214

@@ -219,8 +223,8 @@ construct MakeActions{..} cacheDb (sorted, graph) preloadAll = do
219223
foldl' update cacheDb rebuildStatuses
220224
)
221225
where
222-
-- Timestamp here is just to ensure that we will try to load modules that
223-
-- have previous built results available.
226+
-- Timestamp here is just to ensure that we will only try to load modules
227+
-- that have previous built results available.
224228
loadPrebuilt :: ModuleName -> UTCTime -> m (Maybe Prebuilt)
225229
loadPrebuilt = const . fmap (fmap Prebuilt . snd) . readExterns
226230

src/Language/PureScript/Make/Cache.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ hash = ContentHash . Hash.hash
7272
type CacheDb = Map ModuleName CacheInfo
7373

7474
data CacheDbVersioned = CacheDbVersioned { cdbVersion :: Text, cdbModules :: CacheDb }
75-
--deriving stock (Show)
7675
deriving (Eq, Ord)
7776

7877
instance Aeson.FromJSON CacheDbVersioned where
7978
parseJSON = Aeson.withObject "CacheDb" $ \v ->
8079
CacheDbVersioned
81-
<$> v .: "version"
80+
<$> v .: "version"
8281
<*> v .: "modules"
8382

8483
instance Aeson.ToJSON CacheDbVersioned where

0 commit comments

Comments
 (0)