on master help sign in
Refrain from loading non-referenced packages
02ae161Robert Prije
lib/command/src/Obelisk/Command/Run.hs+54−1
@@ -46,13 +46,23 @@ import Debug.Trace (trace)
46import Distribution.Compiler (CompilerFlavor(..))
46import Distribution.Compiler (CompilerFlavor(..))
47import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
47import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
48import Distribution.Parsec.ParseResult (runParseResult)
48import Distribution.Parsec.ParseResult (runParseResult)
49+import Distribution.Pretty (prettyShow)
50+import Distribution.Simple.Compiler (PackageDB (GlobalPackageDB))
51+import Distribution.Simple.Configure (configCompilerEx, getInstalledPackages)
52+import Distribution.Simple.PackageIndex (InstalledPackageIndex, lookupDependency)
53+import Distribution.Simple.Program.Db (defaultProgramDb)
49import qualified Distribution.System as Dist
54import qualified Distribution.System as Dist
50−import Distribution.Types.BuildInfo (buildable, cppOptions, defaultExtensions, defaultLanguage, hsSourceDirs, options)
55+import Distribution.Types.BuildInfo (buildable, cppOptions, defaultExtensions, defaultLanguage, hsSourceDirs, options, targetBuildDepends)
51import Distribution.Types.CondTree (simplifyCondTree)
56import Distribution.Types.CondTree (simplifyCondTree)
57+import Distribution.Types.Dependency (Dependency (..), depPkgName)
52import Distribution.Types.GenericPackageDescription (ConfVar (Arch, Impl, OS), condLibrary)
58import Distribution.Types.GenericPackageDescription (ConfVar (Arch, Impl, OS), condLibrary)
59+import Distribution.Types.InstalledPackageInfo (compatPackageKey)
53import Distribution.Types.Library (libBuildInfo)
60import Distribution.Types.Library (libBuildInfo)
61+import Distribution.Types.PackageName (mkPackageName)
62+import Distribution.Types.VersionRange (anyVersion)
54import Distribution.Utils.Generic (toUTF8BS, readUTF8File)
63import Distribution.Utils.Generic (toUTF8BS, readUTF8File)
55import qualified Distribution.Parsec.Common as Dist
64import qualified Distribution.Parsec.Common as Dist
65+import qualified Distribution.Verbosity as Verbosity (silent)
56import qualified Hpack.Config as Hpack
66import qualified Hpack.Config as Hpack
57import qualified Hpack.Render as Hpack
67import qualified Hpack.Render as Hpack
58import qualified Hpack.Yaml as Hpack
68import qualified Hpack.Yaml as Hpack
@@ -62,12 +72,16 @@ import System.Directory
62import System.Environment (getExecutablePath)
72import System.Environment (getExecutablePath)
63import System.FilePath
73import System.FilePath
64import qualified System.Info
74import qualified System.Info
75+import System.IO (hClose, hGetLine)
65import System.IO.Temp (withSystemTempDirectory)
76import System.IO.Temp (withSystemTempDirectory)
77+import System.Process (CreateProcess (..), StdStream (..))
66
78
67import Obelisk.App (MonadObelisk, getObelisk, runObelisk)
79import Obelisk.App (MonadObelisk, getObelisk, runObelisk)
68import Obelisk.CliApp (
80import Obelisk.CliApp (
69 Severity (..),
81 Severity (..),
82+ createProcess_,
70 failWith,
83 failWith,
84+ overCreateProcess,
71 proc,
85 proc,
72 putLog,
86 putLog,
73 readCreateProcessWithExitCode,
87 readCreateProcessWithExitCode,
@@ -97,6 +111,8 @@ data CabalPackageInfo = CabalPackageInfo
97 -- ^ List of compiler-specific options (e.g., the "ghc-options" field of the cabal file)
111 -- ^ List of compiler-specific options (e.g., the "ghc-options" field of the cabal file)
98 , _cabalPackageInfo_cppOptions :: [String]
112 , _cabalPackageInfo_cppOptions :: [String]
99 -- ^ List of CPP (C Preprocessor) options (e.g. the "cpp-options" field of the cabal file)
113 -- ^ List of CPP (C Preprocessor) options (e.g. the "cpp-options" field of the cabal file)
114+ , _cabalPackageInfo_buildDepends :: [Dependency]
115+ -- ^ List of build dependencies listed in the cabal file
100 }
116 }
101
117
102-- | 'Bool' with a better name for its purpose.
118-- | 'Bool' with a better name for its purpose.
@@ -367,6 +383,7 @@ parseCabalPackage' pkg = runExceptT $ do
367 defaultLanguage $ libBuildInfo lib
383 defaultLanguage $ libBuildInfo lib
368 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
384 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
369 , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
385 , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
386+ , _cabalPackageInfo_buildDepends = targetBuildDepends $ libBuildInfo lib
370 }
387 }
371 Right Nothing -> pure Nothing
388 Right Nothing -> pure Nothing
372 Left (_, errors) ->
389 Left (_, errors) ->
@@ -464,6 +481,7 @@ getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
464 -- all paths to 'pathBase'.
481 -- all paths to 'pathBase'.
465 selfExe <- liftIO $ canonicalizePath =<< getExecutablePath
482 selfExe <- liftIO $ canonicalizePath =<< getExecutablePath
466 canonicalPathBase <- liftIO $ canonicalizePath pathBase
483 canonicalPathBase <- liftIO $ canonicalizePath pathBase
484+ installedPackageIndex <- loadPackageIndex pathBase
467
485
468 (pkgFiles, pkgSrcPaths :: [NonEmpty FilePath]) <- fmap unzip $ liftIO $ for packageInfos $ \pkg -> do
486 (pkgFiles, pkgSrcPaths :: [NonEmpty FilePath]) <- fmap unzip $ liftIO $ for packageInfos $ \pkg -> do
469 canonicalSrcDirs <- traverse canonicalizePath $ (_cabalPackageInfo_packageRoot pkg </>) <$> _cabalPackageInfo_sourceDirs pkg
487 canonicalSrcDirs <- traverse canonicalizePath $ (_cabalPackageInfo_packageRoot pkg </>) <$> _cabalPackageInfo_sourceDirs pkg
@@ -476,13 +494,48 @@ getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
476 <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
494 <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
477 <> concatMap (\p -> ["-optF", p]) pkgFiles
495 <> concatMap (\p -> ["-optF", p]) pkgFiles
478 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]
496 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]
497+ <> concatMap (\packageId -> ["-package-id", packageId ])
498+ (packageIds installedPackageIndex)
479 where
499 where
480 relativeTo' = if useRelativePaths then relativeTo else const
500 relativeTo' = if useRelativePaths then relativeTo else const
501+ -- Package names we're building and not needed from the package DB
502+ packageNames =
503+ map (mkPackageName . T.unpack . _cabalPackageInfo_packageName)
504+ packageInfos
505+ packageIds installedPackageIndex =
506+ map (dependencyPackageId installedPackageIndex) $
507+ filter ((`notElem` packageNames) . depPkgName) $
508+ concatMap _cabalPackageInfo_buildDepends packageInfos <>
509+ [Dependency (mkPackageName "obelisk-run") anyVersion]
510+ dependencyPackageId installedPackageIndex dep =
511+ case lookupDependency installedPackageIndex dep of
512+ ((_version,installedPackageInfo:_) :_) ->
513+ compatPackageKey installedPackageInfo
514+ _ -> error $ "Couldn't resolve dependency for " <> prettyShow dep
515+
516+-- Load the package index used by the GHC in this path's nix project
517+loadPackageIndex :: (MonadObelisk m) => FilePath -> m InstalledPackageIndex
518+loadPackageIndex root = do
519+ ghcPath <- getPathInNixEnvironment "bash -c 'type -p ghc'"
520+ ghcPkgPath <- getPathInNixEnvironment "bash -c 'type -p ghc-pkg'"
521+ (compiler, _platform, programDb) <- liftIO
522+ $ configCompilerEx (Just GHC) (Just ghcPath) (Just ghcPkgPath) defaultProgramDb Verbosity.silent
523+ liftIO $ getInstalledPackages Verbosity.silent compiler [GlobalPackageDB] programDb
524+ where
525+ getPathInNixEnvironment cmd = do
526+ procSpec <- runProc <$> nixShellRunConfig root True (Just cmd)
527+ (_,Just h,_,_) <- createProcess_ "loadPackageIndex" procSpec
528+ path <- liftIO $ hGetLine h >>= canonicalizePath
529+ liftIO $ hClose h
530+ pure path
531+ runProc =
532+ overCreateProcess (\cs -> cs { std_out = CreatePipe }) . nixShellRunProc
481
533
482baseGhciOptions :: [String]
534baseGhciOptions :: [String]
483baseGhciOptions =
535baseGhciOptions =
484 [ "-ignore-dot-ghci"
536 [ "-ignore-dot-ghci"
485 , "-no-user-package-db"
537 , "-no-user-package-db"
538+ , "-hide-all-packages"
486 , "-package-env", "-"
539 , "-package-env", "-"
487 ]
540 ]
488
541