From 78bdf86a952753b9381c8f50d95002bc97d700cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gr=C3=B6ber?= Date: Wed, 19 Aug 2015 06:48:27 +0200 Subject: [PATCH] Fix all the stack related things --- .travis.yml | 1 + Language/Haskell/GhcMod/CabalHelper.hs | 31 +++++++++-------- Language/Haskell/GhcMod/Caching/Types.hs | 2 +- Language/Haskell/GhcMod/Cradle.hs | 2 +- Language/Haskell/GhcMod/PathsAndFiles.hs | 40 ++++++++++++---------- Language/Haskell/GhcMod/Target.hs | 10 +++--- ghc-mod.cabal | 9 ++--- test/CabalHelperSpec.hs | 7 ++-- test/Main.hs | 22 +++++++----- test/data/stack-project/new-template.cabal | 5 +-- test/data/stack-project/stack.yaml | 5 +++ 11 files changed, 75 insertions(+), 59 deletions(-) create mode 100644 test/data/stack-project/stack.yaml diff --git a/.travis.yml b/.travis.yml index d8d96ad..d836c9b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ cache: directories: - ~/.cabal - ~/.ghc + - ~/.stack before_cache: - rm -f $HOME/.cabal/logs $HOME/.cabal/packages/*/build-reports.log diff --git a/Language/Haskell/GhcMod/CabalHelper.hs b/Language/Haskell/GhcMod/CabalHelper.hs index d269bb4..2b77ec6 100644 --- a/Language/Haskell/GhcMod/CabalHelper.hs +++ b/Language/Haskell/GhcMod/CabalHelper.hs @@ -53,24 +53,24 @@ import Paths_ghc_mod as GhcMod -- access home modules getGhcMergedPkgOptions :: (Applicative m, IOish m, GmEnv m, GmState m, GmLog m) => m [GHCOption] -getGhcMergedPkgOptions = chCached $ \distDir -> Cached { +getGhcMergedPkgOptions = chCached $ \distdir -> Cached { cacheLens = Just (lGmcMergedPkgOptions . lGmCaches), - cacheFile = distDir mergedPkgOptsCacheFile, - cachedAction = \ _tcf (progs, rootdir, distdir, _) _ma -> do + cacheFile = mergedPkgOptsCacheFile distdir, + cachedAction = \ _tcf (progs, rootdir, _) _ma -> do readProc <- gmReadProcess opts <- withCabal $ runQuery'' readProc progs rootdir distdir $ ghcMergedPkgOptions - return ([distDir setupConfigPath], opts) + return ([setupConfigPath distdir], opts) } getCabalPackageDbStack :: (IOish m, GmEnv m, GmState m, GmLog m) => m [GhcPkgDb] -getCabalPackageDbStack = chCached $ \distDir -> Cached { +getCabalPackageDbStack = chCached $ \distdir -> Cached { cacheLens = Just (lGmcPackageDbStack . lGmCaches), - cacheFile = distDir pkgDbStackCacheFile, - cachedAction = \ _tcf (progs, rootdir, distdir, _) _ma -> do + cacheFile = pkgDbStackCacheFile distdir, + cachedAction = \ _tcf (progs, rootdir, _) _ma -> do readProc <- gmReadProcess dbs <- withCabal $ map chPkgToGhcPkg <$> runQuery'' readProc progs rootdir distdir packageDbStack - return ([distDir setupConfigPath, sandboxConfigFile], dbs) + return ([setupConfigPath distdir, sandboxConfigFile], dbs) } chPkgToGhcPkg :: ChPkgDb -> GhcPkgDb @@ -85,10 +85,10 @@ chPkgToGhcPkg (ChPkgSpecific f) = PackageDb f -- 'resolveGmComponents'. getComponents :: (Applicative m, IOish m, GmEnv m, GmState m, GmLog m) => m [GmComponent 'GMCRaw ChEntrypoint] -getComponents = chCached$ \distDir -> Cached { +getComponents = chCached$ \distdir -> Cached { cacheLens = Just (lGmcComponents . lGmCaches), - cacheFile = distDir cabalHelperCacheFile, - cachedAction = \ _tcf (progs, rootdir, distdir, _vers) _ma -> do + cacheFile = cabalHelperCacheFile distdir, + cachedAction = \ _tcf (progs, rootdir, _vers) _ma -> do readProc <- gmReadProcess runQuery'' readProc progs rootdir distdir $ do q <- join7 @@ -100,7 +100,7 @@ getComponents = chCached$ \distDir -> Cached { <*> entrypoints <*> sourceDirs let cs = flip map q $ curry8 (GmComponent mempty) - return ([distDir setupConfigPath], cs) + return ([setupConfigPath distdir], cs) } where curry8 fn (a, (b, (c, (d, (e, (f, (g, h))))))) = fn a b c d e f g h @@ -226,14 +226,15 @@ chCached :: (Applicative m, IOish m, GmEnv m, GmState m, GmLog m, Serialize a) chCached c = do root <- cradleRootDir <$> cradle dist <- cradleDistDir <$> cradle - d <- cacheInputData root dist + d <- cacheInputData root withCabal $ cached root (c dist) d where - cacheInputData root dist = do + -- we don't need to include the disdir in the cache input because when it + -- changes the cache files will be gone anyways ;) + cacheInputData root = do opt <- options return $ ( helperProgs opt , root - , root dist , (gmVer, chVer) ) diff --git a/Language/Haskell/GhcMod/Caching/Types.hs b/Language/Haskell/GhcMod/Caching/Types.hs index c4fce4c..2a21b1a 100644 --- a/Language/Haskell/GhcMod/Caching/Types.hs +++ b/Language/Haskell/GhcMod/Caching/Types.hs @@ -49,4 +49,4 @@ data TimedCacheFiles = TimedCacheFiles { -- ^ Timestamped files returned by the cached action } deriving (Eq, Ord, Show) -type ChCacheData = (Programs, FilePath, FilePath, (Version, [Char])) +type ChCacheData = (Programs, FilePath, (Version, [Char])) diff --git a/Language/Haskell/GhcMod/Cradle.hs b/Language/Haskell/GhcMod/Cradle.hs index 43325e1..e514e5b 100644 --- a/Language/Haskell/GhcMod/Cradle.hs +++ b/Language/Haskell/GhcMod/Cradle.hs @@ -75,7 +75,7 @@ stackCradle wdir = do let cabalDir = takeDirectory cabalFile _stackConfigFile <- MaybeT $ findStackConfigFile cabalDir - distDir <- liftIO $ findStackDistDir cabalDir + distDir <- MaybeT $ getStackDistDir cabalDir return Cradle { cradleProjectType = StackProject diff --git a/Language/Haskell/GhcMod/PathsAndFiles.hs b/Language/Haskell/GhcMod/PathsAndFiles.hs index 5a0d881..f596ccd 100644 --- a/Language/Haskell/GhcMod/PathsAndFiles.hs +++ b/Language/Haskell/GhcMod/PathsAndFiles.hs @@ -22,6 +22,7 @@ module Language.Haskell.GhcMod.PathsAndFiles ( import Config (cProjectVersion) import Control.Applicative import Control.Monad +import Control.Monad.Trans.Maybe import Data.List import Data.Char import Data.Maybe @@ -74,13 +75,10 @@ findCabalFile dir = do findStackConfigFile :: FilePath -> IO (Maybe FilePath) findStackConfigFile dir = mightExist (dir "stack.yaml") -findStackDistDir :: FilePath -> IO FilePath -findStackDistDir dir = U.withDirectory_ dir $ do - mstack <- liftIO $ findExecutable "stack" - case mstack of - Nothing -> return "dist" - Just stack -> - takeWhile (/='\n') <$> readProcess stack ["path", "--dist-dir"] "" +getStackDistDir :: FilePath -> IO (Maybe FilePath) +getStackDistDir dir = U.withDirectory_ dir $ runMaybeT $ do + stack <- MaybeT $ findExecutable "stack" + liftIO $ takeWhile (/='\n') <$> readProcess stack ["path", "--dist-dir"] "" -- | Get path to sandbox config file getSandboxDb :: FilePath @@ -190,14 +188,16 @@ parents dir' = ---------------------------------------------------------------- setupConfigFile :: Cradle -> FilePath -setupConfigFile crdl = cradleRootDir crdl cradleDistDir crdl setupConfigPath +setupConfigFile crdl = + cradleRootDir crdl setupConfigPath (cradleDistDir crdl) sandboxConfigFile :: FilePath sandboxConfigFile = "cabal.sandbox.config" -- | Path to 'LocalBuildInfo' file, usually @dist/setup-config@ -setupConfigPath :: FilePath -setupConfigPath = "setup-config" -- localBuildInfoFile defaultDistPref +setupConfigPath :: FilePath -> FilePath +setupConfigPath dist = dist "setup-config" + -- localBuildInfoFile defaultDistPref macrosHeaderPath :: FilePath macrosHeaderPath = "build/autogen/cabal_macros.h" @@ -216,17 +216,21 @@ symbolCache crdl = cradleTempDir crdl symbolCacheFile symbolCacheFile :: String symbolCacheFile = "ghc-mod.symbol-cache" -resolvedComponentsCacheFile :: String -resolvedComponentsCacheFile = setupConfigPath <.> "ghc-mod.resolved-components" +resolvedComponentsCacheFile :: FilePath -> FilePath +resolvedComponentsCacheFile dist = + setupConfigPath dist <.> "ghc-mod.resolved-components" -cabalHelperCacheFile :: String -cabalHelperCacheFile = setupConfigPath <.> "ghc-mod.cabal-components" +cabalHelperCacheFile :: FilePath -> FilePath +cabalHelperCacheFile dist = + setupConfigPath dist <.> "ghc-mod.cabal-components" -mergedPkgOptsCacheFile :: String -mergedPkgOptsCacheFile = setupConfigPath <.> "ghc-mod.package-options" +mergedPkgOptsCacheFile :: FilePath -> FilePath +mergedPkgOptsCacheFile dist = + setupConfigPath dist <.> "ghc-mod.package-options" -pkgDbStackCacheFile :: String -pkgDbStackCacheFile = setupConfigPath <.> "ghc-mod.package-db-stack" +pkgDbStackCacheFile :: FilePath -> FilePath +pkgDbStackCacheFile dist = + setupConfigPath dist <.> "ghc-mod.package-db-stack" -- | @findCustomPackageDbFile dir@. Searches for a @.ghc-mod.cradle@ file in @dir@. -- If it exists in the given directory it is returned otherwise @findCradleFile@ returns @Nothing@ diff --git a/Language/Haskell/GhcMod/Target.hs b/Language/Haskell/GhcMod/Target.hs index 2f6fbb7..7fe9697 100644 --- a/Language/Haskell/GhcMod/Target.hs +++ b/Language/Haskell/GhcMod/Target.hs @@ -182,9 +182,9 @@ resolvedComponentsCache :: IOish m => FilePath -> Cached (GhcModT m) GhcModState [GmComponent 'GMCRaw (Set.Set ModulePath)] (Map.Map ChComponentName (GmComponent 'GMCResolved (Set.Set ModulePath))) -resolvedComponentsCache distDir = Cached { +resolvedComponentsCache distdir = Cached { cacheLens = Just (lGmcResolvedComponents . lGmCaches), - cacheFile = distDir resolvedComponentsCacheFile, + cacheFile = resolvedComponentsCacheFile distdir, cachedAction = \tcfs comps ma -> do Cradle {..} <- cradle let iifsM = invalidatingInputFiles tcfs @@ -195,13 +195,13 @@ resolvedComponentsCache distDir = Cached { Just iifs -> let filterOutSetupCfg = - filter (/= cradleRootDir cradleDistDir setupConfigPath) + filter (/= cradleRootDir setupConfigPath distdir) changedFiles = filterOutSetupCfg iifs in if null changedFiles then Nothing else Just $ map Left changedFiles setupChanged = maybe False - (elem $ cradleRootDir cradleDistDir setupConfigPath) + (elem $ cradleRootDir setupConfigPath distdir) iifsM case (setupChanged, ma) of (False, Just mcs) -> gmsGet >>= \s -> gmsPut s { gmComponents = mcs } @@ -218,7 +218,7 @@ resolvedComponentsCache distDir = Cached { text "files changed" <+>: changedDoc mcs <- resolveGmComponents mums comps - return ((cradleDistDir setupConfigPath) : flatten mcs , mcs) + return (setupConfigPath distdir : flatten mcs , mcs) } where diff --git a/ghc-mod.cabal b/ghc-mod.cabal index ab2d1ae..595658c 100644 --- a/ghc-mod.cabal +++ b/ghc-mod.cabal @@ -85,11 +85,12 @@ Extra-Source-Files: ChangeLog test/data/file-mapping/preprocessor/*.hs test/data/file-mapping/lhs/*.lhs test/data/nice-qualification/*.hs + test/data/stack-project/stack.yaml test/data/stack-project/new-template.cabal - test/data/stack-project/Setup.hs - test/data/stack-project/app/Main.hs - test/data/stack-project/src/Lib.hs - test/data/stack-project/test/Spec.hs + test/data/stack-project/*.hs + test/data/stack-project/app/*.hs + test/data/stack-project/src/*.hs + test/data/stack-project/test/*.hs Library Default-Language: Haskell2010 diff --git a/test/CabalHelperSpec.hs b/test/CabalHelperSpec.hs index 69e3814..231fc3c 100644 --- a/test/CabalHelperSpec.hs +++ b/test/CabalHelperSpec.hs @@ -58,10 +58,9 @@ spec = do it "handles stack project" $ do let tdir = "test/data/stack-project" - opts <- map gmcGhcOpts <$> runD' tdir getComponents - let ghcOpts = head opts - pkgs = pkgOptions ghcOpts - pkgs `shouldBe` ["Cabal","base","new-template"] + [ghcOpts] <- map gmcGhcOpts . filter ((==ChExeName "new-template-exe") . gmcName) <$> runD' tdir getComponents + let pkgs = pkgOptions ghcOpts + pkgs `shouldBe` ["base", "bytestring"] it "extracts build dependencies" $ do let tdir = "test/data/cabal-project" diff --git a/test/Main.hs b/test/Main.hs index b9827d0..2e9f558 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -28,12 +28,6 @@ main = do genSandboxCfg `mapM_` sandboxes genGhcPkgCache `mapM_` pkgDirs - let stackDir = "test/data/stack-project" - void $ withDirectory_ stackDir $ do - void $ system "stack init --force" - void $ system "stack setup" - void $ system "stack build" - let caches = [ "setup-config" , "setup-config.ghc-mod.cabal-helper" , "setup-config.ghc-mod.cabal-components" @@ -42,16 +36,26 @@ main = do , "setup-config.ghc-mod.package-db-stack" , "ghc-mod.cache" ] - cachesFindExp :: String - cachesFindExp = unwords $ intersperse "-o " $ map ("-name "++) caches + findExp = unwords $ intersperse "-o " $ concat [ + stackWorkFindExp, + cachesFindExp + ] + cachesFindExp = map ("-name "++) caches + stackWorkFindExp = ["-name .stack-work -type d"] - cleanCmd = "find test \\( "++ cachesFindExp ++" \\) -exec rm {} \\;" + cleanCmd = "find test \\( "++ findExp ++" \\) -exec rm -r {} \\;" putStrLn $ "$ " ++ cleanCmd void $ system cleanCmd void $ system "cabal --version" void $ system "ghc --version" + let stackDir = "test/data/stack-project" + void $ withDirectory_ stackDir $ do +-- void $ system "stack init --force" + void $ system "stack setup" + void $ system "stack build" + (putStrLn =<< runD debugInfo) `E.catch` (\(_ :: E.SomeException) -> return () ) diff --git a/test/data/stack-project/new-template.cabal b/test/data/stack-project/new-template.cabal index 34a3a0b..c71f211 100644 --- a/test/data/stack-project/new-template.cabal +++ b/test/data/stack-project/new-template.cabal @@ -7,10 +7,10 @@ homepage: http://github.com/name/project -- license-file: LICENSE author: Your name here maintainer: your.address@example.com --- copyright: +-- copyright: category: Web build-type: Simple --- extra-source-files: +-- extra-source-files: cabal-version: >=1.10 library @@ -25,6 +25,7 @@ executable new-template-exe ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , new-template + , bytestring default-language: Haskell2010 test-suite new-template-test diff --git a/test/data/stack-project/stack.yaml b/test/data/stack-project/stack.yaml new file mode 100644 index 0000000..6252712 --- /dev/null +++ b/test/data/stack-project/stack.yaml @@ -0,0 +1,5 @@ +flags: {} +packages: +- '.' +extra-deps: [] +resolver: lts-2.17