Skip to content

Commit dfd5c82

Browse files
authored
Merge pull request #6117 from commercialhaskell/fix5203
Fix #5203 Put GHCi configuration scripts in a user-specific location
2 parents 805e1ef + 30ed862 commit dfd5c82

File tree

22 files changed

+71
-39
lines changed

22 files changed

+71
-39
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Major changes:
1010

1111
Behavior changes:
1212

13+
* Use `$XDG_CACHE_HOME/stack/ghci-script`, rather than `<temp>/haskell-stack-ghci`
14+
(where `<temp>` is the directory yielded by the `temporary` package's
15+
`System.IO.Temp.getCanonicalTemporaryDirectory`), as the base location for
16+
GHCi script files generated by `stack ghci` or `stack repl`. See
17+
[#5203](https://github.com/commercialhaskell/stack/issues/5203)
18+
1319
Other enhancements:
1420

1521
Bug fixes:

doc/ghci.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ but not another is quite likely to cause failures. GHCi will be run with
2929
`-XNoImplicitPrelude`, but it is likely that modules in the other component
3030
assume that the `Prelude` is implicitly imported.
3131

32+
`stack ghci` configures GHCi by using a GHCi script file. Such files are located
33+
in subdirectories of `<XDG_CACHE_HOME>/stack/ghci-script`, where
34+
`<XDG_CACHE_HOME>` refers to the
35+
[XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
36+
for user-specific non-essential (cached) data. On Unix-like operating systems,
37+
the default for `<XDG_CACHE_HOME>` is `$HOME/.cache`. On Windows, the default
38+
is `%LOCALAPPDATA%`.
39+
3240
## Selecting Main module
3341

3442
When loading multiple packages, there may be multiple definitions for the `Main`

package.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ dependencies:
125125
- streaming-commons
126126
- tar
127127
- template-haskell
128-
- temporary
129128
- text
130129
- text-metrics
131130
- th-reify-many

src/Data/Attoparsec/Interpreter.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import Conduit ( decodeUtf8C, withSourceFile )
6161
import Data.Conduit.Attoparsec ( ParseError (..), Position (..), sinkParserEither )
6262
import Data.List ( intercalate )
6363
import Data.Text ( pack )
64+
import Stack.Constants ( stackProgName )
6465
import Stack.Prelude
6566
import System.FilePath ( takeExtension )
6667
import System.IO ( hPutStrLn )

src/Stack/Build/Execute.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ import Stack.Constants
113113
, relDirSetupExeCache, relDirSetupExeSrc, relFileBuildLock
114114
, relFileConfigure, relFileSetupHs, relFileSetupLhs
115115
, relFileSetupLower, relFileSetupMacrosH, setupGhciShimCode
116-
, testGhcEnvRelFile
116+
, stackProgName, testGhcEnvRelFile
117117
)
118118
import Stack.Constants.Config
119119
( distDirFromDir, distRelativeDir, hpcDirFromDir

src/Stack/Config.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ import Stack.Constants
8585
, platformVariantEnvVar, relDirBin, relDirStackWork
8686
, relFileReadmeTxt, relFileStorage, relDirPantry
8787
, relDirPrograms, relDirStackProgName, relDirUpperPrograms
88-
, stackDeveloperModeDefault, stackDotYaml, stackRootEnvVar
89-
, stackWorkEnvVar, stackXdgEnvVar
88+
, stackDeveloperModeDefault, stackDotYaml, stackProgName
89+
, stackRootEnvVar, stackWorkEnvVar, stackXdgEnvVar
9090
)
9191
import Stack.Lock ( lockCachedWanted )
9292
import Stack.Prelude

src/Stack/Constants.hs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module Stack.Constants
1010
, buildPlanCacheDir
1111
, haskellFileExts
1212
, haskellDefaultPreprocessorExts
13+
, stackProgName
14+
, stackProgName'
1315
, stackDotYaml
1416
, stackWorkEnvVar
1517
, stackRootEnvVar
@@ -44,6 +46,8 @@ module Stack.Constants
4446
, relFileCabalMacrosH
4547
, relDirBuild
4648
, relDirBin
49+
, relDirGhci
50+
, relDirGhciScript
4751
, relDirPantry
4852
, relDirPrograms
4953
, relDirUpperPrograms
@@ -88,7 +92,6 @@ module Stack.Constants
8892
, relDirAll
8993
, relFilePackageCache
9094
, relFileDockerfile
91-
, relDirHaskellStackGhci
9295
, relFileGhciScript
9396
, relDirCombined
9497
, relFileHpcIndexHtml
@@ -134,10 +137,12 @@ import Data.ByteString.Builder ( byteString )
134137
import Data.Char ( toUpper )
135138
import Data.FileEmbed ( embedFile, makeRelativeToProject )
136139
import qualified Data.Set as Set
140+
import qualified Data.Text as T
137141
import Distribution.Package ( mkPackageName )
138142
import Hpack.Config ( packageConfig )
139143
import qualified Language.Haskell.TH.Syntax as TH ( runIO, lift )
140144
import Path ( (</>), mkRelDir, mkRelFile, parseAbsFile )
145+
import Stack.Constants.StackProgName ( stackProgName )
141146
import Stack.Constants.UsrLibDirs ( usrLibDirs )
142147
import Stack.Prelude
143148
import Stack.Types.Compiler ( WhichCompiler (..) )
@@ -154,6 +159,10 @@ instance Exception ConstantsException where
154159
displayException WiredInPackagesNotParsedBug = bugReport "[S-6057]"
155160
"Parse error in wiredInPackages."
156161

162+
-- | Name of the Stack program.
163+
stackProgName' :: Text
164+
stackProgName' = T.pack stackProgName
165+
157166
-- | Extensions used for Haskell modules. Excludes preprocessor ones.
158167
haskellFileExts :: [Text]
159168
haskellFileExts = ["hs", "hsc", "lhs"]
@@ -357,6 +366,12 @@ relDirBuild = $(mkRelDir "build")
357366
relDirBin :: Path Rel Dir
358367
relDirBin = $(mkRelDir "bin")
359368

369+
relDirGhci :: Path Rel Dir
370+
relDirGhci = $(mkRelDir "ghci")
371+
372+
relDirGhciScript :: Path Rel Dir
373+
relDirGhciScript = $(mkRelDir "ghci-script")
374+
360375
relDirPantry :: Path Rel Dir
361376
relDirPantry = $(mkRelDir "pantry")
362377

@@ -493,9 +508,6 @@ relFilePackageCache = $(mkRelFile "package.cache")
493508
relFileDockerfile :: Path Rel File
494509
relFileDockerfile = $(mkRelFile "Dockerfile")
495510

496-
relDirHaskellStackGhci :: Path Rel Dir
497-
relDirHaskellStackGhci = $(mkRelDir "haskell-stack-ghci")
498-
499511
relFileGhciScript :: Path Rel File
500512
relFileGhciScript = $(mkRelFile "ghci-script")
501513

src/Stack/Constants/Config.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module Stack.Constants.Config
2424
) where
2525

