Skip to content

Commit

Permalink
Revert "Drop sub-component targets (haskell#8966)"
Browse files Browse the repository at this point in the history
This reverts commit 3f4c81f.
  • Loading branch information
fendor authored and erikd committed Apr 21, 2024
1 parent 096c612 commit 31ca325
Show file tree
Hide file tree
Showing 29 changed files with 1,409 additions and 183 deletions.
400 changes: 364 additions & 36 deletions Cabal/src/Distribution/Simple/BuildTarget.hs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cabal-install/cabal-install.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ test-suite unit-tests
UnitTests.Distribution.Client.InstallPlan
UnitTests.Distribution.Client.JobControl
UnitTests.Distribution.Client.ProjectConfig
UnitTests.Distribution.Client.ProjectPlanning
UnitTests.Distribution.Client.Store
UnitTests.Distribution.Client.Tar
UnitTests.Distribution.Client.Targets
Expand Down
37 changes: 33 additions & 4 deletions cabal-install/src/Distribution/Client/CmdBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Distribution.Client.CmdBench

-- * Internals exposed for testing
, componentNotBenchmarkProblem
, isSubComponentProblem
, noBenchmarksProblem
, selectPackageTargets
, selectComponentTarget
Expand Down Expand Up @@ -196,17 +197,25 @@ selectPackageTargets targetSelector targets
-- For the @bench@ command we just need to check it is a benchmark, in addition
-- to the basic checks on being buildable etc.
selectComponentTarget
:: AvailableTarget k
:: SubComponentTarget
-> AvailableTarget k
-> Either BenchTargetProblem k
selectComponentTarget t
selectComponentTarget subtarget@WholeComponent t
| CBenchName _ <- availableTargetComponentName t =
selectComponentTargetBasic t
selectComponentTargetBasic subtarget t
| otherwise =
Left
( componentNotBenchmarkProblem
(availableTargetPackageId t)
(availableTargetComponentName t)
)
selectComponentTarget subtarget t =
Left
( isSubComponentProblem
(availableTargetPackageId t)
(availableTargetComponentName t)
subtarget
)

-- | The various error conditions that can occur when matching a
-- 'TargetSelector' against 'AvailableTarget's for the @bench@ command.
Expand All @@ -215,6 +224,8 @@ data BenchProblem
TargetProblemNoBenchmarks TargetSelector
| -- | The 'TargetSelector' refers to a component that is not a benchmark
TargetProblemComponentNotBenchmark PackageId ComponentName
| -- | Asking to benchmark an individual file or module is not supported
TargetProblemIsSubComponent PackageId ComponentName SubComponentTarget
deriving (Eq, Show)

type BenchTargetProblem = TargetProblem BenchProblem
Expand All @@ -227,6 +238,15 @@ componentNotBenchmarkProblem pkgid name =
CustomTargetProblem $
TargetProblemComponentNotBenchmark pkgid name

isSubComponentProblem
:: PackageId
-> ComponentName
-> SubComponentTarget
-> TargetProblem BenchProblem
isSubComponentProblem pkgid name subcomponent =
CustomTargetProblem $
TargetProblemIsSubComponent pkgid name subcomponent

reportTargetProblems :: Verbosity -> [BenchTargetProblem] -> IO a
reportTargetProblems verbosity =
dieWithException verbosity . RenderBenchTargetProblem . map renderBenchTargetProblem
Expand Down Expand Up @@ -263,4 +283,13 @@ renderBenchProblem (TargetProblemComponentNotBenchmark pkgid cname) =
++ prettyShow pkgid
++ "."
where
targetSelector = TargetComponent pkgid cname
targetSelector = TargetComponent pkgid cname WholeComponent
renderBenchProblem (TargetProblemIsSubComponent pkgid cname subtarget) =
"The bench command can only run benchmarks as a whole, "
++ "not files or modules within them, but the target '"
++ showTargetSelector targetSelector
++ "' refers to "
++ renderTargetSelector targetSelector
++ "."
where
targetSelector = TargetComponent pkgid cname subtarget
3 changes: 2 additions & 1 deletion cabal-install/src/Distribution/Client/CmdBuild.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ selectPackageTargets targetSelector targets
--
-- For the @build@ command we just need the basic checks on being buildable etc.
selectComponentTarget
:: AvailableTarget k
:: SubComponentTarget
-> AvailableTarget k
-> Either TargetProblem' k
selectComponentTarget = selectComponentTargetBasic

