Skip to content

Commit 66fb845

Browse files
authored
Merge pull request #2497 from commercialhaskell/self-contained-scripts
Use package-id arg to stack ghc/runghc instead of package name
2 parents d412c37 + 1aecd68 commit 66fb845

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/Stack/GhcPkg.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Stack.GhcPkg
1313
(getGlobalDB
1414
,EnvOverride
1515
,envHelper
16+
,findGhcPkgField
1617
,createDatabase
1718
,unregisterGhcPkgId
1819
,getCabalPkgVer

src/main/Main.hs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE TupleSections #-}
22
{-# LANGUAGE CPP #-}
33
{-# LANGUAGE DeriveDataTypeable #-}
4+
{-# LANGUAGE FlexibleContexts #-}
45
{-# LANGUAGE OverloadedStrings #-}
56
{-# LANGUAGE ScopedTypeVariables #-}
67
{-# LANGUAGE TemplateHaskell #-}
@@ -61,6 +62,7 @@ import Stack.Coverage
6162
import qualified Stack.Docker as Docker
6263
import Stack.Dot
6364
import Stack.Exec
65+
import Stack.GhcPkg (findGhcPkgField)
6466
import qualified Stack.Nix as Nix
6567
import Stack.Fetch
6668
import Stack.FileWatch
@@ -740,28 +742,40 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
740742
Nothing -- Unlocked already above.
741743
ExecOptsEmbellished {..} ->
742744
withBuildConfigAndLock go $ \lk -> do
745+
let targets = concatMap words eoPackages
746+
unless (null targets) $
747+
Stack.Build.build (const $ return ()) lk defaultBuildOptsCLI
748+
{ boptsCLITargets = map T.pack targets
749+
}
750+
743751
config <- asks getConfig
752+
menv <- liftIO $ configEnvOverride config eoEnvSettings
744753
(cmd, args) <- case (eoCmd, eoArgs) of
745754
(ExecCmd cmd, args) -> return (cmd, args)
746-
(ExecGhc, args) -> execCompiler "" args
755+
(ExecGhc, args) -> getGhcCmd menv eoPackages [] args
747756
-- NOTE: this won't currently work for GHCJS, because it doesn't have
748757
-- a runghcjs binary. It probably will someday, though.
749758
(ExecRunGhc, args) ->
750-
let opts = concatMap (\x -> ["-package", x]) eoPackages
751-
in execCompiler "" (opts ++ ("-e" : "Main.main" : args))
752-
let targets = concatMap words eoPackages
753-
unless (null targets) $
754-
Stack.Build.build (const $ return ()) lk defaultBuildOptsCLI
755-
{ boptsCLITargets = map T.pack targets
756-
}
759+
getGhcCmd menv eoPackages ["-e", "Main.main"] args
757760
munlockFile lk -- Unlock before transferring control away.
758-
menv <- liftIO $ configEnvOverride config eoEnvSettings
759761
exec menv cmd args
760762
where
761-
execCompiler cmdPrefix args = do
762-
wc <- getWhichCompiler
763-
let cmd = cmdPrefix ++ compilerExeName wc
764-
return (cmd, args)
763+
-- return the package-id of the first package in GHC_PACKAGE_PATH
764+
getPkgId menv wc name = do
765+
mId <- findGhcPkgField menv wc [] name "id"
766+
case mId of
767+
Just i -> return (head $ words (T.unpack i))
768+
-- should never happen as we have already installed the packages
769+
_ -> error ("Could not find package id of package " ++ name)
770+
771+
getPkgOpts menv wc pkgs = do
772+
ids <- mapM (getPkgId menv wc) pkgs
773+
return $ concatMap (\x -> ["-package-id", x]) ids
774+
775+
getGhcCmd menv pkgs prefix args = do
776+
wc <- getWhichCompiler
777+
pkgopts <- getPkgOpts menv wc pkgs
778+
return (compilerExeName wc, prefix ++ pkgopts ++ args)
765779

766780
-- | Evaluate some haskell code inline.
767781
evalCmd :: EvalOpts -> GlobalOpts -> IO ()

0 commit comments

Comments
 (0)