Skip to content

Commit dc9a6b1

Browse files
committed
Proper build tool loading from Cabal files
1 parent e4e7fb3 commit dc9a6b1

File tree

4 files changed

+37
-45
lines changed

4 files changed

+37
-45
lines changed

src/Stack/Build/ConstructPlan.hs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import qualified Data.Set as Set
3030
import qualified Data.Text as T
3131
import Data.Text.Encoding (decodeUtf8With)
3232
import Data.Text.Encoding.Error (lenientDecode)
33-
import qualified Distribution.Package as Cabal
3433
import qualified Distribution.Text as Cabal
3534
import qualified Distribution.Version as Cabal
3635
import Generics.Deriving.Monoid (memptydefault, mappenddefault)
@@ -129,7 +128,7 @@ data Ctx = Ctx
129128
, baseConfigOpts :: !BaseConfigOpts
130129
, loadPackage :: !(PackageLocationIndex FilePath -> Map FlagName Bool -> [Text] -> IO Package)
131130
, combinedMap :: !CombinedMap
132-
, toolToPackages :: !(Cabal.Dependency -> Map PackageName VersionRange)
131+
, toolToPackages :: !(ExeName -> Map PackageName VersionRange)
133132
, ctxEnvConfig :: !EnvConfig
134133
, callStack :: ![PackageName]
135134
, extraToBuild :: !(Set PackageName)
@@ -224,18 +223,18 @@ constructPlan ls0 baseConfigOpts0 locals extraToBuild0 localDumpPkgs loadPackage
224223
, baseConfigOpts = baseConfigOpts0
225224
, loadPackage = loadPackage0
226225
, combinedMap = combineMap sourceMap installedMap
227-
, toolToPackages = \(Cabal.Dependency name _) ->
226+
, toolToPackages = \name ->
228227
maybe Map.empty (Map.fromSet (const Cabal.anyVersion)) $
229-
Map.lookup (T.pack . packageNameString . fromCabalPackageName $ name) (toolMap lp)
228+
Map.lookup name toolMap
230229
, ctxEnvConfig = econfig
231230
, callStack = []
232231
, extraToBuild = extraToBuild0
233232
, getVersions = getVersions0
234233
, wanted = wantedLocalPackages locals <> extraToBuild0
235234
, localNames = Set.fromList $ map (packageName . lpPackage) locals
236235
}
237-
238-
toolMap = getToolMap ls0
236+
where
237+
toolMap = getToolMap ls0 lp
239238

240239
-- | State to be maintained during the calculation of local packages
241240
-- to unregister.
@@ -795,51 +794,36 @@ packageDepsWithTools p = do
795794
ctx <- ask
796795
-- TODO: it would be cool to defer these warnings until there's an
797796
-- actual issue building the package.
798-
let toEither (Cabal.Dependency (Cabal.unPackageName -> name) _) mp =
797+
let toEither name mp =
799798
case Map.toList mp of
800-
[] -> Left (NoToolFound name (packageName p))
801799
[_] -> Right mp
802-
xs -> Left (AmbiguousToolsFound name (packageName p) (map fst xs))
800+
xs -> Left (ToolWarning name (packageName p) (map fst xs))
803801
(warnings0, toolDeps) =
804802
partitionEithers $
805-
map (\dep -> toEither dep (toolToPackages ctx dep)) (packageTools p)
803+
map (\dep -> toEither dep (toolToPackages ctx dep)) (Map.keys (packageTools p))
806804
-- Check whether the tool is on the PATH before warning about it.
807-
warnings <- fmap catMaybes $ forM warnings0 $ \warning -> do
808-
let toolName = case warning of
809-
NoToolFound tool _ -> tool
810-
AmbiguousToolsFound tool _ _ -> tool
805+
warnings <- fmap catMaybes $ forM warnings0 $ \warning@(ToolWarning (ExeName toolName) _ _) -> do
811806
config <- view configL
812807
menv <- liftIO $ configEnvOverride config minimalEnvSettings { esIncludeLocals = True }
813-
mfound <- findExecutable menv toolName
808+
mfound <- findExecutable menv $ T.unpack toolName
814809
case mfound of
815810
Nothing -> return (Just warning)
816811
Just _ -> return Nothing
817812
tell mempty { wWarnings = (map toolWarningText warnings ++) }
818-
when (any isNoToolFound warnings) $ do
819-
let msg = T.unlines
820-
[ "Missing build-tools may be caused by dependencies of the build-tool being overridden by extra-deps."
821-
, "This should be fixed soon - see this issue https://github.com/commercialhaskell/stack/issues/595"
822-
]
823-
tell mempty { wWarnings = (msg:) }
824813
return $ Map.unionsWith intersectVersionRanges
825814
$ packageDeps p
826815
: toolDeps
827816

