Skip to content

Use proper GHC option handling for passing multi-repl flags #10995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cabal/src/Distribution/Simple/Program/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ data GhcOptions = GhcOptions
-- the @ghc -i@ flag (@-i@ on its own with no path argument).
, ghcOptSourcePath :: NubListR (SymbolicPath Pkg (Dir Source))
-- ^ Search path for Haskell source files; the @ghc -i@ flag.
, ghcOptUnitFiles :: [FilePath]
-- ^ Unit files to load; the @ghc -unit@ flag.
, -------------
-- Packages

Expand Down Expand Up @@ -970,6 +972,8 @@ renderGhcOptions comp _platform@(Platform _arch os) opts
, [prettyShow modu | modu <- flags ghcOptInputModules]
, concat [["-o", u out] | out <- flag ghcOptOutputFile]
, concat [["-dyno", out] | out <- flag ghcOptOutputDynFile]
, -- unit files
concat [["-unit", "@" ++ unit] | unit <- ghcOptUnitFiles opts]
, ---------------
-- Extra

Expand Down
45 changes: 19 additions & 26 deletions cabal-install/src/Distribution/Client/CmdRepl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ import Distribution.Simple.Command
)
import Distribution.Simple.Compiler
( Compiler
, PackageDBX (..)
, compilerCompatVersion
)
import Distribution.Simple.Program.GHC
import Distribution.Simple.Setup
( ReplOptions (..)
, commonSetupTempFileOptions
Expand Down Expand Up @@ -181,10 +183,6 @@ import Distribution.Compat.Binary (decode)
import Distribution.Simple.Flag (fromFlagOrDefault, pattern Flag)
import Distribution.Simple.Program.Builtin (ghcProgram)
import Distribution.Simple.Program.Db (requireProgram)
import Distribution.Simple.Program.Run
( programInvocation
, runProgramInvocation
)
import Distribution.Simple.Program.Types
( ConfiguredProgram (programOverrideEnv)
)
Expand Down Expand Up @@ -364,7 +362,7 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
-- In addition, to avoid a *third* trip through the solver, we are
-- replicating the second half of 'runProjectPreBuildPhase' by hand
-- here.
(buildCtx, compiler, replOpts', targets) <- withInstallPlan verbosity baseCtx'' $
(buildCtx, compiler, platform, replOpts', targets) <- withInstallPlan verbosity baseCtx'' $
\elaboratedPlan elaboratedShared' -> do
let ProjectBaseContext{..} = baseCtx''

Expand Down Expand Up @@ -401,13 +399,13 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
, targetsMap = targets
}

ElaboratedSharedConfig{pkgConfigCompiler = compiler} = elaboratedShared'
ElaboratedSharedConfig{pkgConfigCompiler = compiler, pkgConfigPlatform = platform} = elaboratedShared'

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

return (buildCtx, compiler, configureReplOptions & lReplOptionsFlags %~ (++ repl_flags), targets)
return (buildCtx, compiler, platform, configureReplOptions & lReplOptionsFlags %~ (++ repl_flags), targets)

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

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

render_j Serial = "1"
render_j (UseSem n) = show @Int n
render_j (NumJobs mn) = maybe "" (show @Int) mn
convertParStrat :: ParStratX Int -> ParStratX String
convertParStrat Serial = Serial
convertParStrat (UseSem n) = NumJobs (Just n)
convertParStrat (NumJobs mn) = NumJobs mn

let ghc_opts =
mempty
{ ghcOptMode = Flag GhcModeInteractive
, ghcOptUnitFiles = map (dir </>) unit_files_ordered
, ghcOptNumJobs = Flag (convertParStrat (buildSettingNumJobs (buildSettings ctx)))
, ghcOptPackageDBs = [GlobalPackageDB]
}

-- run ghc --interactive with
runProgramInvocation verbosity $
programInvocation ghcProg' $
concat $
[ "--interactive"
, "-package-env"
, "-" -- to ignore ghc.environment.* files
, "-j"
, render_j (buildSettingNumJobs (buildSettings ctx))
]
: [ ["-unit", "@" ++ dir </> unit]
| unit <- unit_files_ordered
, unit /= "paths"
]

pure ()
runGHCWithResponseFile "ghci_multi.rsp" Nothing tempFileOptions verbosity ghcProg' compiler platform Nothing ghc_opts
else do
-- single target repl
replOpts'' <- case targetCtx of
Expand Down
Loading