2626
import Path ( (</>), mkRelDir, mkRelFile, parseRelDir )
27-
import Stack.Constants ( cabalPackageName )
27+
import Stack.Constants
28+
( cabalPackageName, relDirDist, relDirGhci, relDirHpc )
2829
import Stack.Prelude
2930
import Stack.Types.BuildConfig ( HasBuildConfig, projectRootL )
3031
import Stack.Types.CompilerPaths ( cabalVersionL )
@@ -44,7 +45,7 @@ ghciDirL :: HasBuildConfig env => Getting r env (Path Abs Dir)
4445
ghciDirL = to $ \env -> -- FIXME is this idiomatic lens code?
4546
let workDir = view workDirL env
4647
root = view projectRootL env
47-
in root </> workDir </> $(mkRelDir "ghci/")
48+
in root </> workDir </> relDirGhci
4849

4950
-- | The directory containing the files used for dirtiness check of source files.
5051
buildCachesDir :: (MonadThrow m, MonadReader env m, HasEnvConfig env)
@@ -112,7 +113,7 @@ hpcDirFromDir fp =
112113
hpcRelativeDir :: (MonadThrow m, MonadReader env m, HasEnvConfig env)
113114
=> m (Path Rel Dir)
114115
hpcRelativeDir =
115-
fmap (</> $(mkRelDir "hpc")) distRelativeDir
116+
fmap (</> relDirHpc) distRelativeDir
116117

