Skip to content

Commit

Permalink
Revert "Fix Cabal version selection for Stack (esp. build-type:Custom)"
Browse files Browse the repository at this point in the history
This reverts commit 04c2d34.
  • Loading branch information
DanielG committed Feb 11, 2020
1 parent 4ca6570 commit a18bbb2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 53 deletions.
88 changes: 55 additions & 33 deletions lib/Distribution/Helper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -537,37 +537,43 @@ getFileModTime f = do

readProjInfo
:: QueryEnvI c pt -> ProjConf pt -> ProjConfModTimes -> PreInfo pt -> IO (ProjInfo pt)
readProjInfo qe pc pcm _pi = withVerbosity $ do
readProjInfo qe pc pcm pi = withVerbosity $ do
let projloc = qeProjLoc qe
case (qeDistDir qe, pc) of
(DistDirCabal SCV1 distdir, ProjConfV1{pcV1CabalFile}) -> do
setup_config_path <- canonicalizePath (distdir </> "setup-config")
hdr@(UnitHeader (pkg_name_bs, _pkg_ver) ("Cabal", hdrCabalVersion) _)
<- readSetupConfigHeader setup_config_path
let
v3_0_0_0 = makeVersion [3,0,0,0]
pkg_name
| hdrCabalVersion >= v3_0_0_0 = BSU.toString pkg_name_bs
| otherwise = BS8.unpack pkg_name_bs
pkg = Package
{ pPackageName = pkg_name
, pSourceDir = plCabalProjectDir projloc
, pCabalFile = CabalFile pcV1CabalFile
, pFlags = []
, pUnits = (:|[]) Unit
{ uUnitId = UnitId pkg_name
, uPackage = pkg { pUnits = () }
, uDistDir = DistDirLib distdir
, uImpl = UnitImplV1
mhdr <- readSetupConfigHeader setup_config_path
case mhdr of
Just hdr@(UnitHeader (pkg_name_bs, _pkg_ver) ("Cabal", hdrCabalVersion) _compId) -> do
let
v3_0_0_0 = makeVersion [3,0,0,0]
pkg_name
| hdrCabalVersion >= v3_0_0_0 = BSU.toString pkg_name_bs
| otherwise = BS8.unpack pkg_name_bs
pkg = Package
{ pPackageName = pkg_name
, pSourceDir = plCabalProjectDir projloc
, pCabalFile = CabalFile pcV1CabalFile
, pFlags = []
, pUnits = (:|[]) Unit
{ uUnitId = UnitId pkg_name
, uPackage = pkg { pUnits = () }
, uDistDir = DistDirLib distdir
, uImpl = UnitImplV1
}
}
piImpl = ProjInfoV1 { piV1SetupHeader = hdr }
return ProjInfo
{ piCabalVersion = hdrCabalVersion
, piProjConfModTimes = pcm
, piPackages = pkg :| []
, piImpl
}
}
piImpl = ProjInfoV1 { piV1SetupHeader = hdr }
return ProjInfo
{ piCabalVersion = hdrCabalVersion
, piProjConfModTimes = pcm
, piPackages = pkg :| []
, piImpl
}
Just UnitHeader {uhSetupId=(setup_name, _)} ->
panicIO $ printf "Unknown Setup package-id in setup-config header '%s': '%s'"
(BS8.unpack setup_name) setup_config_path
Nothing ->
panicIO $ printf "Could not read '%s' header" setup_config_path

(DistDirCabal SCV2 distdirv2, _) -> do
let plan_path = distdirv2 </> "cache" </> "plan.json"
Expand Down Expand Up @@ -596,16 +602,32 @@ readProjInfo qe pc pcm _pi = withVerbosity $ do
(DistDirStack{}, _) -> do
Just cabal_files <- NonEmpty.nonEmpty <$> Stack.listPackageCabalFiles qe
pkgs <- mapM (Stack.getPackage qe) cabal_files
let DistDirLib distdir = uDistDir $ NonEmpty.head $ pUnits $ NonEmpty.head pkgs
hdr <- readSetupConfigHeader $ distdir </> "setup-config"
let ("Cabal", cabalVer) = uhSetupId hdr
Just (cabalVer:_) <- runMaybeT $
let ?progs = qePrograms qe in
let PreInfoStack {piStackProjPaths} = pi in
GHC.listCabalVersions (Just (sppGlobalPkgDb piStackProjPaths))
-- ^ See [Note Stack Cabal Version]
return ProjInfo
{ piCabalVersion = cabalVer
, piProjConfModTimes = pcm
, piPackages = NonEmpty.sortWith pPackageName pkgs
, piImpl = ProjInfoStack
}

-- [Note Stack Cabal Version]
--
-- Stack just uses ghc-pkg on the global-pkg-db to determine the
-- appropriate Cabal version for a resolver when building, see
-- Stack.Setup.pathsFromCompiler(cabalPkgVer). We do essentially the same
-- thing here.
--
-- The code for building Setup.hs is in Stack.Build.Execute and the version
-- of cabal is set in withSingleContext.withCabal.getPackageArgs.
--
-- Note there is some special casing going on (see 'depsMinusCabal'), they
-- use the packages from the snapshot pkg-db except Cabal which comes from
-- the global pkg-db.

readUnitInfo :: Helper pt -> Unit pt -> UnitModTimes -> IO UnitInfo
readUnitInfo helper unit@Unit {uUnitId=uiUnitId} uiModTimes = do
res <- runHelper helper unit
Expand Down Expand Up @@ -793,7 +815,7 @@ mkCompHelperEnv
{ cheCabalVer = CabalVersion piCabalVersion
, cheProjDir = plCabalProjectDir projloc
, cheProjLocalCacheDir = distdir
, chePkgDb = []
, chePkgDb = Nothing
, chePlanJson = Nothing
, cheDistV2 = Nothing
}
Expand All @@ -807,7 +829,7 @@ mkCompHelperEnv
cheProjDir = plCabalProjectDir projloc
cheCabalVer = CabalVersion $ makeDataVersion pjCabalLibVersion
cheProjLocalCacheDir = distdir </> "cache"
chePkgDb = []
chePkgDb = Nothing
chePlanJson = Just plan
cheDistV2 = Just distdir
PlanJson {pjCabalLibVersion=Ver pjCabalLibVersion } = plan
Expand All @@ -816,7 +838,7 @@ mkCompHelperEnv
(DistDirStack mworkdir)
PreInfoStack
{ piStackProjPaths=StackProjPaths
{ sppGlobalPkgDb, sppSnapPkgDb, sppLocalPkgDb }
{ sppGlobalPkgDb }
}
ProjInfo { piCabalVersion }
= let workdir = fromMaybe ".stack-work" $ unRelativePath <$> mworkdir in
Expand All @@ -825,7 +847,7 @@ mkCompHelperEnv
{ cheCabalVer = CabalVersion $ piCabalVersion
, cheProjDir = projdir
, cheProjLocalCacheDir = projdir </> workdir
, chePkgDb = [sppGlobalPkgDb, sppSnapPkgDb, sppLocalPkgDb]
, chePkgDb = Just sppGlobalPkgDb
, chePlanJson = Nothing
, cheDistV2 = Nothing
}
13 changes: 2 additions & 11 deletions src/CabalHelper/Compiletime/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,9 @@ complainIfNoCabalFile pkgdir Nothing =
bultinCabalVersion :: Version
bultinCabalVersion = parseVer VERSION_Cabal

readSetupConfigHeader :: FilePath -> IO UnitHeader
readSetupConfigHeader :: FilePath -> IO (Maybe UnitHeader)
readSetupConfigHeader file = bracket (openFile file ReadMode) hClose $ \h -> do
mhdr <- parseSetupHeader <$> BS.hGetLine h
case mhdr of
Just hdr@(UnitHeader _PkgId ("Cabal", _hdrCabalVersion) _compId) -> do
return hdr
Just UnitHeader {uhSetupId=(setup_name, _)} -> panicIO $
printf "Unknown Setup package-id in setup-config header '%s': '%s'"
(BS8.unpack setup_name) file
Nothing -> panicIO $
printf "Could not read '%s' header" file

parseSetupHeader <$> BS.hGetLine h

parseSetupHeader :: BS.ByteString -> Maybe UnitHeader
parseSetupHeader header = case BS8.words header of
Expand Down
12 changes: 6 additions & 6 deletions src/CabalHelper/Compiletime/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ data CompilationProductScope = CPSGlobal | CPSProject
type CompHelperEnv = CompHelperEnv' CabalVersion
data CompHelperEnv' cv = CompHelperEnv
{ cheCabalVer :: !cv
, chePkgDb :: ![PackageDbDir]
, chePkgDb :: !(Maybe PackageDbDir)
-- ^ A package-db where we are guaranteed to find Cabal-`cheCabalVer`.
, cheProjDir :: !FilePath
, chePlanJson :: !(Maybe PlanJson)
Expand Down Expand Up @@ -119,16 +119,16 @@ compileHelper' CompHelperEnv {..} = do
CabalVersion cabalVerPlain -> do
runMaybeT $ msum $ map (\f -> f ghcVer cabalVerPlain) $
case chePkgDb of
[] ->
Nothing ->
[ compileWithCabalV2Inplace
, compileWithCabalV2GhcEnv
, compileCabalSource
, compileSandbox
, compileGlobal
, compileWithCabalInPrivatePkgDb
]
dbs ->
[ ((.).(.)) liftIO (compilePkgDbs dbs)
Just db ->
[ ((.).(.)) liftIO (compilePkgDb db)
]
appdir <- appCacheDir
let cp@CompPaths {compExePath} = compPaths appdir cheProjLocalCacheDir comp
Expand All @@ -148,11 +148,11 @@ compileHelper' CompHelperEnv {..} = do

-- for relaxed deps: find (sameMajorVersionAs cheCabalVer) . reverse . sort

compilePkgDbs dbs _ghcVer cabalVer = return $
compilePkgDb db _ghcVer cabalVer = return $
(,)
(pure ())
CompileWithCabalPackage
{ compPackageSource = GPSPackageDBs dbs
{ compPackageSource = GPSPackageDBs [db]
, compCabalVersion = CabalVersion cabalVer
, compProductTarget = CPSProject
}
Expand Down
6 changes: 3 additions & 3 deletions tests/CompileTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ testCabalVersions versions = do
mcabalVersions <- runMaybeT $ listCabalVersions (Just db)
case mcabalVersions of
Just [hdver] ->
return $ che0 (CabalVersion hdver) [db]
return $ che0 (CabalVersion hdver) (Just db)
_ ->
return $ che0 (CabalHEAD ()) []
return $ che0 (CabalHEAD ()) Nothing
(CabalVersion ver) ->
return $ che0 (CabalVersion ver) []
return $ che0 (CabalVersion ver) Nothing

compileHelper che

Expand Down

0 comments on commit a18bbb2

Please sign in to comment.