Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix graphex cabal for multi-source-dir units #57

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased changes

## 0.1.2.1

- Fix `graphex cabal` for units with multiple source directories.
- It would mistakenly treat some modules as having no file.

## 0.1.2.0

- The graph edge direction has been standardized. `A -> B` means "A imports B."
Expand Down
3 changes: 3 additions & 0 deletions dummy-sublib/test/DummyTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module DummyTest where

import DummySublibModule
6 changes: 4 additions & 2 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: graphex
version: 0.1.2.0
version: 0.1.2.1
github: "dustin/graphex"
license: BSD3
author: "Dustin Sallings"
Expand Down Expand Up @@ -63,7 +63,9 @@ default-extensions:

internal-libraries:
graphex-dummy-sublib:
source-dirs: dummy-sublib
source-dirs:
- dummy-sublib
- dummy-sublib/test

executables:
graphex:
Expand Down
24 changes: 14 additions & 10 deletions src/Graphex/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import Data.List (intersperse)
import Data.List.NonEmpty (NonEmpty)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe,
import Data.Maybe (catMaybes,
mapMaybe,
maybeToList)
import Data.Semigroup.Foldable
import qualified Data.Set as Set
Expand Down Expand Up @@ -98,7 +99,7 @@ discoverCabalModules CabalDiscoverOpts{..} cabalFile = do
guard $ Discovered == foldMap1 (`discoversUnit` CabalLibraryUnit name) toDiscover
pure Module
{ name = fromString $ mconcat $ intersperse "." $ Cabal.components exMod
, path = ModuleFile $ sourceDirToFilePath srcDir </> Cabal.toFilePath exMod <.> ".hs"
, path = if exMod `elem` libBuildInfo.autogenModules then ModuleNoFile else ModuleFile $ sourceDirToFilePath srcDir </> Cabal.toFilePath exMod <.> ".hs"
}
, do
Executable{..} <- executables
Expand All @@ -110,7 +111,7 @@ discoverCabalModules CabalDiscoverOpts{..} cabalFile = do
if otherMod == "Main"
then unUnqualComponentName exeName ++ "-Main"
else mconcat $ intersperse "." $ Cabal.components otherMod
, path = ModuleFile $ sourceDirToFilePath srcDir </> Cabal.toFilePath otherMod <.> ".hs"
, path = if otherMod `elem` buildInfo.autogenModules then ModuleNoFile else ModuleFile $ sourceDirToFilePath srcDir </> Cabal.toFilePath otherMod <.> ".hs"
}
, do
TestSuite{..} <- testSuites
Expand All @@ -120,20 +121,23 @@ discoverCabalModules CabalDiscoverOpts{..} cabalFile = do

pure Module
{ name = fromString $ mconcat $ intersperse "." $ Cabal.components otherMod
, path = ModuleFile $ sourceDirToFilePath srcDir </> Cabal.toFilePath otherMod <.> ".hs"
, path = if otherMod `elem` testBuildInfo.autogenModules then ModuleNoFile else ModuleFile $ sourceDirToFilePath srcDir </> Cabal.toFilePath otherMod <.> ".hs"
}
]

pooledMapConcurrentlyN numJobs validateModulePath candidateModules
catMaybes <$> pooledMapConcurrentlyN numJobs validateModulePath candidateModules

validateModulePath :: Module -> IO Module
validateModulePath :: Module -> IO (Maybe Module)
validateModulePath m = do
path <- case m.path of
let mkModule p = Module m.name p
case m.path of
ModuleFile fp -> do
fileExists <- doesFileExist fp
pure $ if fileExists then ModuleFile fp else ModuleNoFile
ModuleNoFile -> pure ModuleNoFile
pure Module {name = m.name, path = path}
-- If a discovered module has a filepath but doesn't exist, it means
-- that the unit had multiple source directories. We assume it will
-- exist in one of those directories.
pure $ if fileExists then Just $ mkModule $ ModuleFile fp else Nothing
ModuleNoFile -> pure $ Just $ mkModule ModuleNoFile

data CabalUnitType =
CabalLibrary
Expand Down
5 changes: 4 additions & 1 deletion test/CabalSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ exeModules =
]

dummySublibModules :: [Module]
dummySublibModules = [Module "DummySublibModule" "dummy-sublib/DummySublibModule.hs"]
dummySublibModules =
[ Module "DummySublibModule" "dummy-sublib/DummySublibModule.hs"
, Module "DummyTest" "dummy-sublib/test/DummyTest.hs"
]

defaultOpts :: CabalDiscoverOpts
defaultOpts = CabalDiscoverOpts
Expand Down
Loading