@@ -19,13 +19,13 @@ module Distribution.Client.TargetSelector (
19
19
TargetSelector (.. ),
20
20
TargetImplicitCwd (.. ),
21
21
ComponentKind (.. ),
22
- AmbiguityResolver (.. ),
23
22
SubComponentTarget (.. ),
24
23
QualLevel (.. ),
25
24
componentKind ,
26
25
27
26
-- * Reading target selectors
28
27
readTargetSelectors ,
28
+ readTargetSelectors' ,
29
29
TargetSelectorProblem (.. ),
30
30
reportTargetSelectorProblems ,
31
31
showTargetSelector ,
@@ -66,6 +66,12 @@ import Distribution.Simple.LocalBuildInfo
66
66
, pkgComponents , componentName , componentBuildInfo )
67
67
import Distribution.Types.ForeignLib
68
68
69
+ import Distribution.Client.NixStyleOptions
70
+ import Distribution.Client.Setup
71
+ ( ConfigExFlags (.. ) )
72
+ import Distribution.Simple.Setup
73
+ ( fromFlagOrDefault )
74
+
69
75
import Distribution.Simple.Utils
70
76
( die' , lowercase , ordNub )
71
77
import Distribution.Client.Utils
@@ -176,6 +182,7 @@ data AmbiguityResolver =
176
182
| AmbiguityResolverFirst
177
183
-- | Choose the target component with the specific kind
178
184
| AmbiguityResolverKind ComponentKind
185
+ | AmbiguityResolverKindFirst ComponentKind
179
186
deriving (Eq , Ord , Show )
180
187
181
188
-- | Either the component as a whole or detail about a file or module target
@@ -208,36 +215,54 @@ instance Structured SubComponentTarget
208
215
-- the available packages (and their locations).
209
216
--
210
217
readTargetSelectors :: [PackageSpecifier (SourcePackage (PackageLocation a ))]
211
- -> AmbiguityResolver
218
+ -> Maybe ComponentKind
212
219
-- ^ This parameter is used when there are 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
220
+ -- If it is 'Just ', then we attempt to resolve ambiguitiy
221
+ -- by applying it, since otherwise there is no way to
222
+ -- allow contextually valid yet syntactically ambiguous
216
223
-- selectors.
217
224
-- (#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'.
225
+ -> NixStyleFlags b
226
+ -- ^ Used in case @--pick-first-target@ was passed.
223
227
-> [String ]
224
228
-> IO (Either [TargetSelectorProblem ] [TargetSelector ])
225
- readTargetSelectors = readTargetSelectorsWith defaultDirActions
229
+ readTargetSelectors pkgs mfilter NixStyleFlags {configExFlags}
230
+ = readTargetSelectorsWith defaultDirActions pkgs mfilter
231
+ (fromFlagOrDefault False (configPickFirstTarget configExFlags))
232
+
233
+
234
+ -- | Same as 'readTargetSelectors' but in case you don't have 'NixStyleFlags'.
235
+ readTargetSelectors' :: [PackageSpecifier (SourcePackage (PackageLocation a ))]
236
+ -> Maybe ComponentKind
237
+ -> [String ]
238
+ -> IO (Either [TargetSelectorProblem ] [TargetSelector ])
239
+ readTargetSelectors' pkgs mfilter =
240
+ readTargetSelectorsWith defaultDirActions pkgs mfilter False
226
241
227
242
readTargetSelectorsWith :: (Applicative m , Monad m ) => DirActions m
228
243
-> [PackageSpecifier (SourcePackage (PackageLocation a ))]
229
- -> AmbiguityResolver
244
+ -> Maybe ComponentKind
245
+ -- ^ Filter the target to resolve ambiguity?
246
+ -> Bool
247
+ -- ^ Pick the first target to resolve ambiguity?
230
248
-> [String ]
231
249
-> m (Either [TargetSelectorProblem ] [TargetSelector ])
232
- readTargetSelectorsWith dirActions@ DirActions {} pkgs mfilter targetStrs =
250
+ readTargetSelectorsWith dirActions@ DirActions {} pkgs mfilter pickFirst targetStrs =
233
251
case parseTargetStrings targetStrs of
234
252
([] , usertargets) -> do
235
253
usertargets' <- traverse (getTargetStringFileStatus dirActions) usertargets
236
254
knowntargets <- getKnownTargets dirActions pkgs
237
- case resolveTargetSelectors knowntargets usertargets' mfilter of
255
+ case resolveTargetSelectors knowntargets usertargets' resolver of
238
256
([] , btargets) -> return (Right btargets)
239
257
(problems, _) -> return (Left problems)
240
258
(strs, _) -> return (Left (map TargetSelectorUnrecognised strs))
259
+ where
260
+ resolver
261
+ | Just kind <- mfilter
262
+ , pickFirst = AmbiguityResolverKindFirst kind
263
+ | Just kind <- mfilter = AmbiguityResolverKind kind
264
+ | pickFirst = AmbiguityResolverFirst
265
+ | otherwise = AmbiguityResolverNone
241
266
242
267
243
268
data DirActions m = DirActions {
@@ -496,7 +521,7 @@ resolveTargetSelector :: KnownTargets
496
521
-> AmbiguityResolver
497
522
-> TargetStringFileStatus
498
523
-> Either TargetSelectorProblem TargetSelector
499
- resolveTargetSelector knowntargets@ KnownTargets {.. } mfilter targetStrStatus =
524
+ resolveTargetSelector knowntargets@ KnownTargets {.. } resolver targetStrStatus =
500
525
case findMatch (matcher targetStrStatus) of
501
526
502
527
Unambiguous _
@@ -511,18 +536,32 @@ resolveTargetSelector knowntargets@KnownTargets{..} mfilter targetStrStatus =
511
536
| projectIsEmpty -> Left TargetSelectorNoTargetsInProject
512
537
| otherwise -> Left (classifyMatchErrors errs)
513
538
539
+ -- Try to resolve the ambiguity with a kind filter
514
540
Ambiguous _ targets
515
- | AmbiguityResolverKind kfilter <- mfilter
541
+ | AmbiguityResolverKind kfilter <- resolver
516
542
, [target] <- applyKindFilter kfilter targets -> Right target
517
543
544
+ -- If we have a filter and want to pick from the first
545
+ Ambiguous _ targets
546
+ | AmbiguityResolverKindFirst kfilter <- resolver
547
+ , target: _ <- applyKindFilter kfilter targets -> Right target
548
+
549
+ -- Same case as above, except there weren't any filter matches
550
+ Ambiguous _ targets
551
+ | AmbiguityResolverKindFirst _ <- resolver
552
+ , target: _ <- targets -> Right target
553
+
554
+ -- Just pick the first of any
555
+ Ambiguous _ targets
556
+ | AmbiguityResolverFirst <- resolver
557
+ , target: _ <- targets -> Right target
558
+
559
+ -- A truly, unresolvable ambiguity
518
560
Ambiguous exactMatch targets ->
519
561
case disambiguateTargetSelectors
520
562
matcher targetStrStatus exactMatch
521
563
targets of
522
- Right targets' ->
523
- case (targets', mfilter) of
524
- ((_,t): _, AmbiguityResolverFirst ) -> Right t
525
- _ -> Left (TargetSelectorAmbiguous targetStr targets')
564
+ Right targets' -> Left (TargetSelectorAmbiguous targetStr targets')
526
565
Left ((m, ms): _) -> Left (MatchingInternalError targetStr m ms)
527
566
Left [] -> internalError " resolveTargetSelector"
528
567
where
0 commit comments