-addPackage package
- = do pkg_details <- readIORef v_Package_details
- case lookupPkg package pkg_details of
- Nothing -> throwDyn (CmdLineError ("unknown package name: " ++ package))
- Just details -> do
- ps <- readIORef v_Packages
- unless (package `elem` ps) $ do
- mapM_ addPackage (package_deps details)
- ps <- readIORef v_Packages
- writeIORef v_Packages (package:ps)
-
-getPackageImportPath :: IO [String]
+addPackage package = do
+ pkg_details <- getPackageConfigMap
+ ps <- readIORef v_ExplicitPackages
+ ps' <- add_package pkg_details ps (mkPackageName package)
+ -- Throws an exception if it fails
+ writeIORef v_ExplicitPackages ps'
+
+-- internal helper
+add_package :: PackageConfigMap -> [PackageName]
+ -> PackageName -> IO [PackageName]
+add_package pkg_details ps p
+ | p `elem` ps -- Check if we've already added this package
+ = return ps
+ | Just details <- lookupPkg pkg_details p
+ -- Add the package's dependents also
+ = do ps' <- foldM (add_package pkg_details) ps (packageDependents details)
+ return (p : ps')
+ | otherwise
+ = throwDyn (CmdLineError ("unknown package name: " ++ packageNameString p))
+
+
+-- -----------------------------------------------------------------------------
+-- Extracting information from the packages in scope
+
+-- Many of these functions take a list of packages: in those cases,
+-- the list is expected to contain the "dependent packages",
+-- i.e. those packages that were found to be depended on by the
+-- current module/program. These can be auto or non-auto packages, it
+-- doesn't really matter. The list is always combined with the list
+-- of explicit (command-line) packages to determine which packages to
+-- use.
+
+getPackageImportPath :: IO [String]