Expand Down
31 changes: 21 additions & 10 deletions cabal-install/src/Distribution/Client/CmdErrorMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Distribution.Client.TargetProblem
import Distribution.Client.TargetSelector
( ComponentKind (..)
, ComponentKindFilter
, SubComponentTarget (..)
, TargetSelector (..)
, componentKind
, showTargetSelector
Expand Down Expand Up @@ -141,18 +142,28 @@ renderTargetSelector (TargetAllPackages (Just kfilter)) =
"all the "
++ renderComponentKind Plural kfilter
++ " in the project"
renderTargetSelector (TargetComponent pkgid cname) =
"the "
renderTargetSelector (TargetComponent pkgid cname subtarget) =
renderSubComponentTarget subtarget
++ "the "
++ renderComponentName (packageName pkgid) cname
renderTargetSelector (TargetComponentUnknown pkgname (Left ucname)) =
"the component "
renderTargetSelector (TargetComponentUnknown pkgname (Left ucname) subtarget) =
renderSubComponentTarget subtarget
++ "the component "
++ prettyShow ucname
++ " in the package "
++ prettyShow pkgname
renderTargetSelector (TargetComponentUnknown pkgname (Right cname)) =
"the "
renderTargetSelector (TargetComponentUnknown pkgname (Right cname) subtarget) =
renderSubComponentTarget subtarget
++ "the "
++ renderComponentName pkgname cname

renderSubComponentTarget :: SubComponentTarget -> String
renderSubComponentTarget WholeComponent = ""
renderSubComponentTarget (FileTarget filename) =
"the file " ++ filename ++ " in "
renderSubComponentTarget (ModuleTarget modname) =
"the module " ++ prettyShow modname ++ " in "

renderOptionalStanza :: Plural -> OptionalStanza -> String
renderOptionalStanza Singular TestStanzas = "test suite"
renderOptionalStanza Plural TestStanzas = "test suites"
Expand Down Expand Up @@ -249,7 +260,7 @@ renderTargetProblem verb _ (TargetAvailableInIndex pkgname) =
++ "in this project (either directly or indirectly), but it is in the current "
++ "package index. If you want to add it to the project then edit the "
++ "cabal.project file."
renderTargetProblem verb _ (TargetComponentNotProjectLocal pkgid cname) =
renderTargetProblem verb _ (TargetComponentNotProjectLocal pkgid cname _) =
"Cannot "
++ verb
++ " the "
Expand All @@ -262,7 +273,7 @@ renderTargetProblem verb _ (TargetComponentNotProjectLocal pkgid cname) =
++ "non-local dependencies. To run test suites or benchmarks from "
++ "dependencies you can unpack the package locally and adjust the "
++ "cabal.project file to include that package directory."
renderTargetProblem verb _ (TargetComponentNotBuildable pkgid cname) =
renderTargetProblem verb _ (TargetComponentNotBuildable pkgid cname _) =
"Cannot "
++ verb
++ " the "
Expand All @@ -275,7 +286,7 @@ renderTargetProblem verb _ (TargetComponentNotBuildable pkgid cname) =
++ "property is conditional on flags. Alternatively you may simply have to "
++ "edit the .cabal file to declare it as buildable and fix any resulting "
++ "build problems."
renderTargetProblem verb _ (TargetOptionalStanzaDisabledByUser _ cname) =
renderTargetProblem verb _ (TargetOptionalStanzaDisabledByUser _ cname _) =
"Cannot "
++ verb
++ " the "
Expand All @@ -294,7 +305,7 @@ renderTargetProblem verb _ (TargetOptionalStanzaDisabledByUser _ cname) =
++ "explanation."
where
compkinds = renderComponentKind Plural (componentKind cname)
renderTargetProblem verb _ (TargetOptionalStanzaDisabledBySolver pkgid cname) =
renderTargetProblem verb _ (TargetOptionalStanzaDisabledBySolver pkgid cname _) =
"Cannot "
++ verb
++ " the "
Expand Down
3 changes: 2 additions & 1 deletion cabal-install/src/Distribution/Client/CmdHaddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ selectPackageTargets haddockFlags targetSelector targets
-- For the @haddock@ command we just need the basic checks on being buildable
-- etc.
selectComponentTarget
:: AvailableTarget k
:: SubComponentTarget
-> AvailableTarget k
-> Either TargetProblem' k
selectComponentTarget = selectComponentTargetBasic

Expand Down
11 changes: 6 additions & 5 deletions cabal-install/src/Distribution/Client/CmdInstall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ partitionToKnownTargetsAndHackagePackages verbosity pkgDb elaboratedPlan targetS

let
targetSelectors' = flip filter targetSelectors $ \case
TargetComponentUnknown name _
TargetComponentUnknown name _ _
| name `elem` hackageNames -> False
TargetPackageNamed name _
| name `elem` hackageNames -> False
Expand Down Expand Up @@ -954,7 +954,7 @@ warnIfNoExes verbosity buildCtx =
selectors = concatMap (NE.toList . snd) targets
noExes = null $ catMaybes $ exeMaybe <$> components

exeMaybe (ComponentTarget (CExeName exe)) = Just exe
exeMaybe (ComponentTarget (CExeName exe) _) = Just exe
exeMaybe _ = Nothing

-- | Return the package specifiers and non-global environment file entries.
Expand Down Expand Up @@ -1034,7 +1034,7 @@ installCheckUnitExes
else traverse_ warnAbout (zip symlinkables exes)
where
exes = catMaybes $ (exeMaybe . fst) <$> components
exeMaybe (ComponentTarget (CExeName exe)) = Just exe
exeMaybe (ComponentTarget (CExeName exe) _) = Just exe
exeMaybe _ = Nothing

warnAbout (True, _) = return ()
Expand Down Expand Up @@ -1136,7 +1136,7 @@ entriesForLibraryComponents :: TargetsMap -> [GhcEnvironmentFileEntry]
entriesForLibraryComponents = Map.foldrWithKey' (\k v -> mappend (go k v)) []
where
hasLib :: (ComponentTarget, NonEmpty TargetSelector) -> Bool
hasLib (ComponentTarget (CLibName _), _) = True
hasLib (ComponentTarget (CLibName _) _, _) = True
hasLib _ = False

go
Expand Down Expand Up @@ -1262,7 +1262,8 @@ selectPackageTargets targetSelector targets
--
-- For the @build@ command we just need the basic checks on being buildable etc.
selectComponentTarget
:: AvailableTarget k
:: SubComponentTarget
-> AvailableTarget k
-> Either TargetProblem' k
selectComponentTarget = selectComponentTargetBasic

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ woPackageTargets :: WithoutProjectTargetSelector -> TargetSelector
woPackageTargets (WoPackageId pid) =
TargetPackageNamed (pkgName pid) Nothing
woPackageTargets (WoPackageComponent pid cn) =
TargetComponentUnknown (pkgName pid) (Right cn)
TargetComponentUnknown (pkgName pid) (Right cn) WholeComponent
woPackageTargets (WoURI _) =
TargetAllPackages (Just ExeKind)

Expand Down
36 changes: 32 additions & 4 deletions cabal-install/src/Distribution/Client/CmdListBin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ selectPackageTargets targetSelector targets
-- (an executable, a test, or a benchmark), in addition
-- to the basic checks on being buildable etc.
selectComponentTarget
:: AvailableTarget k
:: SubComponentTarget
-> AvailableTarget k
-> Either ListBinTargetProblem k
selectComponentTarget t =
selectComponentTarget subtarget@WholeComponent t =
case availableTargetComponentName t of
CExeName _ -> component
CTestName _ -> component
Expand All @@ -302,7 +303,14 @@ selectComponentTarget t =
where
pkgid = availableTargetPackageId t
cname = availableTargetComponentName t
component = selectComponentTargetBasic t
component = selectComponentTargetBasic subtarget t
selectComponentTarget subtarget t =
Left
( isSubComponentProblem
(availableTargetPackageId t)
(availableTargetComponentName t)
subtarget
)

-- | The various error conditions that can occur when matching a
-- 'TargetSelector' against 'AvailableTarget's for the @run@ command.
Expand All @@ -315,6 +323,8 @@ data ListBinProblem
TargetProblemMultipleTargets TargetsMap
| -- | The 'TargetSelector' refers to a component that is not an executable
TargetProblemComponentNotRightKind PackageId ComponentName
| -- | Asking to run an individual file or module is not supported
TargetProblemIsSubComponent PackageId ComponentName SubComponentTarget
deriving (Eq, Show)

type ListBinTargetProblem = TargetProblem ListBinProblem
Expand All @@ -335,6 +345,15 @@ componentNotRightKindProblem pkgid name =
CustomTargetProblem $
TargetProblemComponentNotRightKind pkgid name

isSubComponentProblem
:: PackageId
-> ComponentName
-> SubComponentTarget
-> TargetProblem ListBinProblem
isSubComponentProblem pkgid name subcomponent =
CustomTargetProblem $
TargetProblemIsSubComponent pkgid name subcomponent

reportTargetProblems :: Verbosity -> [ListBinTargetProblem] -> IO a
reportTargetProblems verbosity =
dieWithException verbosity . ListBinTargetException . unlines . map renderListBinTargetProblem
Expand Down Expand Up @@ -385,7 +404,16 @@ renderListBinProblem (TargetProblemComponentNotRightKind pkgid cname) =
++ prettyShow pkgid
++ "."
where
targetSelector = TargetComponent pkgid cname
targetSelector = TargetComponent pkgid cname WholeComponent
renderListBinProblem (TargetProblemIsSubComponent pkgid cname subtarget) =
"The list-bin command can only find a binary as a whole, "
++ "not files or modules within them, but the target '"
++ showTargetSelector targetSelector
++ "' refers to "
++ renderTargetSelector targetSelector
++ "."
where
targetSelector = TargetComponent pkgid cname subtarget
renderListBinProblem (TargetProblemNoRightComps targetSelector) =
"Cannot list-bin the target '"
++ showTargetSelector targetSelector
Expand Down
3 changes: 2 additions & 1 deletion cabal-install/src/Distribution/Client/CmdRepl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,8 @@ selectPackageTargetsSingle decision targetSelector targets
--
-- For the @repl@ command we just need the basic checks on being buildable etc.
selectComponentTarget
:: AvailableTarget k
:: SubComponentTarget
-> AvailableTarget k
-> Either ReplTargetProblem k
selectComponentTarget = selectComponentTargetBasic

Expand Down
36 changes: 32 additions & 4 deletions cabal-install/src/Distribution/Client/CmdRun.hs
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,10 @@ selectPackageTargets targetSelector targets
-- (an executable, a test, or a benchmark), in addition
-- to the basic checks on being buildable etc.
selectComponentTarget
:: AvailableTarget k
:: SubComponentTarget
-> AvailableTarget k
-> Either RunTargetProblem k
selectComponentTarget t =
selectComponentTarget subtarget@WholeComponent t =
case availableTargetComponentName t of
CExeName _ -> component
CTestName _ -> component
Expand All @@ -450,7 +451,14 @@ selectComponentTarget t =
where
pkgid = availableTargetPackageId t
cname = availableTargetComponentName t
component = selectComponentTargetBasic t
component = selectComponentTargetBasic subtarget t
selectComponentTarget subtarget t =
Left
( isSubComponentProblem
(availableTargetPackageId t)
(availableTargetComponentName t)
subtarget
)

-- | The various error conditions that can occur when matching a
-- 'TargetSelector' against 'AvailableTarget's for the @run@ command.
Expand All @@ -463,6 +471,8 @@ data RunProblem
TargetProblemMultipleTargets TargetsMap
| -- | The 'TargetSelector' refers to a component that is not an executable
TargetProblemComponentNotExe PackageId ComponentName
| -- | Asking to run an individual file or module is not supported
TargetProblemIsSubComponent PackageId ComponentName SubComponentTarget
deriving (Eq, Show)

type RunTargetProblem = TargetProblem RunProblem
Expand All @@ -483,6 +493,15 @@ componentNotExeProblem pkgid name =
CustomTargetProblem $
TargetProblemComponentNotExe pkgid name

isSubComponentProblem
:: PackageId
-> ComponentName
-> SubComponentTarget
-> TargetProblem RunProblem
isSubComponentProblem pkgid name subcomponent =
CustomTargetProblem $
TargetProblemIsSubComponent pkgid name subcomponent

reportTargetProblems :: Verbosity -> [RunTargetProblem] -> IO a
reportTargetProblems verbosity =
dieWithException verbosity . CmdRunReportTargetProblems . unlines . map renderRunTargetProblem
Expand Down Expand Up @@ -536,7 +555,16 @@ renderRunProblem (TargetProblemComponentNotExe pkgid cname) =
++ prettyShow pkgid
++ "."
where
targetSelector = TargetComponent pkgid cname
targetSelector = TargetComponent pkgid cname WholeComponent
renderRunProblem (TargetProblemIsSubComponent pkgid cname subtarget) =
"The run command can only run an executable as a whole, "
++ "not files or modules within them, but the target '"
++ showTargetSelector targetSelector
++ "' refers to "
++ renderTargetSelector targetSelector
++ "."
where
targetSelector = TargetComponent pkgid cname subtarget
renderRunProblem (TargetProblemNoExes targetSelector) =
"Cannot run the target '"
++ showTargetSelector targetSelector
Expand Down
Loading

0 comments on commit 31ca325

Please sign in to comment.