828-
data ToolWarning
829-
= NoToolFound String PackageName
830-
| AmbiguousToolsFound String PackageName [PackageName]
831-
832-
isNoToolFound :: ToolWarning -> Bool
833-
isNoToolFound NoToolFound{} = True
834-
isNoToolFound _ = False
817+
data ToolWarning = ToolWarning ExeName PackageName [PackageName]
818+
deriving Show
835819

836820
toolWarningText :: ToolWarning -> Text
837-
toolWarningText (NoToolFound toolName pkgName) =
821+
toolWarningText (ToolWarning (ExeName toolName) pkgName []) =
838822
"No packages found in snapshot which provide a " <>
839823
T.pack (show toolName) <>
840824
" executable, which is a build-tool dependency of " <>
841825
T.pack (show (packageNameString pkgName))
842-
toolWarningText (AmbiguousToolsFound toolName pkgName options) =
826+
toolWarningText (ToolWarning (ExeName toolName) pkgName options) =
843827
"Multiple packages found in snapshot which provide a " <>
844828
T.pack (show toolName) <>
845829
" exeuctable, which is a build-tool dependency of " <>

src/Stack/BuildPlan.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ instance Show BuildPlanException where
152152
-- both snapshot and local packages (deps and project packages).
153153
getToolMap :: LoadedSnapshot
154154
-> LocalPackages
155-
-> Map Text (Set PackageName)
155+
-> Map ExeName (Set PackageName)
156156
getToolMap ls locals =
157157

158158
{- We no longer do this, following discussion at:
@@ -171,13 +171,13 @@ getToolMap ls locals =
171171
]
172172
where
173173
goSnap (pname, lpi) =
174-
map (flip Map.singleton (Set.singleton pname) . unExeName)
174+
map (flip Map.singleton (Set.singleton pname))
175175
$ Set.toList
176176
$ lpiProvidedExes lpi
177177

178178
goLocalProj (pname, lpv) =
179179
map (flip Map.singleton (Set.singleton pname))
180-
[t | CExe t <- Set.toList (lpvComponents lpv)]
180+
[ExeName t | CExe t <- Set.toList (lpvComponents lpv)]
181181

182182
goLocalDep (pname, (gpd, _loc)) =
183183
map (flip Map.singleton (Set.singleton pname))
@@ -186,8 +186,8 @@ getToolMap ls locals =
186186
-- TODO consider doing buildable checking. Not a big deal though:
187187
-- worse case scenario is we build an extra package that wasn't
188188
-- strictly needed.
189-
gpdExes :: GenericPackageDescription -> [Text]
190-
gpdExes = map (T.pack . C.unUnqualComponentName . fst) . condExecutables
189+
gpdExes :: GenericPackageDescription -> [ExeName]
190+
gpdExes = map (ExeName . T.pack . C.unUnqualComponentName . fst) . condExecutables
191191

192192
gpdPackages :: [GenericPackageDescription] -> Map PackageName Version
193193
gpdPackages gpds = Map.fromList $

