@@ -14,25 +14,21 @@ module Obelisk.Command.Run where
1414import Control.Arrow ((&&&))
1515import Control.Exception (Exception, bracket)
1616import Control.Lens (ifor, (.~), (&))
17−import Control.Monad (filterM)
17+import Control.Monad (filterM, unless, void)
1818import Control.Monad.Except (runExceptT, throwError)
1919import Control.Monad.IO.Class (liftIO)
2020import Control.Monad.Reader (MonadIO)
21−import Data.Bifoldable (bifoldr1)
22−import Data.Bifunctor (bimap)
2321import Data.Coerce (coerce)
2422import Data.Default (def)
2523import Data.Either (partitionEithers)
2624import Data.Foldable (fold, for_, toList)
2725import Data.Functor.Identity (runIdentity)
28−import Data.List (intercalate)
2926import Data.List.NonEmpty (NonEmpty)
3027import qualified Data.List.NonEmpty as NE
3128import Data.Map (Map)
3229import qualified Data.Map as Map
3330import qualified Data.Map.Monoidal as MMap
34−import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
35−import Data.Ord (comparing)
31+import Data.Maybe
3632import Data.Set (Set)
3733import qualified Data.Set as Set
3834import Data.String.Here.Interpolated (i)
@@ -46,17 +42,17 @@ import Distribution.Compiler (CompilerFlavor(..))
4642import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
4743import Distribution.Parsec.ParseResult (runParseResult)
4844import qualified Distribution.System as Dist
49−import Distribution.Types.BuildInfo (buildable, cppOptions, defaultExtensions, defaultLanguage, hsSourceDirs, options)
50−import Distribution.Types.CondTree (simplifyCondTree)
51−import Distribution.Types.GenericPackageDescription (ConfVar (Arch, Impl, OS), condLibrary)
52−import Distribution.Types.Library (libBuildInfo)
53−import Distribution.Utils.Generic (toUTF8BS, readUTF8File)
45+import Distribution.Types.BuildInfo
46+import Distribution.Types.CondTree
47+import Distribution.Types.GenericPackageDescription
48+import Distribution.Types.Library
49+import Distribution.Utils.Generic
5450import qualified Distribution.Parsec.Common as Dist
5551import qualified Hpack.Config as Hpack
5652import qualified Hpack.Render as Hpack
5753import qualified Hpack.Yaml as Hpack
58−import Language.Haskell.Extension (Extension, Language)
59−import qualified Network.Socket as Socket
54+import Language.Haskell.Extension
55+import Network.Socket hiding (Debug)
6056import System.Directory
6157import System.Environment (getExecutablePath)
6258import System.FilePath
@@ -66,18 +62,19 @@ import System.IO.Temp (withSystemTempDirectory)
6662import Obelisk.App (MonadObelisk)
6763import Obelisk.CliApp (
6864 Severity (..),
65+ createProcess_,
6966 failWith,
7067 proc,
7168 putLog,
7269 readCreateProcessWithExitCode,
7370 readProcessAndLogStderr,
74− runProcess_,
7571 setCwd,
7672 setDelegateCtlc,
73+ waitForProcess,
7774 withSpinner,
7875 )
7976import Obelisk.Command.Nix
80−import Obelisk.Command.Project (nixShellWithoutPkgs, withProjectRoot, findProjectAssets, bashEscape)
77+import Obelisk.Command.Project (nixShellWithPkgs, withProjectRoot, findProjectAssets)
8178import Obelisk.Command.Thunk (attrCacheFileName)
8279import Obelisk.Command.Utils (findExePath, ghcidExePath)
8380
@@ -94,11 +91,9 @@ data CabalPackageInfo = CabalPackageInfo
9491 -- ^ List of globally set languages of the library component
9592 , _cabalPackageInfo_compilerOptions :: [(CompilerFlavor, [String])]
9693 -- ^ List of compiler-specific options (e.g., the "ghc-options" field of the cabal file)
97− , _cabalPackageInfo_cppOptions :: [String]
98− -- ^ List of CPP (C Preprocessor) options (e.g. the "cpp-options" field of the cabal file)
9994 }
10095
101−-- | 'Bool' with a better name for its purpose.
96+-- | 'Bool' with a better name for it's purpose.
10297data Interpret = Interpret_Interpret | Interpret_NoInterpret deriving (Eq, Ord, Show)
10398
10499textInterpret :: Interpret -> Text
@@ -112,6 +107,9 @@ data PathTree a = PathTree_Node
112107 (Map FilePath (PathTree a)) -- Branches to deeper leaves
113108 deriving (Eq, Ord, Show, Functor, Foldable, Traversable)
114109
110+emptyPathTree :: PathTree a
111+emptyPathTree = PathTree_Node Nothing mempty
112+
115113-- | 2D ASCII drawing of a 'PathTree'. Adapted from Data.Tree.draw.
116114drawPathTree :: (a -> Text) -> PathTree a -> Text
117115drawPathTree showA (PathTree_Node _ ts0) = T.intercalate "\n" $ goForest (Map.toList ts0)
@@ -151,7 +149,7 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
151149 liftIO $ createDirectoryIfMissing True $ takeDirectory $ root </> profileBaseName
152150 putLog Debug $ "Storing profiled data under base name of " <> T.pack (root </> profileBaseName)
153151 freePort <- getFreePort
154− runProcess_ $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
152+ (_, _, _, ph) <- createProcess_ "runProfExe" $ setCwd (Just root) $ setDelegateCtlc True $ proc (outPath </> "bin" </> "ob-run") $
155153 [ show freePort
156154 , T.unpack assets
157155 , profileBaseName
@@ -159,16 +157,16 @@ profile profileBasePattern rtsFlags = withProjectRoot "." $ \root -> do
159157 , "-po" <> profileBaseName
160158 ] <> rtsFlags
161159 <> [ "-RTS" ]
160+ void $ waitForProcess ph
162161
163162run :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
164163run root interpretPaths = do
165164 pkgs <- getParsedLocalPkgs root interpretPaths
166− assets <- findProjectAssets root
167− putLog Debug $ "Assets impurely loaded from: " <> assets
168− ghciArgs <- getGhciSessionSettings pkgs root True
169− freePort <- getFreePort
170− withGhciScriptArgs pkgs $ \dotGhciArgs -> do
171− runGhcid root True (ghciArgs <> dotGhciArgs) pkgs $ Just $ unwords
165+ withGhciScript pkgs root $ \dotGhciPath -> do
166+ freePort <- getFreePort
167+ assets <- findProjectAssets root
168+ putLog Debug $ "Assets impurely loaded from: " <> assets
169+ runGhcid root True dotGhciPath pkgs $ Just $ unwords
172170 [ "Obelisk.Run.run"
173171 , show freePort
174172 , "(Obelisk.Run.runServeAsset " ++ show assets ++ ")"
@@ -179,26 +177,24 @@ run root interpretPaths = do
179177runRepl :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
180178runRepl root interpretPaths = do
181179 pkgs <- getParsedLocalPkgs root interpretPaths
182− ghciArgs <- getGhciSessionSettings pkgs "." True
183− withGhciScriptArgs pkgs $ \dotGhciArgs ->
184− runGhciRepl root pkgs (ghciArgs <> dotGhciArgs)
180+ withGhciScript pkgs "." $ \dotGhciPath ->
181+ runGhciRepl root pkgs dotGhciPath
185182
186183runWatch :: MonadObelisk m => FilePath -> PathTree Interpret -> m ()
187184runWatch root interpretPaths = do
188185 pkgs <- getParsedLocalPkgs root interpretPaths
189− ghciArgs <- getGhciSessionSettings pkgs root True
190− withGhciScriptArgs pkgs $ \dotGhciArgs ->
191− runGhcid root True (ghciArgs <> dotGhciArgs) pkgs Nothing
186+ withGhciScript pkgs root $ \dotGhciPath ->
187+ runGhcid root True dotGhciPath pkgs Nothing
192188
193−exportGhciConfig :: MonadObelisk m => Bool -> FilePath -> PathTree Interpret -> m [String]
194−exportGhciConfig useRelativePaths root interpretPaths = do
189+exportGhciConfig :: MonadObelisk m => FilePath -> PathTree Interpret -> m [String]
190+exportGhciConfig root interpretPaths = do
195191 pkgs <- getParsedLocalPkgs root interpretPaths
196− getGhciSessionSettings pkgs "." useRelativePaths
192+ getGhciSessionSettings pkgs "."
197193
198194nixShellForInterpretPaths :: MonadObelisk m => Bool -> String -> FilePath -> PathTree Interpret -> Maybe String -> m ()
199195nixShellForInterpretPaths isPure shell root interpretPaths cmd = do
200196 pkgs <- getParsedLocalPkgs root interpretPaths
201− nixShellWithoutPkgs root isPure False (packageInfoToNamePathMap pkgs) shell cmd
197+ nixShellWithPkgs root isPure False (packageInfoToNamePathMap pkgs) shell cmd
202198
203199-- | Like 'getLocalPkgs' but also parses them and fails if any of them can't be parsed.
204200getParsedLocalPkgs :: MonadObelisk m => FilePath -> PathTree Interpret -> m (NonEmpty CabalPackageInfo)
@@ -304,16 +300,15 @@ parseCabalPackage
304300 -> m (Maybe CabalPackageInfo)
305301parseCabalPackage dir = parseCabalPackage' dir >>= \case
306302 Left err -> Nothing <$ putLog Error err
307− Right (Just (warnings, pkgInfo)) -> do
303+ Right (warnings, pkgInfo) -> do
308304 for_ warnings $ putLog Warning . T.pack . show
309305 pure $ Just pkgInfo
310− Right Nothing -> pure Nothing
311306
312307-- | Like 'parseCabalPackage' but returns errors and warnings directly so as to avoid 'MonadObelisk'.
313308parseCabalPackage'
314309 :: (MonadIO m)
315310 => FilePath -- ^ Package directory
316− -> m (Either T.Text (Maybe ([Dist.PWarning], CabalPackageInfo)))
311+ -> m (Either T.Text ([Dist.PWarning], CabalPackageInfo))
317312parseCabalPackage' pkg = runExceptT $ do
318313 (cabalContents, packageFile, packageName) <- guessCabalPackageFile pkg >>= \case
319314 Left GuessPackageFileError_NotFound -> throwError $ "No .cabal or package.yaml file found in " <> T.pack pkg
@@ -341,7 +336,7 @@ parseCabalPackage' pkg = runExceptT $ do
341336 case condLibrary <$> result of
342337 Right (Just condLib) -> do
343338 let (_, lib) = simplifyCondTree evalConfVar condLib
344− pure $ Just $ (warnings,) $ CabalPackageInfo
339+ pure $ (warnings,) $ CabalPackageInfo
345340 { _cabalPackageInfo_packageName = T.pack packageName
346341 , _cabalPackageInfo_packageFile = packageFile
347342 , _cabalPackageInfo_packageRoot = takeDirectory packageFile
@@ -353,19 +348,18 @@ parseCabalPackage' pkg = runExceptT $ do
353348 , _cabalPackageInfo_defaultLanguage =
354349 defaultLanguage $ libBuildInfo lib
355350 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
356− , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
357351 }
358− Right Nothing -> pure Nothing
352+ Right Nothing -> throwError "Haskell package has no library component"
359353 Left (_, errors) ->
360354 throwError $ T.pack $ "Failed to parse " <> packageFile <> ":\n" <> unlines (map show errors)
361355
362356parsePackagesOrFail :: (MonadObelisk m, Foldable f) => f FilePath -> m (NE.NonEmpty CabalPackageInfo)
363357parsePackagesOrFail dirs' = do
364− packageInfos' <- fmap catMaybes $ for dirs $ \dir -> do
358+ (pkgDirErrs, packageInfos') <- fmap partitionEithers $ for dirs $ \dir -> do
365359 flip fmap (parseCabalPackage dir) $ \case
366360 Just packageInfo
367− | _cabalPackageInfo_buildable packageInfo -> Just packageInfo
368− _ -> Nothing
361+ | _cabalPackageInfo_buildable packageInfo -> Right packageInfo
362+ _ -> Left dir
369363
370364 -- Sort duplicate packages such that we prefer shorter paths, but fall back to alphabetical ordering.
371365 let packagesByName = Map.map (NE.sortBy $ comparing $ \p -> let n = _cabalPackageInfo_packageFile p in (length n, n))
@@ -387,6 +381,9 @@ parsePackagesOrFail dirs' = do
387381 "No valid, buildable packages found" <> (if null dirs then "" else " in " <> intercalate ", " dirs)
388382 Just xs -> pure xs
389383
384+ unless (null pkgDirErrs) $
385+ putLog Warning $ T.pack $ "Failed to find buildable packages in " <> intercalate ", " pkgDirErrs
386+
390387 pure packageInfos
391388 where
392389 dirs = toList dirs'
@@ -394,57 +391,41 @@ parsePackagesOrFail dirs' = do
394391packageInfoToNamePathMap :: Foldable f => f CabalPackageInfo -> Map Text FilePath
395392packageInfoToNamePathMap = Map.fromList . map (_cabalPackageInfo_packageName &&& _cabalPackageInfo_packageRoot) . toList
396393
397−-- Like 'withGhciScript' but provides the precise ghci arguments to add to a ghci session
398−withGhciScriptArgs
399− :: (MonadObelisk m, Foldable f)
400− => f CabalPackageInfo -- ^ List of packages to load into ghci
401− -> ([String] -> m ()) -- ^ Action to run with the extra ghci arguments
402− -> m ()
403−withGhciScriptArgs packageInfos f = withGhciScript loadPreludeManually packageInfos $ \fp -> f ["-XNoImplicitPrelude", "-ghci-script", fp]
404− where
405− -- These lines must be first and allow the session to support a custom Prelude when @-XNoImplicitPrelude@
406− -- is passed to the ghci session.
407− loadPreludeManually =
408− [ ":add Prelude" -- @:add@ is used because it's less noisy when there is no custom Prelude
409− , ":set -XImplicitPrelude" -- Turn the default setting on
410− ]
411−
412394-- | Create ghci configuration to load the given packages
413395withGhciScript
414396 :: (MonadObelisk m, Foldable f)
415− => [String] -- ^ Commands to prefix to file
416− -> f CabalPackageInfo -- ^ List of packages to load into ghci
397+ => f CabalPackageInfo -- ^ List of packages to load into ghci
398+ -> FilePath -- ^ All paths written to the .ghci file will be relative to this path
417399 -> (FilePath -> m ()) -- ^ Action to run with the path to generated temporary .ghci
418400 -> m ()
419−withGhciScript preCommands (toList -> packageInfos) f =
420− withSystemTempDirectory "ob-ghci" $ \fp -> do
421− let dotGhciPath = fp </> ".ghci"
422− liftIO $ writeFile dotGhciPath dotGhci
423− f dotGhciPath
424− where
401+withGhciScript (toList -> packageInfos) pathBase f = do
402+ ghciSettings <- getGhciSessionSettings packageInfos pathBase
403+ let
425404 packageNames = Set.fromList $ map _cabalPackageInfo_packageName packageInfos
426405 modulesToLoad = mconcat
427406 [ [ "Obelisk.Run" | "obelisk-run" `Set.member` packageNames ]
428407 , [ "Backend" | "backend" `Set.member` packageNames ]
429408 , [ "Frontend" | "frontend" `Set.member` packageNames ]
430409 ]
431− dotGhci = unlines $
432− preCommands <>
433− [ if null modulesToLoad then "" else ":load " <> unwords modulesToLoad
410+ dotGhci = unlines
411+ [ ":set " <> unwords ghciSettings -- TODO: Shell escape
412+ , if null modulesToLoad then "" else ":load " <> unwords modulesToLoad
434413 , "import qualified Obelisk.Run"
435414 , "import qualified Frontend"
436− , "import qualified Backend"
437− ]
415+ , "import qualified Backend" ]
416+ withSystemTempDirectory "ob-ghci" $ \fp -> do
417+ let dotGhciPath = fp </> ".ghci"
418+ liftIO $ writeFile dotGhciPath dotGhci
419+ f dotGhciPath
438420
439421-- | Builds a list of options to pass to ghci or set in .ghci file that configures
440422-- the preprocessor and source includes.
441423getGhciSessionSettings
442424 :: (MonadObelisk m, Foldable f)
443425 => f CabalPackageInfo -- ^ List of packages to load into ghci
444− -> FilePath -- ^ All paths will be relative to this path
445− -> Bool -- ^ Use relative paths
426+ -> FilePath -- ^ All paths written to the .ghci file will be relative to this path
446427 -> m [String]
447−getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
428+getGhciSessionSettings (toList -> packageInfos) pathBase = do
448429 -- N.B. ghci settings do NOT support escaping in any way. To minimize the likelihood that
449430 -- paths-with-spaces ruin our day, we first canonicalize everything, and then relativize
450431 -- all paths to 'pathBase'.
@@ -454,69 +435,66 @@ getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
454435 (pkgFiles, pkgSrcPaths :: [NonEmpty FilePath]) <- fmap unzip $ liftIO $ for packageInfos $ \pkg -> do
455436 canonicalSrcDirs <- traverse canonicalizePath $ (_cabalPackageInfo_packageRoot pkg </>) <$> _cabalPackageInfo_sourceDirs pkg
456437 canonicalPkgFile <- canonicalizePath $ _cabalPackageInfo_packageFile pkg
457− pure (canonicalPkgFile `relativeTo'` canonicalPathBase, (`relativeTo'` canonicalPathBase) <$> canonicalSrcDirs)
438+ pure (makeRelative canonicalPathBase canonicalPkgFile, makeRelative canonicalPathBase <$> canonicalSrcDirs)
458439
459440 pure
460− $ baseGhciOptions
461− <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
441+ $ ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
462442 <> concatMap (\p -> ["-optF", p]) pkgFiles
463443 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]
464− where
465− relativeTo' = if useRelativePaths then relativeTo else const
466−
467−baseGhciOptions :: [String]
468−baseGhciOptions =
469− [ "-ignore-dot-ghci"
470− , "-no-user-package-db"
471− , "-package-env", "-"
472− ]
473444
474445-- | Run ghci repl
475446runGhciRepl
476447 :: (MonadObelisk m, Foldable f)
477448 => FilePath -- ^ Path to project root
478− -> f CabalPackageInfo -- ^ Packages to keep unbuilt
479− -> [String] -- ^ GHCi arguments
449+ -> f CabalPackageInfo
450+ -> FilePath -- ^ Path to .ghci
480451 -> m ()
481−runGhciRepl root (toList -> packages) ghciArgs =
452+runGhciRepl root (toList -> packages) dotGhci =
482453 -- NOTE: We do *not* want to use $(staticWhich "ghci") here because we need the
483454 -- ghc that is provided by the shell in the user's project.
484− nixShellWithoutPkgs root True False (packageInfoToNamePathMap packages) "ghc" $
485− Just $ unwords $ fmap bashEscape $ "ghci" : ghciArgs
455+ nixShellWithPkgs root True False (packageInfoToNamePathMap packages) "ghc" $ Just $ "ghci " <> makeBaseGhciOptions dotGhci -- TODO: Shell escape
486456
487457-- | Run ghcid
488458runGhcid
489459 :: (MonadObelisk m, Foldable f)
490460 => FilePath -- ^ Path to project root
491461 -> Bool -- ^ Should we chdir to root when running this process?
492− -> [String] -- ^ GHCi arguments
493− -> f CabalPackageInfo -- ^ Packages to keep unbuilt
462+ -> FilePath -- ^ Path to .ghci
463+ -> f CabalPackageInfo
494464 -> Maybe String -- ^ Optional command to run at every reload
495465 -> m ()
496−runGhcid root chdirToRoot ghciArgs (toList -> packages) mcmd =
497− nixShellWithoutPkgs root True chdirToRoot (packageInfoToNamePathMap packages) "ghc" $
498− Just $ unwords $ fmap bashEscape $ ghcidExePath : opts
466+runGhcid root chdirToRoot dotGhci (toList -> packages) mcmd =
467+ nixShellWithPkgs root True chdirToRoot (packageInfoToNamePathMap packages) "ghc" $ Just $ unwords $ ghcidExePath : opts -- TODO: Shell escape
499468 where
500− opts = concat
501− [ ["-W"]
502− , ["--outputfile=ghcid-output.txt"]
503− , map (\x -> "--reload=" <> x) reloadFiles
504− , map (\x -> "--restart=" <> x) restartFiles
505− , maybe [] (\cmd -> ["--test=" <> cmd]) mcmd
506− , ["--command=" <> unwords ("ghci" : ghciArgs)]
507− ]
469+ opts =
470+ [ "-W"
471+ , "--command='ghci -ignore-dot-ghci " <> makeBaseGhciOptions dotGhci <> "' "
472+ , "--outputfile=ghcid-output.txt"
473+ ] <> map (\x -> "--reload='" <> x <> "'") reloadFiles
474+ <> map (\x -> "--restart='" <> x <> "'") restartFiles
475+ <> testCmd
476+ testCmd = maybeToList (flip fmap mcmd $ \cmd -> "--test='" <> cmd <> "'") -- TODO: Shell escape
477+
508478 adjustRoot x = if chdirToRoot then makeRelative root x else x
509479 reloadFiles = map adjustRoot [root </> "config"]
510480 restartFiles = map (adjustRoot . _cabalPackageInfo_packageFile) packages
511481
512−getFreePort :: MonadIO m => m Socket.PortNumber
513−getFreePort = liftIO $ Socket.withSocketsDo $ do
514− addr:_ <- Socket.getAddrInfo (Just Socket.defaultHints) (Just "127.0.0.1") (Just "0")
515− bracket (open addr) Socket.close Socket.socketPort
482+makeBaseGhciOptions :: FilePath -> String
483+makeBaseGhciOptions dotGhci =
484+ unwords
485+ [ "-no-user-package-db"
486+ , "-package-env -"
487+ , "-ghci-script " <> dotGhci
488+ ]
489+
490+getFreePort :: MonadIO m => m PortNumber
491+getFreePort = liftIO $ withSocketsDo $ do
492+ addr:_ <- getAddrInfo (Just defaultHints) (Just "127.0.0.1") (Just "0")
493+ bracket (open addr) close socketPort
516494 where
517495 open addr = do
518− sock <- Socket.socket (Socket.addrFamily addr) (Socket.addrSocketType addr) (Socket.addrProtocol addr)
519− Socket.bind sock (Socket.addrAddress addr)
496+ sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
497+ bind sock (addrAddress addr)
520498 return sock
521499
522500
@@ -526,29 +504,3 @@ pathToTree a p = go $ splitDirectories p
526504 where
527505 go [] = PathTree_Node (Just a) mempty
528506 go (x : xs) = PathTree_Node Nothing $ Map.singleton x $ go xs
529−
530−-- | Like 'zipWith' but pads with a padding value instead of stopping on the shortest list.
531−zipDefaultWith :: a -> b -> (a -> b -> c) -> [a] -> [b] -> [c]
532−zipDefaultWith _da _db _f [] [] = []
533−zipDefaultWith da db f (a:as) [] = f a db : zipDefaultWith da db f as []
534−zipDefaultWith da db f [] (b:bs) = f da b : zipDefaultWith da db f [] bs
535−zipDefaultWith da db f (a:as) (b:bs) = f a b : zipDefaultWith da db f as bs
536−
537−-- | Makes the first absolute path relative to the second absolute path.
538−--
539−-- Both input paths MUST be absolute.
540−--
541−-- Unlike 'makeRelative' this does not merely strip prefixes. It will introduce
542−-- enough @..@ paths to make the resulting path truly relative in virtually every
543−-- case. The only exception is on Windows when the two paths are on different
544−-- drives. In this case the resulting path may be absolute.
545−relativeTo :: FilePath -> FilePath -> FilePath
546−relativeTo dir base
547− = bifoldr1 (</>)
548− $ bimap (collapse . (".." <$) . catMaybes) (collapse . catMaybes)
549− $ unzip
550− $ dropWhile (\(a,b) -> a == b)
551− $ zipDefaultWith Nothing Nothing (,)
552− (map Just $ splitDirectories base)
553− (map Just $ splitDirectories dir)
554− where collapse = foldr (</>) ""