Skip to content

Commit 0e66a4b

Browse files
authored
Merge pull request #9690 from fendor/fix/dont-drop-subcomponent-fp-targets
Revert "Drop sub-component targets (#8966)"
2 parents 5a67423 + b5f0ce4 commit 0e66a4b

29 files changed

+1409
-183
lines changed

Cabal/src/Distribution/Simple/BuildTarget.hs

Lines changed: 364 additions & 36 deletions
Large diffs are not rendered by default.

cabal-install/cabal-install.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ test-suite unit-tests
310310
UnitTests.Distribution.Client.InstallPlan
311311
UnitTests.Distribution.Client.JobControl
312312
UnitTests.Distribution.Client.ProjectConfig
313+
UnitTests.Distribution.Client.ProjectPlanning
313314
UnitTests.Distribution.Client.Store
314315
UnitTests.Distribution.Client.Tar
315316
UnitTests.Distribution.Client.Targets

cabal-install/src/Distribution/Client/CmdBench.hs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Distribution.Client.CmdBench
88

99
-- * Internals exposed for testing
1010
, componentNotBenchmarkProblem
11+
, isSubComponentProblem
1112
, noBenchmarksProblem
1213
, selectPackageTargets
1314
, selectComponentTarget
@@ -196,17 +197,25 @@ selectPackageTargets targetSelector targets
196197
-- For the @bench@ command we just need to check it is a benchmark, in addition
197198
-- to the basic checks on being buildable etc.
198199
selectComponentTarget
199-
:: AvailableTarget k
200+
:: SubComponentTarget
201+
-> AvailableTarget k
200202
-> Either BenchTargetProblem k
201-
selectComponentTarget t
203+
selectComponentTarget subtarget@WholeComponent t
202204
| CBenchName _ <- availableTargetComponentName t =
203-
selectComponentTargetBasic t
205+
selectComponentTargetBasic subtarget t
204206
| otherwise =
205207
Left
206208
( componentNotBenchmarkProblem
207209
(availableTargetPackageId t)
208210
(availableTargetComponentName t)
209211
)
212+
selectComponentTarget subtarget t =
213+
Left
214+
( isSubComponentProblem
215+
(availableTargetPackageId t)
216+
(availableTargetComponentName t)
217+
subtarget
218+
)
210219

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

220231
type BenchTargetProblem = TargetProblem BenchProblem
@@ -227,6 +238,15 @@ componentNotBenchmarkProblem pkgid name =
227238
CustomTargetProblem $
228239
TargetProblemComponentNotBenchmark pkgid name
229240

241+
isSubComponentProblem
242+
:: PackageId
243+
-> ComponentName
244+
-> SubComponentTarget
245+
-> TargetProblem BenchProblem
246+
isSubComponentProblem pkgid name subcomponent =
247+
CustomTargetProblem $
248+
TargetProblemIsSubComponent pkgid name subcomponent
249+
230250
reportTargetProblems :: Verbosity -> [BenchTargetProblem] -> IO a
231251
reportTargetProblems verbosity =
232252
dieWithException verbosity . RenderBenchTargetProblem . map renderBenchTargetProblem
@@ -263,4 +283,13 @@ renderBenchProblem (TargetProblemComponentNotBenchmark pkgid cname) =
263283
++ prettyShow pkgid
264284
++ "."
265285
where
266-
targetSelector = TargetComponent pkgid cname
286+
targetSelector = TargetComponent pkgid cname WholeComponent
287+
renderBenchProblem (TargetProblemIsSubComponent pkgid cname subtarget) =
288+
"The bench command can only run benchmarks as a whole, "
289+
++ "not files or modules within them, but the target '"
290+
++ showTargetSelector targetSelector
291+
++ "' refers to "
292+
++ renderTargetSelector targetSelector
293+
++ "."
294+
where
295+
targetSelector = TargetComponent pkgid cname subtarget

cabal-install/src/Distribution/Client/CmdBuild.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ selectPackageTargets targetSelector targets
226226
--
227227
-- For the @build@ command we just need the basic checks on being buildable etc.
228228
selectComponentTarget
229-
:: AvailableTarget k
229+
:: SubComponentTarget
230+
-> AvailableTarget k
230231
-> Either TargetProblem' k
231232
selectComponentTarget = selectComponentTargetBasic
232233

