@@ -19,7 +19,7 @@ module Distribution.Client.TargetSelector (
19
19
TargetSelector (.. ),
20
20
TargetImplicitCwd (.. ),
21
21
ComponentKind (.. ),
22
- ComponentKindFilter ,
22
+ AmbiguityResolver ( .. ) ,
23
23
SubComponentTarget (.. ),
24
24
QualLevel (.. ),
25
25
componentKind ,
@@ -130,18 +130,18 @@ data TargetSelector =
130
130
-- These are always packages that are local to the project. In the case
131
131
-- that there is more than one, they all share the same directory location.
132
132
--
133
- TargetPackage TargetImplicitCwd [PackageId ] (Maybe ComponentKindFilter )
133
+ TargetPackage TargetImplicitCwd [PackageId ] (Maybe ComponentKind )
134
134
135
135
-- | A package specified by name. This may refer to @extra-packages@ from
136
136
-- the @cabal.project@ file, or a dependency of a known project package or
137
137
-- could refer to a package from a hackage archive. It needs further
138
138
-- context to resolve to a specific package.
139
139
--
140
- | TargetPackageNamed PackageName (Maybe ComponentKindFilter )
140
+ | TargetPackageNamed PackageName (Maybe ComponentKind )
141
141
142
142
-- | All packages, or all components of a particular kind in all packages.
143
143
--
144
- | TargetAllPackages (Maybe ComponentKindFilter )
144
+ | TargetAllPackages (Maybe ComponentKind )
145
145
146
146
-- | A specific component in a package within the project.
147
147
--
@@ -167,7 +167,16 @@ data TargetImplicitCwd = TargetImplicitCwd | TargetExplicitNamed
167
167
data ComponentKind = LibKind | FLibKind | ExeKind | TestKind | BenchKind
168
168
deriving (Eq , Ord , Enum , Show )
169
169
170
- type ComponentKindFilter = ComponentKind
170
+ -- | Whenever there is an ambiguous TargetSelector from some user input, how
171
+ -- should it be resolved?
172
+ data AmbiguityResolver =
173
+ -- | Treat ambiguity as an error
174
+ AmbiguityResolverNone
175
+ -- | Choose the first target
176
+ | AmbiguityResolverFirst
177
+ -- | Choose the target component with the specific kind
178
+ | AmbiguityResolverKind ComponentKind
179
+ deriving (Eq , Ord , Show )
171
180
172
181
-- | Either the component as a whole or detail about a file or module target
173
182
-- within a component.
@@ -199,19 +208,25 @@ instance Structured SubComponentTarget
199
208
-- the available packages (and their locations).
200
209
--
201
210
readTargetSelectors :: [PackageSpecifier (SourcePackage (PackageLocation a ))]
202
- -> Maybe ComponentKindFilter
211
+ -> AmbiguityResolver
203
212
-- ^ This parameter is used when there are ambiguous selectors.
204
- -- If it is 'Just', then we attempt to resolve ambiguitiy
205
- -- by applying it, since otherwise there is no way to allow
206
- -- contextually valid yet syntactically ambiguous selectors.
213
+ -- If it is 'AmbiguityResolverKind', then we attempt to resolve
214
+ -- ambiguitiy by applying it, since otherwise there is no
215
+ -- way to allow contextually valid yet syntactically ambiguous
216
+ -- selectors.
207
217
-- (#4676, #5461)
218
+ -- If it is 'AmbiguityResolverFirst', then we resolve it by
219
+ -- choosing just the first target. This is used by
220
+ -- the show-build-info command.
221
+ -- Otherwise, if it is 'AmbiguityResolverNone', we make
222
+ -- ambiguity a 'TargetSelectorProblem'.
208
223
-> [String ]
209
224
-> IO (Either [TargetSelectorProblem ] [TargetSelector ])
210
225
readTargetSelectors = readTargetSelectorsWith defaultDirActions
211
226
212
227
readTargetSelectorsWith :: (Applicative m , Monad m ) => DirActions m
213
228
-> [PackageSpecifier (SourcePackage (PackageLocation a ))]
214
- -> Maybe ComponentKindFilter
229
+ -> AmbiguityResolver
215
230
-> [String ]
216
231
-> m (Either [TargetSelectorProblem ] [TargetSelector ])
217
232
readTargetSelectorsWith dirActions@ DirActions {} pkgs mfilter targetStrs =
@@ -457,7 +472,7 @@ copyFileStatus src dst =
457
472
--
458
473
resolveTargetSelectors :: KnownTargets
459
474
-> [TargetStringFileStatus ]
460
- -> Maybe ComponentKindFilter
475
+ -> AmbiguityResolver
461
476
-> ([TargetSelectorProblem ],
462
477
[TargetSelector ])
463
478
-- default local dir target if there's no given target:
@@ -478,7 +493,7 @@ resolveTargetSelectors knowntargets targetStrs mfilter =
478
493
$ targetStrs
479
494
480
495
resolveTargetSelector :: KnownTargets
481
- -> Maybe ComponentKindFilter
496
+ -> AmbiguityResolver
482
497
-> TargetStringFileStatus
483
498
-> Either TargetSelectorProblem TargetSelector
484
499
resolveTargetSelector knowntargets@ KnownTargets {.. } mfilter targetStrStatus =
@@ -497,14 +512,17 @@ resolveTargetSelector knowntargets@KnownTargets{..} mfilter targetStrStatus =
497
512
| otherwise -> Left (classifyMatchErrors errs)
498
513
499
514
Ambiguous _ targets
500
- | Just kfilter <- mfilter
515
+ | AmbiguityResolverKind kfilter <- mfilter
501
516
, [target] <- applyKindFilter kfilter targets -> Right target
502
517
503
518
Ambiguous exactMatch targets ->
504
519
case disambiguateTargetSelectors
505
520
matcher targetStrStatus exactMatch
506
521
targets of
507
- Right targets' -> Left (TargetSelectorAmbiguous targetStr targets')
522
+ Right targets' ->
523
+ case (targets', mfilter) of
524
+ ((_,t): _, AmbiguityResolverFirst ) -> Right t
525
+ _ -> Left (TargetSelectorAmbiguous targetStr targets')
508
526
Left ((m, ms): _) -> Left (MatchingInternalError targetStr m ms)
509
527
Left [] -> internalError " resolveTargetSelector"
510
528
where
@@ -559,7 +577,7 @@ resolveTargetSelector knowntargets@KnownTargets{..} mfilter targetStrStatus =
559
577
= innerErr (Just (kind,thing)) m
560
578
innerErr c m = (c,m)
561
579
562
- applyKindFilter :: ComponentKindFilter -> [TargetSelector ] -> [TargetSelector ]
580
+ applyKindFilter :: ComponentKind -> [TargetSelector ] -> [TargetSelector ]
563
581
applyKindFilter kfilter = filter go
564
582
where
565
583
go (TargetPackage _ _ (Just filter')) = kfilter == filter'
0 commit comments