on master help sign in
Merge pull request #843 from obsidiansystems/fix-project-shells

Fix project shells

commit60d56bc119a52ba8bcd6cd847d70edfaf781e60a authorAli Abrar <aliabrar@gmail.com> dateTue 23 Mar 2021, 15:07 (6 years ago) parent53957d75a782b1
2 files changed+22−18
lib/command/src/Obelisk/Command/Project.hs+17−4 lib/command/src/Obelisk/Command/Run.hs+5−14
lib/command/src/Obelisk/Command/Project.hs+17−4
@@ -14,6 +14,7 @@ module Obelisk.Command.Project
1414 , nixShellRunProc
1515 , nixShellWithHoogle
1616 , nixShellWithoutPkgs
17+ , mkObNixShellProc
1718 , obeliskDirName
1819 , toImplDir
1920 , toObeliskDir
@@ -317,7 +318,7 @@ bashEscape = BSU.toString . bytes . bash . BSU.fromString
317318nixShellRunProc :: NixShellConfig -> ProcessSpec
318319nixShellRunProc cfg = setDelegateCtlc True $ proc nixShellPath $ runNixShellConfig cfg
319320
320−nixShellWithoutPkgs
321+mkObNixShellProc
321322 :: MonadObelisk m
322323 => FilePath -- ^ Path to project root
323324 -> Bool -- ^ Should this be a pure shell?
@@ -325,12 +326,12 @@ nixShellWithoutPkgs
325326 -> Map Text FilePath -- ^ Package names mapped to their paths
326327 -> String -- ^ Shell attribute to use (e.g. @"ghc"@, @"ghcjs"@, etc.)
327328 -> Maybe String -- ^ If 'Just' run the given command; otherwise just open the interactive shell
328− -> m ()
329−nixShellWithoutPkgs root isPure chdirToRoot packageNamesAndPaths shellAttr command = do
329+ -> m ProcessSpec
330+mkObNixShellProc root isPure chdirToRoot packageNamesAndPaths shellAttr command = do
330331 packageNamesAndAbsPaths <- liftIO $ for packageNamesAndPaths makeAbsolute
331332 defShellConfig <- nixShellRunConfig root isPure command
332333 let setCwd_ = if chdirToRoot then setCwd (Just root) else id
333− runProcess_ $ setCwd_ $ nixShellRunProc $ defShellConfig
334+ pure $ setCwd_ $ nixShellRunProc $ defShellConfig
334335 & nixShellConfig_common . nixCmdConfig_target . target_expr ?~
335336 "{root, pkgs, shell}: ((import root {}).passthru.__unstable__.self.extend (_: _: {\
336337 \shellPackages = builtins.fromJSON pkgs;\
@@ -341,6 +342,18 @@ nixShellWithoutPkgs root isPure chdirToRoot packageNamesAndPaths shellAttr comma
341342 , strArg "shell" shellAttr
342343 ]
343344
345+nixShellWithoutPkgs
346+ :: MonadObelisk m
347+ => FilePath -- ^ Path to project root
348+ -> Bool -- ^ Should this be a pure shell?
349+ -> Bool -- ^ Should we chdir to the package root in the shell?
350+ -> Map Text FilePath -- ^ Package names mapped to their paths
351+ -> String -- ^ Shell attribute to use (e.g. @"ghc"@, @"ghcjs"@, etc.)
352+ -> Maybe String -- ^ If 'Just' run the given command; otherwise just open the interactive shell
353+ -> m ()
354+nixShellWithoutPkgs root isPure chdirToRoot packageNamesAndPaths shellAttr command = do
355+ runProcess_ =<< mkObNixShellProc root isPure chdirToRoot packageNamesAndPaths shellAttr command
356+
344357nixShellWithHoogle :: MonadObelisk m => FilePath -> Bool -> String -> Maybe String -> m ()
345358nixShellWithHoogle root isPure shell' command = do
346359 defShellConfig <- nixShellRunConfig root isPure command
lib/command/src/Obelisk/Command/Run.hs+5−14
@@ -72,16 +72,12 @@ import System.Directory
7272import System.Environment (getExecutablePath)
7373import System.FilePath
7474import qualified System.Info
75−import System.IO (hClose, hGetLine)
7675import System.IO.Temp (withSystemTempDirectory)
77−import System.Process (CreateProcess (..), StdStream (..))
7876
7977import Obelisk.App (MonadObelisk, getObelisk, runObelisk)
8078import Obelisk.CliApp (
8179 Severity (..),
82− createProcess_,
8380 failWith,
84− overCreateProcess,
8581 proc,
8682 putLog,
8783 readCreateProcessWithExitCode,
@@ -481,7 +477,7 @@ getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
481477 -- all paths to 'pathBase'.
482478 selfExe <- liftIO $ canonicalizePath =<< getExecutablePath
483479 canonicalPathBase <- liftIO $ canonicalizePath pathBase
484− installedPackageIndex <- loadPackageIndex pathBase
480+ installedPackageIndex <- loadPackageIndex packageInfos pathBase
485481
486482 (pkgFiles, pkgSrcPaths :: [NonEmpty FilePath]) <- fmap unzip $ liftIO $ for packageInfos $ \pkg -> do
487483 canonicalSrcDirs <- traverse canonicalizePath $ (_cabalPackageInfo_packageRoot pkg </>) <$> _cabalPackageInfo_sourceDirs pkg
@@ -514,8 +510,8 @@ getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
514510 _ -> error $ "Couldn't resolve dependency for " <> prettyShow dep
515511
516512-- 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
513+loadPackageIndex :: MonadObelisk m => [CabalPackageInfo] -> FilePath -> m InstalledPackageIndex
514+loadPackageIndex packageInfos root = do
519515 ghcPath <- getPathInNixEnvironment "bash -c 'type -p ghc'"
520516 ghcPkgPath <- getPathInNixEnvironment "bash -c 'type -p ghc-pkg'"
521517 (compiler, _platform, programDb) <- liftIO
@@ -523,13 +519,8 @@ loadPackageIndex root = do
523519 liftIO $ getInstalledPackages Verbosity.silent compiler [GlobalPackageDB] programDb
524520 where
525521 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
522+ path <- readProcessAndLogStderr Debug =<< mkObNixShellProc root False True (packageInfoToNamePathMap packageInfos) "ghc" (Just cmd)
523+ liftIO $ canonicalizePath $ T.unpack $ T.strip path
533524
534525baseGhciOptions :: [String]
535526baseGhciOptions =