Skip to content

Commit bc7fc99

Browse files
committed
Use proper GHC option handling for passing multi-repl flags
This refactoring modifies the logic to start the multi-repl to use runGHCWithResponseFile rather than directly constructing a command line. Towards #10881
1 parent ffaa46b commit bc7fc99

File tree

2 files changed

+23
-26
lines changed
  • Cabal/src/Distribution/Simple/Program
  • cabal-install/src/Distribution/Client

2 files changed

+23
-26
lines changed

Cabal/src/Distribution/Simple/Program/GHC.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ data GhcOptions = GhcOptions
437437
-- the @ghc -i@ flag (@-i@ on its own with no path argument).
438438
, ghcOptSourcePath :: NubListR (SymbolicPath Pkg (Dir Source))
439439
-- ^ Search path for Haskell source files; the @ghc -i@ flag.
440+
, ghcOptUnitFiles :: [FilePath]
441+
-- ^ Unit files to load; the @ghc -unit@ flag.
440442
, -------------
441443
-- Packages
442444

@@ -970,6 +972,8 @@ renderGhcOptions comp _platform@(Platform _arch os) opts
970972
, [prettyShow modu | modu <- flags ghcOptInputModules]
971973
, concat [["-o", u out] | out <- flag ghcOptOutputFile]
972974
, concat [["-dyno", out] | out <- flag ghcOptOutputDynFile]
975+
, -- unit files
976+
concat [["-unit", "@" ++ unit] | unit <- ghcOptUnitFiles opts]
973977
, ---------------
974978
-- Extra
975979

cabal-install/src/Distribution/Client/CmdRepl.hs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ import Distribution.Simple.Command
100100
)
101101
import Distribution.Simple.Compiler
102102
( Compiler
103+
, PackageDBX (..)
103104
, compilerCompatVersion
104105
)
106+
import Distribution.Simple.Program.GHC
105107
import Distribution.Simple.Setup
106108
( ReplOptions (..)
107109
, commonSetupTempFileOptions
@@ -181,10 +183,6 @@ import Distribution.Compat.Binary (decode)
181183
import Distribution.Simple.Flag (fromFlagOrDefault, pattern Flag)
182184
import Distribution.Simple.Program.Builtin (ghcProgram)
183185
import Distribution.Simple.Program.Db (requireProgram)
184-
import Distribution.Simple.Program.Run
185-
( programInvocation
186-
, runProgramInvocation
187-
)
188186
import Distribution.Simple.Program.Types
189187
( ConfiguredProgram (programOverrideEnv)
190188
)
@@ -364,7 +362,7 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
364362
-- In addition, to avoid a *third* trip through the solver, we are
365363
-- replicating the second half of 'runProjectPreBuildPhase' by hand
366364
-- here.
367-
(buildCtx, compiler, replOpts', targets) <- withInstallPlan verbosity baseCtx'' $
365+
(buildCtx, compiler, platform, replOpts', targets) <- withInstallPlan verbosity baseCtx'' $
368366
\elaboratedPlan elaboratedShared' -> do
369367
let ProjectBaseContext{..} = baseCtx''
370368

@@ -401,13 +399,13 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
401399
, targetsMap = targets
402400
}
403401

404-
ElaboratedSharedConfig{pkgConfigCompiler = compiler} = elaboratedShared'
402+
ElaboratedSharedConfig{pkgConfigCompiler = compiler, pkgConfigPlatform = platform} = elaboratedShared'
405403

406404
repl_flags = case originalComponent of
407405
Just oci -> generateReplFlags includeTransitive elaboratedPlan' oci
408406
Nothing -> []
409407

410-
return (buildCtx, compiler, configureReplOptions & lReplOptionsFlags %~ (++ repl_flags), targets)
408+
return (buildCtx, compiler, platform, configureReplOptions & lReplOptionsFlags %~ (++ repl_flags), targets)
411409

412410
-- Multi Repl implementation see: https://well-typed.com/blog/2023/03/cabal-multi-unit/ for
413411
-- a high-level overview about how everything fits together.
@@ -448,7 +446,7 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
448446
-- Find what the unit files are, and start a repl based on all the response
449447
-- files which have been created in the directory.
450448
-- unit files for components
451-
unit_files <- listDirectory dir
449+
unit_files <- (filter (/= "paths")) <$> listDirectory dir
452450

453451
-- Order the unit files so that the find target becomes the active unit
454452
let active_unit_fp :: Maybe FilePath
@@ -469,26 +467,21 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
469467
in -- GHC considers the last unit passed to be the active one
470468
other_units ++ active_unit_files
471469

472-
render_j Serial = "1"
473-
render_j (UseSem n) = show @Int n
474-
render_j (NumJobs mn) = maybe "" (show @Int) mn
470+
convertParStrat :: ParStratX Int -> ParStratX String
471+
convertParStrat Serial = Serial
472+
convertParStrat (UseSem n) = NumJobs (Just n)
473+
convertParStrat (NumJobs mn) = NumJobs mn
474+
475+
let ghc_opts =
476+
mempty
477+
{ ghcOptMode = Flag GhcModeInteractive
478+
, ghcOptUnitFiles = map (dir </>) unit_files_ordered
479+
, ghcOptNumJobs = Flag (convertParStrat (buildSettingNumJobs (buildSettings ctx)))
480+
, ghcOptPackageDBs = [GlobalPackageDB]
481+
}
475482

476483
-- run ghc --interactive with
477-
runProgramInvocation verbosity $
478-
programInvocation ghcProg' $
479-
concat $
480-
[ "--interactive"
481-
, "-package-env"
482-
, "-" -- to ignore ghc.environment.* files
483-
, "-j"
484-
, render_j (buildSettingNumJobs (buildSettings ctx))
485-
]
486-
: [ ["-unit", "@" ++ dir </> unit]
487-
| unit <- unit_files_ordered
488-
, unit /= "paths"
489-
]
490-
491-
pure ()
484+
runGHCWithResponseFile "ghci_multi.rsp" Nothing tempFileOptions verbosity ghcProg' compiler platform Nothing ghc_opts
492485
else do
493486
-- single target repl
494487
replOpts'' <- case targetCtx of

0 commit comments

Comments
 (0)