@@ -36,16 +36,13 @@ import Stack.Setup
3636import Stack.Types.Config
3737import System.Console.ANSI (hSupportsANSIWithoutEmulation )
3838import System.Environment (getEnvironment )
39- import System.IO
4039import System.FileLock
4140import System.Terminal (getTerminalWidth )
4241import Stack.Dot
4342
4443-- FIXME it seems wrong that we call loadBuildConfig multiple times
45- loadCompilerVersion :: Config
46- -> IO WantedCompiler
47- loadCompilerVersion config =
48- view wantedCompilerVersionL <$> runRIO config loadBuildConfig
44+ loadCompilerVersion :: RIO Config WantedCompiler
45+ loadCompilerVersion = view wantedCompilerVersionL <$> loadBuildConfig
4946
5047-- | Enforce mutual exclusion of every action running via this
5148-- function, on this path, on this users account.
@@ -54,13 +51,12 @@ loadCompilerVersion config =
5451-- stack uses locks per-snapshot. In the future, stack may refine
5552-- this to an even more fine-grain locking approach.
5653--
57- withUserFileLock :: MonadUnliftIO m
58- => GlobalOpts
59- -> Path Abs Dir
60- -> (Maybe FileLock -> m a )
61- -> m a
62- withUserFileLock go@ GlobalOpts {} dir act = do
63- env <- liftIO getEnvironment
54+ withUserFileLock :: HasRunner env
55+ => Path Abs Dir
56+ -> (Maybe FileLock -> RIO env a )
57+ -> RIO env a
58+ withUserFileLock dir act = withRunInIO $ \ run -> do
59+ env <- getEnvironment
6460 let toLock = lookup " STACK_LOCK" env == Just " true"
6561 if toLock
6662 then do
@@ -69,37 +65,37 @@ withUserFileLock go@GlobalOpts{} dir act = do
6965 ensureDir dir
7066 -- Just in case of asynchronous exceptions, we need to be careful
7167 -- when using tryLockFile here:
72- bracket (liftIO $ tryLockFile (toFilePath pth) Exclusive )
73- ( maybe ( return () ) (liftIO . unlockFile))
68+ bracket (tryLockFile (toFilePath pth) Exclusive )
69+ munlockFile
7470 (\ fstTry ->
7571 case fstTry of
76- Just lk -> finally ( act $ Just lk) (liftIO $ unlockFile lk)
72+ Just lk -> run $ act $ Just lk
7773 Nothing ->
78- do let chatter = globalLogLevel go /= LevelOther " silent"
79- when chatter $
80- liftIO $ hPutStrLn stderr $ " Failed to grab lock (" ++ show pth++
81- " ); other stack instance running. Waiting..."
82- bracket (liftIO $ lockFile (toFilePath pth) Exclusive )
83- (liftIO . unlockFile)
84- (\ lk -> do
85- when chatter $
86- liftIO $ hPutStrLn stderr " Lock acquired, proceeding."
74+ do run $ logError $
75+ " Failed to grab lock (" <>
76+ displayShow pth <>
77+ " ); other stack instance running. Waiting..."
78+ bracket (lockFile (toFilePath pth) Exclusive )
79+ unlockFile
80+ (\ lk -> run $ do
81+ logError " Lock acquired, proceeding."
8782 act $ Just lk))
88- else act Nothing
83+ else run $ act Nothing
8984
9085withConfigAndLock
9186 :: GlobalOpts
9287 -> RIO Config ()
9388 -> IO ()
94- withConfigAndLock go@ GlobalOpts {.. } inner = withConfig go $ \ config -> do
95- withUserFileLock go (view stackRootL config) $ \ lk ->
96- runRIO config $
97- Docker. reexecWithOptionalContainer
98- (configProjectRoot config)
99- Nothing
100- (runRIO config inner)
101- Nothing
102- (Just $ munlockFile lk)
89+ withConfigAndLock go@ GlobalOpts {.. } inner = withConfig go $ do
90+ stackRoot <- view stackRootL
91+ projectRoot <- view $ to configProjectRoot
92+ withUserFileLock stackRoot $ \ lk ->
93+ Docker. reexecWithOptionalContainer
94+ projectRoot
95+ Nothing
96+ inner
97+ Nothing
98+ (Just $ munlockFile lk)
10399
104100-- | Loads global config, ignoring any configuration which would be
105101-- loaded due to $PWD.
@@ -108,12 +104,12 @@ withGlobalConfigAndLock
108104 -> RIO Config ()
109105 -> IO ()
110106withGlobalConfigAndLock go@ GlobalOpts {.. } inner =
111- withRunnerGlobal go $ \ runner ->
112- runRIO runner $ loadConfigMaybeProject
107+ withRunnerGlobal go $
108+ loadConfigMaybeProject
113109 globalConfigMonoid
114110 globalResolver
115111 PCNoProject $ \ lc ->
116- withUserFileLock go (view stackRootL lc) $ \ _lk ->
112+ withUserFileLock (view stackRootL lc) $ \ _lk ->
117113 runRIO lc inner
118114
119115-- For now the non-locking version just unlocks immediately.
@@ -163,12 +159,13 @@ withEnvConfigAndLock go needTargets boptsCLI inner =
163159-- since it does not need to run any commands to get information on
164160-- the project. This is a change as of #4480. For previous behavior,
165161-- see issue #2010.
166- withCleanConfig :: GlobalOpts -> RIO BuildConfig () -> IO ()
162+ withCleanConfig :: GlobalOpts -> RIO BuildConfig a -> IO a
167163withCleanConfig go inner =
168- withConfig go $ \ config ->
169- withUserFileLock go (view stackRootL config) $ \ _lk0 -> do
170- bconfig <- runRIO config loadBuildConfig
171- runRIO bconfig inner
164+ withConfig go $ do
165+ root <- view stackRootL
166+ withUserFileLock root $ \ _lk0 -> do
167+ bconfig <- loadBuildConfig
168+ runRIO bconfig inner
172169
173170withEnvConfigExt
174171 :: GlobalOpts
@@ -188,8 +185,9 @@ withEnvConfigExt
188185 -- available in this action, since that would require build tools to be
189186 -- installed on the host OS.
190187 -> IO ()
191- withEnvConfigExt go@ GlobalOpts {.. } needTargets boptsCLI mbefore inner mafter = withConfig go $ \ config -> do
192- withUserFileLock go (view stackRootL config) $ \ lk0 -> do
188+ withEnvConfigExt go@ GlobalOpts {.. } needTargets boptsCLI mbefore inner mafter = withConfig go $ do
189+ config <- ask
190+ withUserFileLock (view stackRootL config) $ \ lk0 -> do
193191 -- A local bit of state for communication between callbacks:
194192 curLk <- newIORef lk0
195193 let inner' lk =
@@ -198,7 +196,7 @@ withEnvConfigExt go@GlobalOpts{..} needTargets boptsCLI mbefore inner mafter = w
198196 -- trade in the lock here.
199197 do dir <- installationRootDeps
200198 -- Hand-over-hand locking:
201- withUserFileLock go dir $ \ lk2 -> do
199+ withUserFileLock dir $ \ lk2 -> do
202200 liftIO $ writeIORef curLk lk2
203201 liftIO $ munlockFile lk
204202 logDebug " Starting to execute command inside EnvConfig"
@@ -209,13 +207,10 @@ withEnvConfigExt go@GlobalOpts{..} needTargets boptsCLI mbefore inner mafter = w
209207 envConfig <- runRIO bconfig (setupEnv needTargets boptsCLI Nothing )
210208 runRIO envConfig (inner' lk)
211209
212- let getCompilerVersion = loadCompilerVersion config
213- runRIO config $
214- Docker. reexecWithOptionalContainer
210+ Docker. reexecWithOptionalContainer
215211 (configProjectRoot config)
216212 mbefore
217- (runRIO config $
218- Nix. reexecWithOptionalShell (configProjectRoot config) getCompilerVersion (inner'' lk0))
213+ (Nix. reexecWithOptionalShell (configProjectRoot config) loadCompilerVersion (inner'' lk0))
219214 mafter
220215 (Just $ liftIO $
221216 do lk' <- readIORef curLk
@@ -225,20 +220,19 @@ withEnvConfigExt go@GlobalOpts{..} needTargets boptsCLI mbefore inner mafter = w
225220-- throughout this module.
226221withConfig
227222 :: GlobalOpts
228- -> ( Config -> IO a )
223+ -> RIO Config a
229224 -> IO a
230- withConfig go@ GlobalOpts {.. } inner = withRunnerGlobal go $ \ runner -> do
225+ withConfig go@ GlobalOpts {.. } inner = withRunnerGlobal go $ do
231226 mstackYaml <- forM globalStackYaml resolveFile'
232- runRIO runner $
233- loadConfig globalConfigMonoid globalResolver mstackYaml $ \ config -> do
234- -- If we have been relaunched in a Docker container, perform in-container initialization
235- -- (switch UID, etc.). We do this after first loading the configuration since it must
236- -- happen ASAP but needs a configuration.
237- forM_ globalDockerEntrypoint $ Docker. entrypoint config
238- liftIO $ inner config
227+ loadConfig globalConfigMonoid globalResolver mstackYaml $ \ config -> do
228+ -- If we have been relaunched in a Docker container, perform in-container initialization
229+ -- (switch UID, etc.). We do this after first loading the configuration since it must
230+ -- happen ASAP but needs a configuration.
231+ forM_ globalDockerEntrypoint $ Docker. entrypoint config
232+ runRIO config inner
239233
240- withRunnerGlobal :: GlobalOpts -> ( Runner -> IO a ) -> IO a
241- withRunnerGlobal go inner = liftIO $ do
234+ withRunnerGlobal :: GlobalOpts -> RIO Runner a -> IO a
235+ withRunnerGlobal go inner = do
242236 colorWhen <-
243237 case getFirst $ configMonoidColorWhen $ globalConfigMonoid go of
244238 Nothing -> defaultColorWhen
@@ -260,13 +254,13 @@ withRunnerGlobal go inner = liftIO $ do
260254 $ setLogVerboseFormat (globalLogLevel go <= LevelDebug )
261255 $ setLogTerminal (globalTerminal go)
262256 logOptions0
263- withLogFunc logOptions $ \ logFunc -> inner Runner
257+ withLogFunc logOptions $ \ logFunc -> runRIO Runner
264258 { runnerGlobalOpts = go
265259 , runnerUseColor = useColor
266260 , runnerLogFunc = logFunc
267261 , runnerTermWidth = termWidth
268262 , runnerProcessContext = menv
269- }
263+ } inner
270264 where clipWidth w
271265 | w < minTerminalWidth = minTerminalWidth
272266 | w > maxTerminalWidth = maxTerminalWidth
0 commit comments