src/Stack/Package.hs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import Distribution.Simple.Utils
6565
import Distribution.System (OS (..), Arch, Platform (..))
6666
import qualified Distribution.Text as D
6767
import qualified Distribution.Types.CondTree as Cabal
68-
import qualified Distribution.Types.Dependency as Cabal
6968
import qualified Distribution.Types.ExeDependency as Cabal
7069
import qualified Distribution.Types.LegacyExeDependency as Cabal
7170
import qualified Distribution.Types.UnqualComponentName as Cabal
@@ -83,7 +82,7 @@ import Stack.Constants.Config
8382
import Stack.Prelude
8483
import Stack.PrettyPrint
8584
import Stack.Types.Build
86-
import Stack.Types.BuildPlan (PackageLocationIndex (..), PackageLocation (..))
85+
import Stack.Types.BuildPlan (PackageLocationIndex (..), PackageLocation (..), ExeName (..))
8786
import Stack.Types.Compiler
8887
import Stack.Types.Config
8988
import Stack.Types.FlagName
@@ -222,9 +221,7 @@ packageFromPackageDescription packageConfig pkgFlags pkg =
222221
, packageLicense = license pkg
223222
, packageDeps = deps
224223
, packageFiles = pkgFiles
225-
, packageTools = map
226-
(\(Cabal.ExeDependency name' _ range) -> Cabal.Dependency name' range)
227-
(packageDescTools pkg)
224+
, packageTools = packageDescTools pkg
228225
, packageGhcOptions = packageConfigGhcOptions packageConfig
229226
, packageFlags = packageConfigFlags packageConfig
230227
, packageDefaultFlags = M.fromList
@@ -544,8 +541,20 @@ packageToolDependencies =
544541
allBuildInfo
545542

546543
-- | Get all dependencies of the package (buildable targets only).
547-
packageDescTools :: PackageDescription -> [Cabal.ExeDependency]
548-
packageDescTools = concatMap buildToolDepends . allBuildInfo
544+
--
545+
-- This uses both the new 'buildToolDepends' and old 'buildTools'
546+
-- information.
547+
packageDescTools :: PackageDescription -> Map ExeName VersionRange
548+
packageDescTools =
549+
M.fromList . concatMap tools . allBuildInfo
550+
where
551+
tools bi = map go1 (buildTools bi) ++ map go2 (buildToolDepends bi)
552+
553+
go1 :: Cabal.LegacyExeDependency -> (ExeName, VersionRange)
554+
go1 (Cabal.LegacyExeDependency name range) = (ExeName $ T.pack name, range)
555+
556+
go2 :: Cabal.ExeDependency -> (ExeName, VersionRange)
557+
go2 (Cabal.ExeDependency _pkg name range) = (ExeName $ T.pack $ Cabal.unUnqualComponentName name, range)
549558

550559
-- | Get all files referenced by the package.
551560
packageDescModulesAndFiles

src/Stack/Types/Package.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import Data.Text.Encoding (encodeUtf8, decodeUtf8)
2121
import Distribution.InstalledPackageInfo (PError)
2222
import Distribution.License (License)
2323
import Distribution.ModuleName (ModuleName)
24-
import Distribution.Package hiding (Package,PackageName,packageName,packageVersion,PackageIdentifier)
2524
import Distribution.PackageDescription (TestSuiteInterface, BuildType)
2625
import Distribution.System (Platform (..))
2726
import Path as FL
28-
import Stack.Types.BuildPlan (PackageLocation, PackageLocationIndex (..))
27+
import Stack.Types.BuildPlan (PackageLocation, PackageLocationIndex (..), ExeName)
2928
import Stack.Types.Compiler
3029
import Stack.Types.Config
3130
import Stack.Types.FlagName
@@ -77,7 +76,7 @@ data Package =
7776
,packageLicense :: !License -- ^ The license the package was released under.
7877
,packageFiles :: !GetPackageFiles -- ^ Get all files of the package.
7978
,packageDeps :: !(Map PackageName VersionRange) -- ^ Packages that the package depends on.
80-
,packageTools :: ![Dependency] -- ^ A build tool name.
79+
,packageTools :: !(Map ExeName VersionRange) -- ^ A build tool name.
8180
,packageAllDeps :: !(Set PackageName) -- ^ Original dependencies (not sieved).
8281
,packageGhcOptions :: ![Text] -- ^ Ghc options used on package.
8382
,packageFlags :: !(Map FlagName Bool) -- ^ Flags used on package.

0 commit comments

Comments
 (0)