117118
-- | Package's setup-config storing Cabal configuration
118119
setupConfigFromDir :: (MonadThrow m, MonadReader env m, HasEnvConfig env)
@@ -145,7 +146,7 @@ rootDistRelativeDir ::
145146
=> m (Path Rel Dir)
146147
rootDistRelativeDir = do
147148
workDir <- view workDirL
148-
pure $ workDir </> $(mkRelDir "dist")
149+
pure $ workDir </> relDirDist
149150

150151
-- | Package's working directory.
151152
workDirFromDir :: (MonadReader env m, HasConfig env)

src/Stack/Constants/StackProgName.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
module Stack.Constants.StackProgName
4+
( stackProgName
5+
) where
6+
7+
import Stack.Prelude ( String )
8+
9+
-- | Name of the Stack program.
10+
11+
-- NOTE: Defined in this module rather than in "Stack.Constants", due to
12+
-- GHC stage restrictions and the use of Template Haskell.
13+
stackProgName :: String
14+
stackProgName = "stack"

src/Stack/Ghci.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import qualified Data.Text as T
2525
import qualified Data.Text.Lazy as TL
2626
import qualified Data.Text.Lazy.Encoding as TLE
2727
import qualified Distribution.PackageDescription as C
28-
import Path ((</>), parent, parseAbsDir, parseRelFile )
28+
import Path ((</>), parent, parseRelFile )
2929
import Path.Extra ( forgivingResolveFile', toFilePathNoTrailingSep )
30-
import Path.IO ( doesFileExist, ensureDir )
30+
import Path.IO
31+
( XdgDirectory (..), doesFileExist, ensureDir, getXdgDir )
3132
import RIO.Process
3233
( HasProcessContext, exec, proc, readProcess_
3334
, withWorkingDir
@@ -38,8 +39,8 @@ import Stack.Build.Source
3839
( getLocalFlags, localDependencies, projectLocalPackages )
3940
import Stack.Build.Target ( NeedTargets (..), parseTargets )
4041
import Stack.Constants
41-
( relDirHaskellStackGhci, relFileCabalMacrosH
42-
, relFileGhciScript
42+
( relDirGhciScript, relDirStackProgName, relFileCabalMacrosH
43+
, relFileGhciScript, stackProgName'
4344
)
4445
import Stack.Constants.Config ( ghciDirL, objectInterfaceDirL )
4546
import Stack.Ghci.Script
@@ -85,7 +86,6 @@ import Stack.Types.SourceMap
8586
, SMTargets (..), SMWanted (..), SourceMap (..), Target (..)
8687
)
8788
import System.IO ( putStrLn )
88-
import System.IO.Temp ( getCanonicalTemporaryDirectory )
8989
import System.Permissions ( setScriptPerms )
9090

9191
-- | Type representing exceptions thrown by functions exported by the
@@ -566,9 +566,8 @@ runGhci GhciOpts{..} targets mainFile pkgs extraFiles exposePackages = do
566566
-- file names are determined by hashing. This also has the nice side
567567
-- effect of making it possible to copy the ghci invocation out of
568568
-- the log and have it still work.
569-
tmpDirectory <-
570-
(</> relDirHaskellStackGhci) <$>
571-
(parseAbsDir =<< liftIO getCanonicalTemporaryDirectory)
569+
tmpDirectory <- getXdgDir XdgCache $
570+
Just (relDirStackProgName </> relDirGhciScript)
572571
ghciDir <- view ghciDirL
573572
ensureDir ghciDir
574573
ensureDir tmpDirectory

0 commit comments

Comments
 (0)