Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 2a2fa86

Browse files
authored
Fix multi source directories (#1577)
* Add failing test-cases for #1576 * Handle multiple source directories correctly Change existing behaviour to find all source directories which are a prefix of the given filepath. * Relax base constraints on c-h testdata There was probably no reason to have the constraints in the first pace * Lower cabal constraint for testdata * Avoid load checks as long as AZURE CI chokes on it
1 parent eae2e4b commit 2a2fa86

File tree

15 files changed

+138
-69
lines changed

15 files changed

+138
-69
lines changed

haskell-ide-engine.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ test-suite unit-test
220220
, ghc
221221
, haskell-ide-engine
222222
, haskell-lsp-types == 0.19.*
223+
, hie-bios
223224
, hie-test-utils
224225
, hie-plugin-api
225226
, hoogle > 5.0.11

hie-plugin-api/Haskell/Ide/Engine/Cradle.hs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,8 @@ partOfComponent ::
684684
-- | Component to check whether the given FilePath is part of it.
685685
ChComponentInfo ->
686686
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')
692689
where
693690
-- Check if the FilePath is in an executable or setup's main-is field
694691
inMainIs :: FilePath -> Bool
@@ -698,11 +695,15 @@ partOfComponent fp' comp
698695
| otherwise = False
699696

700697
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+
]
706707

707708
getModuleName :: FilePath -> String
708709
getModuleName fp = map
@@ -846,24 +847,23 @@ ancestors dir
846847
subdir = takeDirectory dir
847848

848849
-- | 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
850851
-- with a matching directory stripped away.
851852
-- 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.
855855
--
856856
-- >>> relativeTo "src/Lib/Lib.hs" ["src"]
857-
-- Just "Lib/Lib.hs"
857+
-- ["Lib/Lib.hs"]
858858
--
859859
-- >>> relativeTo "src/Lib/Lib.hs" ["app"]
860-
-- Nothing
860+
-- []
861861
--
862862
-- >>> 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
867867

868868
-- | Returns a user facing display name for the cradle type,
869869
-- e.g. "Stack project" or "GHC session"

test/testdata/cabal-helper/implicit-exe/implicit-exe.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ build-type: Simple
77
library
88
exposed-modules: Lib
99
hs-source-dirs: src
10-
build-depends: base >=4.8 && <4.14
10+
build-depends: base
1111
default-language: Haskell2010
1212

1313

1414
executable implicit-exe
1515
main-is: src/Exe.hs
16-
build-depends: base >=4.8 && <4.14, implicit-exe
16+
build-depends: base, implicit-exe
1717
default-language: Haskell2010
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
cabal-version: >=2.0
1+
cabal-version: >=1.10
22
name: A
33
version: 0.1.0.0
44
build-type: Simple
55

66
library
77
exposed-modules: MyLib
8-
build-depends: base >=4.9 && < 5
8+
build-depends: base
99
default-language: Haskell2010
1010

1111
executable A
1212
main-is: Main.hs
1313
other-modules: MyLib
14-
build-depends: base >= 4.9 && < 5, A
14+
build-depends: base, A
1515
default-language: Haskell2010
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
cabal-version: >=2.0
1+
cabal-version: >=1.10
22
name: B
33
version: 0.1.0.0
44
build-type: Simple
55

66
library
77
exposed-modules: MyLib
8-
build-depends: base >= 4.9 && < 5
8+
build-depends: base
99
default-language: Haskell2010
1010

1111
executable B
1212
main-is: Main.hs
1313
other-modules: MyLib
14-
build-depends: base >= 4.9 && < 5, B
14+
build-depends: base, B
1515
default-language: Haskell2010
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
cabal-version: >=2.0
1+
cabal-version: >=1.10
22
name: C
33
version: 0.1.0.0
44
build-type: Simple
55

66
library
77
exposed-modules: MyLib
8-
build-depends: base>= 4.9 && < 5
8+
build-depends: base
99
default-language: Haskell2010
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cabal-version: >=1.10
2+
name: multi-source-dirs
3+
version: 0.1.0.0
4+
license-file: LICENSE
5+
build-type: Simple
6+
7+
library
8+
exposed-modules: Lib, BetterLib
9+
hs-source-dirs: src, src/input
10+
build-depends: base
11+
default-language: Haskell2010
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module BetterLib where
2+
3+
4+
foo = 3
5+
bar = "String"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Lib where
2+
3+
foobar = 15
4+
5+
fizbuzz :: Int -> String
6+
fizbuzz n = "Fizz"

test/testdata/cabal-helper/simple-cabal/simple-cabal-test.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ build-type: Simple
66

77
library
88
exposed-modules: MyLib
9-
build-depends: base >=4.12 && <4.13
9+
build-depends: base
1010
default-language: Haskell2010

test/testdata/cabal-helper/simple-stack/simple-stack-test.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ build-type: Simple
66

77
library
88
exposed-modules: MyLib
9-
build-depends: base >=4.12 && <4.13
9+
build-depends: base
1010
default-language: Haskell2010

test/testdata/cabal-helper/sub-package/plugins-api/plugins-api.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ build-type: Simple
66

77
library
88
exposed-modules: PluginLib
9-
build-depends: base >=4.12 && <4.13
9+
build-depends: base
1010
default-language: Haskell2010

test/testdata/cabal-helper/sub-package/sub-package.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ build-type: Simple
66

77
library
88
exposed-modules: MyLib
9-
build-depends: base >=4.12 && <4.13, plugins-api
9+
build-depends: base, plugins-api
1010
hs-source-dirs: src
1111
default-language: Haskell2010
1212

1313
executable sub-package
1414
main-is: Main.hs
15-
build-depends: base >=4.12 && <4.13, sub-package
15+
build-depends: base, sub-package
1616
hs-source-dirs: app
1717
default-language: Haskell2010

0 commit comments

Comments
 (0)