Skip to content

Support sub-library dependencies (rebased). #5839

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

Closed
Closed
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
39 changes: 1 addition & 38 deletions src/Stack/Build.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Build the project.
Expand Down Expand Up @@ -27,8 +26,6 @@ import qualified Data.Text.IO as TIO
import Data.Text.Read ( decimal )
import qualified Data.Vector as V
import qualified Data.Yaml as Yaml
import qualified Distribution.PackageDescription as C
import Distribution.Types.Dependency ( Dependency (..), depLibraries )
import Distribution.Version ( mkVersion )
import Path ( parent )
import Stack.Build.ConstructPlan ( constructPlan )
Expand Down Expand Up @@ -57,8 +54,7 @@ import Stack.Types.Package
( InstallLocation (..), LocalPackage (..), Package (..)
, PackageConfig (..), lpFiles, lpFilesForComponents )
import Stack.Types.SourceMap
( CommonPackage (..), ProjectPackage (..), SMTargets (..)
, SourceMap (..), Target (..) )
( SMTargets (..), SourceMap (..), Target (..) )
import System.Terminal ( fixCodePage )

newtype CabalVersionPrettyException
Expand Down Expand Up @@ -121,8 +117,6 @@ build msetLocalFiles = do
depsLocals <- localDependencies
let allLocals = locals <> depsLocals

checkSubLibraryDependencies (Map.elems $ smProject sourceMap)

boptsCli <- view $ envConfigL.to envConfigBuildOptsCLI
-- Set local files, necessary for file watching
stackYaml <- view stackYamlL
Expand Down Expand Up @@ -411,34 +405,3 @@ checkComponentsBuildable lps =
| lp <- lps
, c <- Set.toList (lpUnbuildable lp)
]

-- | Find if any sublibrary dependency (other than internal libraries) exists in
-- each project package.
checkSubLibraryDependencies :: HasTerm env => [ProjectPackage] -> RIO env ()
checkSubLibraryDependencies projectPackages =
forM_ projectPackages $ \projectPackage -> do
C.GenericPackageDescription pkgDesc _ _ lib subLibs foreignLibs exes tests benches <-
liftIO $ cpGPD . ppCommon $ projectPackage

let pName = pkgName . C.package $ pkgDesc
dependencies = concatMap getDeps subLibs <>
concatMap getDeps foreignLibs <>
concatMap getDeps exes <>
concatMap getDeps tests <>
concatMap getDeps benches <>
maybe [] C.condTreeConstraints lib
notInternal (Dependency pName' _ _) = pName' /= pName
publicDependencies = filter notInternal dependencies
publicLibraries = concatMap (toList . depLibraries) publicDependencies

when (subLibDepExist publicLibraries) $
prettyWarnS
"Sublibrary dependency is not supported, this will almost certainly \
\fail."
where
getDeps (_, C.CondNode _ dep _) = dep
subLibDepExist = any
( \case
C.LSubLibName _ -> True
C.LMainLibName -> False
)
21 changes: 15 additions & 6 deletions src/Stack/Build/ConstructPlan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Data.Monoid.Map ( MonoidMap(..) )
import qualified Data.Set as Set
import qualified Data.Text as T
import Distribution.Types.BuildType ( BuildType (Configure) )
import Distribution.Types.MungedPackageName
( encodeCompatPackageName )
import Distribution.Types.PackageName ( mkPackageName )
import Generics.Deriving.Monoid ( memptydefault, mappenddefault )
import Path ( parent )
Expand Down Expand Up @@ -239,7 +241,7 @@ constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap
sources <- getSources globalCabalVersion
mcur <- view $ buildConfigL.to bcCurator

let onTarget = void . addDep
let onTarget = void . addDep . toMungedPackageName
let inner = mapM_ onTarget $ Map.keys (smtTargets $ smTargets sourceMap)
pathEnvVar' <- liftIO $ maybe mempty T.pack <$> lookupEnv "PATH"
let ctx = mkCtx econfig globalCabalVersion sources mcur pathEnvVar'
Expand Down Expand Up @@ -519,8 +521,9 @@ addFinal lp package isAllInOne buildHaddocks = do
-- marked as a dependency, even if it is directly wanted. This makes sense - if
-- we left out packages that are deps, it would break the --only-dependencies
-- build plan.
addDep :: PackageName -> M (Either ConstructPlanException AddDepRes)
addDep name = do
addDep :: MungedPackageName -> M (Either ConstructPlanException AddDepRes)
addDep mungedName = do
let name = encodeCompatPackageName mungedName
libMap <- get
case Map.lookup name libMap of
Just res -> do
Expand Down Expand Up @@ -822,6 +825,9 @@ addEllipsis t
| T.length t < 100 = t
| otherwise = T.take 97 t <> "..."

toMungedPackageName :: PackageName -> MungedPackageName
toMungedPackageName pn = MungedPackageName pn LMainLibName

-- | Given a package, recurses into all of its dependencies. The results
-- indicate which packages are missing, meaning that their 'GhcPkgId's will be
-- figured out during the build, after they've been built. The 2nd part of the
Expand All @@ -843,9 +849,12 @@ addPackageDeps ::
addPackageDeps package = do
ctx <- ask
checkAndWarnForUnknownTools package
let deps' = packageDeps package
deps <- forM (Map.toList deps') $ \(depname, DepValue range depType) -> do
eres <- addDep depname
let deps' =
map (first toMungedPackageName) (Map.toList $ packageDeps package) <>
Map.toList (packageSubLibDeps package)
deps <- forM deps' $ \(mungedDepname, DepValue range depType) -> do
let MungedPackageName depname _ = mungedDepname
eres <- addDep mungedDepname
let getLatestApplicableVersionAndRev :: M (Maybe (Version, BlobKey))
getLatestApplicableVersionAndRev = do
vsAndRevs <-
Expand Down
8 changes: 1 addition & 7 deletions test/integration/tests/cabal-sublibrary-dependency/Main.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Control.Monad (unless)
import Data.List (isInfixOf)
import StackTest

-- This tests building two local packages, one of which depends on the other
Expand All @@ -10,8 +8,4 @@ main :: IO ()
-- The '--install-ghc' flag is passed here, because etc/scripts/release.hs
-- passes `--no-install-ghc` when `--alpine` is passed to its 'check' command.
-- (See stack.yaml; using GHC 9.4.4.)
main = stackErrStderr ["build", "--install-ghc"] $ \str ->
let msg = "Sublibrary dependency is not supported, this will almost \
\certainly fail."
in unless (msg `isInfixOf` str) $
error $ "Expected a warning: \n" ++ show msg
main = stack ["build", "--install-ghc"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
./
, subproject/