cabal-install/src/Distribution/Client/CmdErrorMessages.hs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Distribution.Client.TargetProblem
2424
import Distribution.Client.TargetSelector
2525
( ComponentKind (..)
2626
, ComponentKindFilter
27+
, SubComponentTarget (..)
2728
, TargetSelector (..)
2829
, componentKind
2930
, showTargetSelector
@@ -141,18 +142,28 @@ renderTargetSelector (TargetAllPackages (Just kfilter)) =
141142
"all the "
142143
++ renderComponentKind Plural kfilter
143144
++ " in the project"
144-
renderTargetSelector (TargetComponent pkgid cname) =
145-
"the "
145+
renderTargetSelector (TargetComponent pkgid cname subtarget) =
146+
renderSubComponentTarget subtarget
147+
++ "the "
146148
++ renderComponentName (packageName pkgid) cname
147-
renderTargetSelector (TargetComponentUnknown pkgname (Left ucname)) =
148-
"the component "
149+
renderTargetSelector (TargetComponentUnknown pkgname (Left ucname) subtarget) =
150+
renderSubComponentTarget subtarget
151+
++ "the component "
149152
++ prettyShow ucname
150153
++ " in the package "
151154
++ prettyShow pkgname
152-
renderTargetSelector (TargetComponentUnknown pkgname (Right cname)) =
153-
"the "
155+
renderTargetSelector (TargetComponentUnknown pkgname (Right cname) subtarget) =
156+
renderSubComponentTarget subtarget
157+
++ "the "
154158
++ renderComponentName pkgname cname
155159

160+
renderSubComponentTarget :: SubComponentTarget -> String
161+
renderSubComponentTarget WholeComponent = ""
162+
renderSubComponentTarget (FileTarget filename) =
163+
"the file " ++ filename ++ " in "
164+
renderSubComponentTarget (ModuleTarget modname) =
165+
"the module " ++ prettyShow modname ++ " in "
166+
156167
renderOptionalStanza :: Plural -> OptionalStanza -> String
157168
renderOptionalStanza Singular TestStanzas = "test suite"
158169
renderOptionalStanza Plural TestStanzas = "test suites"
@@ -249,7 +260,7 @@ renderTargetProblem verb _ (TargetAvailableInIndex pkgname) =
249260
++ "in this project (either directly or indirectly), but it is in the current "
250261
++ "package index. If you want to add it to the project then edit the "
251262
++ "cabal.project file."
252-
renderTargetProblem verb _ (TargetComponentNotProjectLocal pkgid cname) =
263+
renderTargetProblem verb _ (TargetComponentNotProjectLocal pkgid cname _) =
253264
"Cannot "
254265
++ verb
255266
++ " the "
@@ -262,7 +273,7 @@ renderTargetProblem verb _ (TargetComponentNotProjectLocal pkgid cname) =
262273
++ "non-local dependencies. To run test suites or benchmarks from "
263274
++ "dependencies you can unpack the package locally and adjust the "
264275
++ "cabal.project file to include that package directory."
265-
renderTargetProblem verb _ (TargetComponentNotBuildable pkgid cname) =
276+
renderTargetProblem verb _ (TargetComponentNotBuildable pkgid cname _) =
266277
"Cannot "
267278
++ verb
268279
++ " the "
@@ -275,7 +286,7 @@ renderTargetProblem verb _ (TargetComponentNotBuildable pkgid cname) =
275286
++ "property is conditional on flags. Alternatively you may simply have to "
276287
++ "edit the .cabal file to declare it as buildable and fix any resulting "
277288
++ "build problems."
278-
renderTargetProblem verb _ (TargetOptionalStanzaDisabledByUser _ cname) =
289+
renderTargetProblem verb _ (TargetOptionalStanzaDisabledByUser _ cname _) =
279290
"Cannot "
280291
++ verb
281292
++ " the "
@@ -294,7 +305,7 @@ renderTargetProblem verb _ (TargetOptionalStanzaDisabledByUser _ cname) =
294305
++ "explanation."
295306
where
296307
compkinds = renderComponentKind Plural (componentKind cname)
297-
renderTargetProblem verb _ (TargetOptionalStanzaDisabledBySolver pkgid cname) =
308+
renderTargetProblem verb _ (TargetOptionalStanzaDisabledBySolver pkgid cname _) =
298309
"Cannot "
299310
++ verb
300311
++ " the "

cabal-install/src/Distribution/Client/CmdHaddock.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ selectPackageTargets haddockFlags targetSelector targets
268268
-- For the @haddock@ command we just need the basic checks on being buildable
269269
-- etc.
270270
selectComponentTarget
271-
:: AvailableTarget k
271+
:: SubComponentTarget
272+
-> AvailableTarget k
272273
-> Either TargetProblem' k
273274
selectComponentTarget = selectComponentTargetBasic
274275

cabal-install/src/Distribution/Client/CmdInstall.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ partitionToKnownTargetsAndHackagePackages verbosity pkgDb elaboratedPlan targetS
774774

775775
let
776776
targetSelectors' = flip filter targetSelectors $ \case
777-
TargetComponentUnknown name _
777+
TargetComponentUnknown name _ _
778778
| name `elem` hackageNames -> False
779779
TargetPackageNamed name _
780780
| name `elem` hackageNames -> False
@@ -954,7 +954,7 @@ warnIfNoExes verbosity buildCtx =
954954
selectors = concatMap (NE.toList . snd) targets
955955
noExes = null $ catMaybes $ exeMaybe <$> components
956956

957-
exeMaybe (ComponentTarget (CExeName exe)) = Just exe
957+
exeMaybe (ComponentTarget (CExeName exe) _) = Just exe
958958
exeMaybe _ = Nothing
959959

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

10401040
warnAbout (True, _) = return ()
@@ -1136,7 +1136,7 @@ entriesForLibraryComponents :: TargetsMap -> [GhcEnvironmentFileEntry]
11361136
entriesForLibraryComponents = Map.foldrWithKey' (\k v -> mappend (go k v)) []
11371137
where
11381138
hasLib :: (ComponentTarget, NonEmpty TargetSelector) -> Bool
1139-
hasLib (ComponentTarget (CLibName _), _) = True
1139+
hasLib (ComponentTarget (CLibName _) _, _) = True
11401140
hasLib _ = False
11411141

11421142
go
@@ -1262,7 +1262,8 @@ selectPackageTargets targetSelector targets
12621262
--
12631263
-- For the @build@ command we just need the basic checks on being buildable etc.
12641264
selectComponentTarget
1265-
:: AvailableTarget k
1265+
:: SubComponentTarget
1266+
-> AvailableTarget k
12661267
-> Either TargetProblem' k
12671268
selectComponentTarget = selectComponentTargetBasic
12681269

cabal-install/src/Distribution/Client/CmdInstall/ClientInstallTargetSelector.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ woPackageTargets :: WithoutProjectTargetSelector -> TargetSelector
5252
woPackageTargets (WoPackageId pid) =
5353
TargetPackageNamed (pkgName pid) Nothing
5454
woPackageTargets (WoPackageComponent pid cn) =
55-
TargetComponentUnknown (pkgName pid) (Right cn)
55+
TargetComponentUnknown (pkgName pid) (Right cn) WholeComponent
5656
woPackageTargets (WoURI _) =
5757
TargetAllPackages (Just ExeKind)
5858

cabal-install/src/Distribution/Client/CmdListBin.hs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,10 @@ selectPackageTargets targetSelector targets
290290
-- (an executable, a test, or a benchmark), in addition
291291
-- to the basic checks on being buildable etc.
292292
selectComponentTarget
293-
:: AvailableTarget k
293+
:: SubComponentTarget
294+
-> AvailableTarget k
294295
-> Either ListBinTargetProblem k
295-
selectComponentTarget t =
296+
selectComponentTarget subtarget@WholeComponent t =
296297
case availableTargetComponentName t of
297298
CExeName _ -> component
298299
CTestName _ -> component
@@ -302,7 +303,14 @@ selectComponentTarget t =
302303
where
303304
pkgid = availableTargetPackageId t
304305
cname = availableTargetComponentName t
305-
component = selectComponentTargetBasic t
306+
component = selectComponentTargetBasic subtarget t
307+
selectComponentTarget subtarget t =
308+
Left
309+
( isSubComponentProblem
310+
(availableTargetPackageId t)
311+
(availableTargetComponentName t)
312+
subtarget
313+
)
306314

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

320330
type ListBinTargetProblem = TargetProblem ListBinProblem
@@ -335,6 +345,15 @@ componentNotRightKindProblem pkgid name =
335345
CustomTargetProblem $
336346
TargetProblemComponentNotRightKind pkgid name
337347

348+
isSubComponentProblem
349+
:: PackageId
350+
-> ComponentName
351+
-> SubComponentTarget
352+
-> TargetProblem ListBinProblem
353+
isSubComponentProblem pkgid name subcomponent =
354+
CustomTargetProblem $
355+
TargetProblemIsSubComponent pkgid name subcomponent
356+
338357
reportTargetProblems :: Verbosity -> [ListBinTargetProblem] -> IO a
339358
reportTargetProblems verbosity =
340359
dieWithException verbosity . ListBinTargetException . unlines . map renderListBinTargetProblem
@@ -385,7 +404,16 @@ renderListBinProblem (TargetProblemComponentNotRightKind pkgid cname) =
385404
++ prettyShow pkgid
386405
++ "."
387406
where
388-
targetSelector = TargetComponent pkgid cname
407+
targetSelector = TargetComponent pkgid cname WholeComponent
408+
renderListBinProblem (TargetProblemIsSubComponent pkgid cname subtarget) =
409+
"The list-bin command can only find a binary as a whole, "
410+
++ "not files or modules within them, but the target '"
411+
++ showTargetSelector targetSelector
412+
++ "' refers to "
413+
++ renderTargetSelector targetSelector
414+
++ "."
415+
where
416+
targetSelector = TargetComponent pkgid cname subtarget
389417
renderListBinProblem (TargetProblemNoRightComps targetSelector) =
390418
"Cannot list-bin the target '"
391419
++ showTargetSelector targetSelector

cabal-install/src/Distribution/Client/CmdRepl.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,8 @@ selectPackageTargetsSingle decision targetSelector targets
734734
--
735735
-- For the @repl@ command we just need the basic checks on being buildable etc.
736736
selectComponentTarget
737-
:: AvailableTarget k
737+
:: SubComponentTarget
738+
-> AvailableTarget k
738739
-> Either ReplTargetProblem k
739740
selectComponentTarget = selectComponentTargetBasic
740741

cabal-install/src/Distribution/Client/CmdRun.hs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,10 @@ selectPackageTargets targetSelector targets
439439
-- (an executable, a test, or a benchmark), in addition
440440
-- to the basic checks on being buildable etc.
441441
selectComponentTarget
442-
:: AvailableTarget k
442+
:: SubComponentTarget
443+
-> AvailableTarget k
443444
-> Either RunTargetProblem k
444-
selectComponentTarget t =
445+
selectComponentTarget subtarget@WholeComponent t =
445446
case availableTargetComponentName t of
446447
CExeName _ -> component
447448
CTestName _ -> component
@@ -450,7 +451,14 @@ selectComponentTarget t =
450451
where
451452
pkgid = availableTargetPackageId t
452453
cname = availableTargetComponentName t
453-
component = selectComponentTargetBasic t
454+
component = selectComponentTargetBasic subtarget t
455+
selectComponentTarget subtarget t =
456+
Left
457+
( isSubComponentProblem
458+
(availableTargetPackageId t)
459+
(availableTargetComponentName t)
460+
subtarget
461+
)
454462

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

468478
type RunTargetProblem = TargetProblem RunProblem
@@ -483,6 +493,15 @@ componentNotExeProblem pkgid name =
483493
CustomTargetProblem $
484494
TargetProblemComponentNotExe pkgid name
485495

496+
isSubComponentProblem
497+
:: PackageId
498+
-> ComponentName
499+
-> SubComponentTarget
500+
-> TargetProblem RunProblem
501+
isSubComponentProblem pkgid name subcomponent =
502+
CustomTargetProblem $
503+
TargetProblemIsSubComponent pkgid name subcomponent
504+
486505
reportTargetProblems :: Verbosity -> [RunTargetProblem] -> IO a
487506
reportTargetProblems verbosity =
488507
dieWithException verbosity . CmdRunReportTargetProblems . unlines . map renderRunTargetProblem
@@ -536,7 +555,16 @@ renderRunProblem (TargetProblemComponentNotExe pkgid cname) =
536555
++ prettyShow pkgid
537556
++ "."
538557
where
539-
targetSelector = TargetComponent pkgid cname
558+
targetSelector = TargetComponent pkgid cname WholeComponent
559+
renderRunProblem (TargetProblemIsSubComponent pkgid cname subtarget) =
560+
"The run command can only run an executable as a whole, "
561+
++ "not files or modules within them, but the target '"
562+
++ showTargetSelector targetSelector
563+
++ "' refers to "
564+
++ renderTargetSelector targetSelector
565+
++ "."
566+
where
567+
targetSelector = TargetComponent pkgid cname subtarget
540568
renderRunProblem (TargetProblemNoExes targetSelector) =
541569
"Cannot run the target '"
542570
++ showTargetSelector targetSelector

0 commit comments

Comments
 (0)