Skip to content

Commit

Permalink
Fix #6225 Avoid use of GHC_PACKAGE_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem committed Aug 30, 2023
1 parent bc6a6a5 commit 6489923
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
58 changes: 22 additions & 36 deletions src/Stack/Build/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import Data.Conduit.Process.Typed ( createSource )
import qualified Data.Conduit.Text as CT
import qualified Data.List as L
import Data.List.NonEmpty ( nonEmpty )
import qualified Data.List.NonEmpty as NonEmpty ( toList )
import qualified Data.List.NonEmpty as NonEmpty
import Data.List.Split ( chunksOf )
import qualified Data.Map.Merge.Strict as Map
import qualified Data.Map.Strict as Map
Expand All @@ -56,7 +56,6 @@ import Data.Time
( ZonedTime, getZonedTime, formatTime, defaultTimeLocale )
import qualified Data.ByteString.Char8 as S8
import qualified Distribution.PackageDescription as C
import Distribution.Pretty ( prettyShow )
import qualified Distribution.Simple.Build.Macros as C
import Distribution.System ( OS (Windows), Platform (Platform) )
import qualified Distribution.Text as C
Expand Down Expand Up @@ -85,10 +84,9 @@ import Path.IO
import RIO.Process
( HasProcessContext, byteStringInput, doesExecutableExist
, eceExitCode, findExecutable, getStderr, getStdout, inherit
, modifyEnvVars, proc, readProcess_, runProcess_, setStderr
, setStdin, setStdout, showProcessArgDebug, useHandleOpen
, waitExitCode, withModifyEnvVars, withProcessWait
, withWorkingDir
, modifyEnvVars, proc, runProcess_, setStderr, setStdin
, setStdout, showProcessArgDebug, useHandleOpen, waitExitCode
, withProcessWait, withWorkingDir
)
import Stack.Build.Cache
( TestStatus (..), deleteCaches, getTestStatus
Expand Down Expand Up @@ -123,7 +121,7 @@ import Stack.Coverage
( deleteHpcReports, generateHpcMarkupIndex, generateHpcReport
, generateHpcUnifiedReport, updateTixFile
)
import Stack.GhcPkg ( ghcPkgPathEnvVar, unregisterGhcPkgIds )
import Stack.GhcPkg ( ghcPkg, unregisterGhcPkgIds )
import Stack.Package ( buildLogPath )
import Stack.PackageDump ( conduitDumpPackage, ghcPkgDescribe )
import Stack.Prelude
Expand Down Expand Up @@ -1721,7 +1719,6 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap
_ -> pure Nothing

copyPreCompiled (PrecompiledCache mlib sublibs exes) = do
wc <- view $ actualCompilerVersionL.whichCompilerL
announceTask ee task "using precompiled package"

-- We need to copy .conf files for the main library and all sublibraries
Expand All @@ -1736,39 +1733,28 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap
toMungedPackageId sublib =
let sublibName = LSubLibName $ mkUnqualComponentName $ T.unpack sublib
in MungedPackageId (MungedPackageName pname sublibName) pversion
toPackageId :: MungedPackageId -> PackageIdentifier
toPackageId (MungedPackageId n v) =
PackageIdentifier (encodeCompatPackageName n) v
allToUnregister :: [Either PackageIdentifier GhcPkgId]
allToUnregister = mcons
(prettyShow taskProvides <$ mlib)
(map (prettyShow . toMungedPackageId) subLibNames)
(Left taskProvides <$ mlib)
(map (Left . toPackageId . toMungedPackageId) subLibNames)
allToRegister = mcons mlib sublibs

unless (null allToRegister) $
withMVar eeInstallLock $ \() -> do
-- We want to ignore the global and user databases.
-- Unfortunately, ghc-pkg doesn't take such arguments on the
-- command line. Instead, we'll set GHC_PACKAGE_PATH. See:
-- https://github.com/commercialhaskell/stack/issues/1146

let modifyEnv = Map.insert
(ghcPkgPathEnvVar wc)
(T.pack $ toFilePathNoTrailingSep $ bcoSnapDB eeBaseConfigOpts)

withModifyEnvVars modifyEnv $ do
GhcPkgExe ghcPkgExe <- getGhcPkgExe

-- first unregister everything that needs to be unregistered
forM_ allToUnregister $ \packageName -> catchAny
( readProcessNull
(toFilePath ghcPkgExe)
[ "unregister", "--force", packageName]
)
(const (pure ()))

-- now, register the cached conf files
forM_ allToRegister $ \libpath ->
proc
(toFilePath ghcPkgExe)
[ "register", "--force", toFilePath libpath]
readProcess_
-- We want to ignore the global and user package databases. ghc-pkg
-- allows us to specify --no-user-package-db and --package-db=<db> on
-- the command line.
let pkgDb = bcoSnapDB eeBaseConfigOpts
ghcPkgExe <- getGhcPkgExe
-- First unregister everything that needs to be unregistered.
unless (null allToUnregister) $
unregisterGhcPkgIds ghcPkgExe pkgDb $ NonEmpty.fromList allToUnregister
-- Now, register the cached conf files.
forM_ allToRegister $ \libpath ->
ghcPkg ghcPkgExe [pkgDb] ["register", "--force", toFilePath libpath]

liftIO $ forM_ exes $ \exe -> do
ensureDir bindir
Expand Down
5 changes: 4 additions & 1 deletion src/Stack/GhcPkg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Stack.GhcPkg
( createDatabase
, findGhcPkgField
, getGlobalDB
, ghcPkg
, ghcPkgPathEnvVar
, mkGhcPackagePath
, unregisterGhcPkgIds
Expand Down Expand Up @@ -144,13 +145,15 @@ unregisterGhcPkgIds ::
-> NonEmpty (Either PackageIdentifier GhcPkgId)
-> RIO env ()
unregisterGhcPkgIds pkgexe pkgDb epgids = do
-- The ghcPkg function supplies initial arguments
-- --no-user-package-db --package-db=<db1> ... --package-db=<dbn>
eres <- ghcPkg pkgexe [pkgDb] args
case eres of
Left e -> prettyWarn $ string $ displayException e
Right _ -> pure ()
where
(idents, gids) = partitionEithers $ toList epgids
args = "unregister" : "--user" : "--force" :
args = "unregister" : "--force" :
map packageIdentifierString idents ++
if null gids then [] else "--ipid" : map ghcPkgIdString gids

Expand Down

0 comments on commit 6489923

Please sign in to comment.