@@ -47,33 +47,54 @@ inferCradleTree start_dir =
4747 maybeItsBios
4848 -- If we have both a config file (cabal.project/stack.yaml) and a work dir
4949 -- (dist-newstyle/.stack-work), prefer that
50- <|> (cabalExecutable >> cabalConfigDir start_dir >>= \ dir -> cabalWorkDir dir >> pure (cabalCradle dir))
50+ <|> (cabalExecutable >> cabalConfigDir start_dir >>= \ dir -> cabalWorkDir dir >> pure (simpleCabalCradle dir))
5151 <|> (stackExecutable >> stackConfigDir start_dir >>= \ dir -> stackWorkDir dir >> stackCradle dir)
5252 -- If we have a cabal.project OR we have a .cabal and dist-newstyle, prefer cabal
53- <|> (cabalExecutable >> (cabalConfigDir start_dir <|> cabalFileAndWorkDir) >>= pure . cabalCradle )
53+ <|> (cabalExecutable >> (cabalConfigDir start_dir <|> cabalFileAndWorkDir) >>= pure . simpleCabalCradle )
5454 -- If we have a stack.yaml, use stack
5555 <|> (stackExecutable >> stackConfigDir start_dir >>= stackCradle)
5656 -- If we have a cabal file, use cabal
57- <|> (cabalExecutable >> cabalFileDir start_dir >>= pure . cabalCradle )
57+ <|> (cabalExecutable >> cabalFileDir start_dir >>= pure . simpleCabalCradle )
5858
5959 where
6060 maybeItsBios = (\ wdir -> (Bios (Program $ wdir </> " .hie-bios" ) Nothing Nothing , wdir)) <$> biosWorkDir start_dir
6161
6262 cabalFileAndWorkDir = cabalFileDir start_dir >>= (\ dir -> cabalWorkDir dir >> pure dir)
6363
64- stackCradle :: FilePath -> MaybeT IO (CradleTree a , FilePath )
65- stackCradle fp = do
66- pkgs <- stackYamlPkgs fp
67- pkgsWithComps <- liftIO $ catMaybes <$> mapM (nestedPkg fp) pkgs
68- let yaml = fp </> " stack.yaml"
69- pure $ (,fp) $ case pkgsWithComps of
70- [] -> Stack (StackType Nothing (Just yaml))
71- ps -> StackMulti mempty $ do
72- Package n cs <- ps
73- c <- cs
74- let (prefix, comp) = Implicit. stackComponent n c
75- pure (prefix, StackType (Just comp) (Just yaml))
76- cabalCradle fp = (Cabal $ CabalType Nothing Nothing , fp)
64+ -- | Generate a stack cradle given a filepath.
65+ --
66+ -- Since we assume there was proof that this file belongs to a stack cradle
67+ -- we look immediately for the relevant @*.cabal@ and @stack.yaml@ files.
68+ -- We do not look for package.yaml, as we assume the corresponding .cabal has
69+ -- been generated already.
70+ --
71+ -- We parse the @stack.yaml@ to find relevant @*.cabal@ file locations, then
72+ -- we parse the @*.cabal@ files to generate a mapping from @hs-source-dirs@ to
73+ -- component names.
74+ stackCradle :: FilePath -> MaybeT IO (CradleTree a , FilePath )
75+ stackCradle fp = do
76+ pkgs <- stackYamlPkgs fp
77+ pkgsWithComps <- liftIO $ catMaybes <$> mapM (nestedPkg fp) pkgs
78+ let yaml = fp </> " stack.yaml"
79+ pure $ (,fp) $ case pkgsWithComps of
80+ [] -> Stack (StackType Nothing (Just yaml))
81+ ps -> StackMulti mempty $ do
82+ Package n cs <- ps
83+ c <- cs
84+ let (prefix, comp) = Implicit. stackComponent n c
85+ pure (prefix, StackType (Just comp) (Just yaml))
86+
87+ -- | By default, we generate a simple cabal cradle which is equivalent to the
88+ -- following hie.yaml:
89+ --
90+ -- @
91+ -- cradle:
92+ -- cabal:
93+ -- @
94+ --
95+ -- Note, this only works reliable for reasonably modern cabal versions >= 3.2.
96+ simpleCabalCradle :: FilePath -> (CradleTree a , FilePath )
97+ simpleCabalCradle fp = (Cabal $ CabalType Nothing Nothing , fp)
7798
7899cabalExecutable :: MaybeT IO FilePath
79100cabalExecutable = MaybeT $ findExecutable " cabal"
0 commit comments