@@ -684,11 +684,8 @@ partOfComponent ::
684
684
-- | Component to check whether the given FilePath is part of it.
685
685
ChComponentInfo ->
686
686
Bool
687
- partOfComponent fp' comp
688
- | inTargets (ciSourceDirs comp) fp' (getTargets comp fp')
689
- = True
690
- | otherwise
691
- = False
687
+ partOfComponent fp' comp =
688
+ inTargets (ciSourceDirs comp) fp' (getTargets comp fp')
692
689
where
693
690
-- Check if the FilePath is in an executable or setup's main-is field
694
691
inMainIs :: FilePath -> Bool
@@ -698,11 +695,15 @@ partOfComponent fp' comp
698
695
| otherwise = False
699
696
700
697
inTargets :: [FilePath ] -> FilePath -> [String ] -> Bool
701
- inTargets sourceDirs fp targets
702
- | Just relative <- relativeTo fp sourceDirs
703
- = any (`elem` targets) [getModuleName relative, fp] || inMainIs relative
704
- | otherwise
705
- = False
698
+ inTargets sourceDirs fp targets =
699
+ let candidates = relativeTo fp sourceDirs
700
+ in any (existsInTargets targets fp) candidates
701
+
702
+ existsInTargets :: [String ] -> FilePath -> FilePath -> Bool
703
+ existsInTargets targets absFp relFp = or
704
+ [ any (`elem` targets) [getModuleName relFp, absFp]
705
+ , inMainIs relFp
706
+ ]
706
707
707
708
getModuleName :: FilePath -> String
708
709
getModuleName fp = map
@@ -846,24 +847,23 @@ ancestors dir
846
847
subdir = takeDirectory dir
847
848
848
849
-- | Assuming a FilePath @"src\/Lib\/Lib.hs"@ and a list of directories
849
- -- such as @["src", "app"]@, returns either the given FilePath
850
+ -- such as @["src", "app"]@, returns the given FilePath
850
851
-- with a matching directory stripped away.
851
852
-- If there are multiple matches, e.g. multiple directories are a prefix
852
- -- of the given FilePath, return the first match in the list.
853
- -- Returns Nothing, if not a single
854
- -- given directory is a prefix of the FilePath.
853
+ -- of the given FilePath we return all matches.
854
+ -- Returns an empty list if no prefix matches the given FilePath.
855
855
--
856
856
-- >>> relativeTo "src/Lib/Lib.hs" ["src"]
857
- -- Just "Lib/Lib.hs"
857
+ -- [ "Lib/Lib.hs"]
858
858
--
859
859
-- >>> relativeTo "src/Lib/Lib.hs" ["app"]
860
- -- Nothing
860
+ -- []
861
861
--
862
862
-- >>> relativeTo "src/Lib/Lib.hs" ["src", "src/Lib"]
863
- -- Just "Lib/Lib.hs"
864
- relativeTo :: FilePath -> [FilePath ] -> Maybe FilePath
865
- relativeTo file sourceDirs = listToMaybe
866
- $ mapMaybe (`stripFilePath` file) sourceDirs
863
+ -- [ "Lib/Lib.hs", "Lib.hs"]
864
+ relativeTo :: FilePath -> [FilePath ] -> [ FilePath ]
865
+ relativeTo file sourceDirs =
866
+ mapMaybe (`stripFilePath` file) sourceDirs
867
867
868
868
-- | Returns a user facing display name for the cradle type,
869
869
-- e.g. "Stack project" or "GHC session"
0 commit comments