Skip to content

Commit c0942c6

Browse files
committed
Add --install-cabal option for stack setup.
Closes #2386.
1 parent 9571c19 commit c0942c6

File tree

7 files changed

+53
-30
lines changed

7 files changed

+53
-30
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Major changes:
1515
that all packages be present in a snapshot, however.
1616
[#2805](https://github.com/commercialhaskell/stack/issues/2805)
1717

18+
* `stack setup` now accepts a `--install-cabal VERSION` option which
19+
will install a specific version of the Cabal library globally.
20+
1821
Behavior changes:
1922

2023
* The default package metadata backend has been changed from Git to

doc/GUIDE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,9 @@ users. Here's a quick rundown:
20742074
* `stack setup --upgrade-cabal` can install a newer version of the Cabal
20752075
library, used for performing actual builds. You shouldn't generally do this,
20762076
since new Cabal versions may introduce incompatibilities with package sets,
2077-
but it can be useful if you're trying to test a specific bugfix.
2077+
but it can be useful if you're trying to test a specific bugfix. Since Stack
2078+
1.3.3, `stack setup --install-cabal VERSION` is also available if you need a
2079+
specific version.
20782080
* `stack list-dependencies` lists all of the packages and versions used for a
20792081
project
20802082
* `stack sig` subcommand can help you with GPG signing & verification

src/Stack/Setup.hs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ data SetupOpts = SetupOpts
137137
-- ^ Don't check for a compatible GHC version/architecture
138138
, soptsSkipMsys :: !Bool
139139
-- ^ Do not use a custom msys installation on Windows
140-
, soptsUpgradeCabal :: !Bool
140+
, soptsUpgradeCabal :: !(Maybe UpgradeTo)
141141
-- ^ Upgrade the global Cabal library in the database to the newest
142142
-- version. Only works reliably with a stack-managed installation.
143143
, soptsResolveMissingGHC :: !(Maybe Text)
@@ -231,7 +231,7 @@ setupEnv mResolveMissingGHC = do
231231
, soptsSanityCheck = False
232232
, soptsSkipGhcCheck = configSkipGHCCheck config
233233
, soptsSkipMsys = configSkipMsys config
234-
, soptsUpgradeCabal = False
234+
, soptsUpgradeCabal = Nothing
235235
, soptsResolveMissingGHC = mResolveMissingGHC
236236
, soptsSetupInfoYaml = defaultSetupInfoYaml
237237
, soptsGHCBindistURL = Nothing
@@ -489,11 +489,12 @@ ensureCompiler sopts = do
489489
m <- augmentPathMap (edBins ed) (unEnvOverride menv0)
490490
mkEnvOverride (configPlatform config) (removeHaskellEnvVars m)
491491

492-
when (soptsUpgradeCabal sopts) $ do
492+
let cabalVersion = soptsUpgradeCabal sopts
493+
when (isJust $ cabalVersion) $ do
493494
unless needLocal $ do
494-
$logWarn "Trying to upgrade Cabal library on a GHC not installed by stack."
495+
$logWarn "Trying to change a Cabal library on a GHC not installed by stack."
495496
$logWarn "This may fail, caveat emptor!"
496-
upgradeCabal menv wc
497+
upgradeCabal menv wc (fromJust cabalVersion)
497498

498499
case mtools of
499500
Just (Just (ToolGhcjs cv), _) -> ensureGhcjsBooted menv cv (soptsInstallIfMissing sopts)
@@ -622,46 +623,47 @@ ensureDockerStackExe containerPlatform = do
622623
downloadStackExe platforms sri stackExeDir (const $ return ())
623624
return stackExePath
624625

625-
-- | Install the newest version of Cabal globally
626+
-- | Install the newest version or a specific version of Cabal globally
626627
upgradeCabal :: (StackM env m, HasConfig env, HasGHCVariant env)
627628
=> EnvOverride
628629
-> WhichCompiler
630+
-> UpgradeTo
629631
-> m ()
630-
upgradeCabal menv wc = do
632+
upgradeCabal menv wc cabalVersion = do
631633
let name = $(mkPackageName "Cabal")
632634
rmap <- resolvePackages menv Nothing Map.empty (Set.singleton name)
633-
newest <-
634-
case map rpIdent rmap of
635+
version <- case cabalVersion of
636+
Specific v -> return v
637+
Latest -> case map rpIdent rmap of
635638
[] -> error "No Cabal library found in index, cannot upgrade"
636-
[PackageIdentifier name' version]
637-
| name == name' -> return version
639+
[PackageIdentifier name' version] | name == name' -> return version
638640
x -> error $ "Unexpected results for resolvePackages: " ++ show x
639641
installed <- getCabalPkgVer menv wc
640-
if installed >= newest
642+
if installed >= version
641643
then $logInfo $ T.concat
642644
[ "Currently installed Cabal is "
643645
, T.pack $ versionString installed
644-
, ", newest is "
645-
, T.pack $ versionString newest
646-
, ". I'm not upgrading Cabal."
646+
, case cabalVersion of
647+
Specific _ -> " (specified)"
648+
Latest -> " (latest)"
649+
, ". No work to be done."
647650
]
648651
else withSystemTempDir "stack-cabal-upgrade" $ \tmpdir -> do
649652
$logInfo $ T.concat
650653
[ "Installing Cabal-"
651-
, T.pack $ versionString newest
654+
, T.pack $ versionString version
652655
, " to replace "
653656
, T.pack $ versionString installed
654657
]
655-
let ident = PackageIdentifier name newest
658+
let ident = PackageIdentifier name version
656659
-- Nothing below: use the newest .cabal file revision
657660
m <- unpackPackageIdents menv tmpdir Nothing (Map.singleton ident Nothing)
658661

659662
compilerPath <- join $ findExecutable menv (compilerExeName wc)
660-
newestDir <- parseRelDir $ versionString newest
663+
newestDir <- parseRelDir $ versionString version
661664
let installRoot = toFilePath $ parent (parent compilerPath)
662665
</> $(mkRelDir "new-cabal")
663666
</> newestDir
664-
665667
dir <-
666668
case Map.lookup ident m of
667669
Nothing -> error "upgradeCabal: Invariant violated, dir missing"

src/Stack/SetupCmd.hs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Stack.Types.Version
3030
data SetupCmdOpts = SetupCmdOpts
3131
{ scoCompilerVersion :: !(Maybe CompilerVersion)
3232
, scoForceReinstall :: !Bool
33-
, scoUpgradeCabal :: !Bool
33+
, scoUpgradeCabal :: !(Maybe UpgradeTo)
3434
, scoSetupInfoYaml :: !String
3535
, scoGHCBindistURL :: !(Maybe String)
3636
}
@@ -48,6 +48,22 @@ setupYamlCompatParser = stackSetupYaml <|> setupInfoYaml
4848
<> OA.metavar "URL"
4949
<> OA.value defaultSetupInfoYaml )
5050

51+
cabalUpgradeParser :: OA.Parser UpgradeTo
52+
cabalUpgradeParser = Specific <$> version' <|> latestParser
53+
where
54+
versionReader = do
55+
s <- OA.readerAsk
56+
case parseVersion (T.pack s) of
57+
Nothing -> OA.readerError $ "Invalid version: " ++ s
58+
Just v -> return v
59+
version' = OA.option versionReader (
60+
OA.long "install-cabal"
61+
<> OA.metavar "VERSION"
62+
<> OA.help "Install a specific version of Cabal" )
63+
latestParser = OA.flag' Latest (
64+
OA.long "upgrade-cabal"
65+
<> OA.help "Install latest version of Cabal globally" )
66+
5167
setupParser :: OA.Parser SetupCmdOpts
5268
setupParser = SetupCmdOpts
5369
<$> OA.optional (OA.argument readVersion
@@ -58,10 +74,7 @@ setupParser = SetupCmdOpts
5874
"reinstall"
5975
"reinstalling GHC, even if available (incompatible with --system-ghc)"
6076
OA.idm
61-
<*> OA.boolFlags False
62-
"upgrade-cabal"
63-
"installing the newest version of the Cabal library globally"
64-
OA.idm
77+
<*> OA.optional cabalUpgradeParser
6578
<*> setupYamlCompatParser
6679
<*> OA.optional (OA.strOption
6780
(OA.long "ghc-bindist"

src/Stack/Solver.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,14 @@ setupCompiler compiler = do
293293
, soptsUseSystem = configSystemGHC config
294294
, soptsWantedCompiler = compiler
295295
, soptsCompilerCheck = configCompilerCheck config
296-
297296
, soptsStackYaml = Nothing
298297
, soptsForceReinstall = False
299298
, soptsSanityCheck = False
300299
, soptsSkipGhcCheck = False
301300
, soptsSkipMsys = configSkipMsys config
302-
, soptsUpgradeCabal = False
301+
, soptsUpgradeCabal = Nothing
303302
, soptsResolveMissingGHC = msg
304-
, soptsSetupInfoYaml = defaultSetupInfoYaml
303+
, soptsSetupInfoYaml = defaultSetupInfoYaml
305304
, soptsGHCBindistURL = Nothing
306305
}
307306
return dirs

src/Stack/Types/Version.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module Stack.Types.Version
2727
,toMajorVersion
2828
,latestApplicableVersion
2929
,checkVersion
30-
,nextMajorVersion)
30+
,nextMajorVersion
31+
,UpgradeTo(..))
3132
where
3233

3334
import Control.Applicative
@@ -64,6 +65,9 @@ instance Exception VersionParseFail
6465
instance Show VersionParseFail where
6566
show (VersionParseFail bs) = "Invalid version: " ++ show bs
6667

68+
-- | A package version or the latest.
69+
data UpgradeTo = Specific Version | Latest deriving (Show)
70+
6771
-- | A package version.
6872
newtype Version =
6973
Version {unVersion :: Vector Word}

src/main/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ pathCmd keys go = withBuildConfig go (Stack.Path.path keys)
574574
setupCmd :: SetupCmdOpts -> GlobalOpts -> IO ()
575575
setupCmd sco@SetupCmdOpts{..} go@GlobalOpts{..} = do
576576
lc <- loadConfigWithOpts go
577-
when (scoUpgradeCabal && nixEnable (configNix (lcConfig lc))) $ do
577+
when (isJust scoUpgradeCabal && nixEnable (configNix (lcConfig lc))) $ do
578578
throwIO UpgradeCabalUnusable
579579
withUserFileLock go (configStackRoot $ lcConfig lc) $ \lk -> do
580580
let getCompilerVersion = loadCompilerVersion go lc

0 commit comments

Comments
 (0)