Skip to content

Commit

Permalink
Refine PackageImports support in multi-component
Browse files Browse the repository at this point in the history
In particular, be more careful about only returning import paths which
could actually be imported into the current module which means excluding
'main' modules.

Fixes #40
  • Loading branch information
mpickering committed May 20, 2020
1 parent f1a10fe commit 0d4f92f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Development/IDE/Core/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ getParsedModuleRule = defineEarlyCutoff $ \GetParsedModule file -> do
let hsc = hscEnv sess
-- After parsing the module remove all package imports referring to
-- these packages as we have already dealt with what they map to.
comp_pkgs = map (fst . mkImportDirs) (deps sess)
comp_pkgs = mapMaybe (fmap fst . mkImportDirs) (deps sess)
opt <- getIdeOptions
(_, contents) <- getFileContents file

Expand Down Expand Up @@ -676,7 +676,7 @@ getModIfaceRule = define $ \GetModIface f -> do
let hsc = hscEnv sess
-- After parsing the module remove all package imports referring to
-- these packages as we have already dealt with what they map to.
comp_pkgs = map (fst . mkImportDirs) (deps sess)
comp_pkgs = mapMaybe (fmap fst . mkImportDirs) (deps sess)
opt <- getIdeOptions
(_, contents) <- getFileContents f
-- Embed --haddocks in the interface file
Expand Down
15 changes: 8 additions & 7 deletions src/Development/IDE/Import/FindImports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import DriverPhases
import Control.Monad.Extra
import Control.Monad.IO.Class
import System.FilePath
import Data.Maybe

data Import
= FileImport !ArtifactsLocation
Expand Down Expand Up @@ -79,12 +80,12 @@ locateModuleFile import_dirss exts doesExist isSource modName = do
| isSource = ext ++ "-boot"
| otherwise = ext

mkImportDirs :: (M.InstalledUnitId, DynFlags) -> (PackageName, [FilePath])
mkImportDirs (i, df@DynFlags{importPaths}) = (pn , importPaths)
where
pn = case getPackageName df i of
Nothing -> error ("mkImportDirs: " ++ show i)
Just n -> n
-- | This function is used to map a package name to a set of import paths.
-- It only returns Just for unit-ids which are possible to import into the
-- current module. In particular, it will return Nothing for 'main' components
-- as they can never be imported into another package.
mkImportDirs :: (M.InstalledUnitId, DynFlags) -> Maybe (PackageName, [FilePath])
mkImportDirs (i, df@DynFlags{importPaths}) = (, importPaths) <$> getPackageName df i

-- | locate a module in either the file system or the package database. Where we go from *daml to
-- Haskell
Expand Down Expand Up @@ -119,7 +120,7 @@ locateModule dflags comp_info exts doesExist modName mbPkgName isSource = do
Nothing -> lookupInPackageDB dflags
Just file -> toModLocation file
where
import_paths = map mkImportDirs comp_info
import_paths = mapMaybe mkImportDirs comp_info
toModLocation file = liftIO $ do
loc <- mkHomeModLocation dflags (unLoc modName) (fromNormalizedFilePath file)
return $ Right $ FileImport $ ArtifactsLocation file loc (not isSource)
Expand Down

0 comments on commit 0d4f92f

Please sign in to comment.