Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more docs for implicit discovery #3887

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions ghcide/session-loader/Development/IDE/Session/Implicit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,54 @@
maybeItsBios
-- If we have both a config file (cabal.project/stack.yaml) and a work dir
-- (dist-newstyle/.stack-work), prefer that
<|> (cabalExecutable >> cabalConfigDir start_dir >>= \dir -> cabalWorkDir dir >> pure (cabalCradle dir))
<|> (cabalExecutable >> cabalConfigDir start_dir >>= \dir -> cabalWorkDir dir >> pure (simpleCabalCradle dir))
<|> (stackExecutable >> stackConfigDir start_dir >>= \dir -> stackWorkDir dir >> stackCradle dir)
-- If we have a cabal.project OR we have a .cabal and dist-newstyle, prefer cabal
<|> (cabalExecutable >> (cabalConfigDir start_dir <|> cabalFileAndWorkDir) >>= pure . cabalCradle)
<|> (cabalExecutable >> (cabalConfigDir start_dir <|> cabalFileAndWorkDir) >>= pure . simpleCabalCradle)

Check warning on line 53 in ghcide/session-loader/Development/IDE/Session/Implicit.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Suggestion in inferCradleTree in module Development.IDE.Session.Implicit: Use <&> ▫︎ Found: "cabalExecutable\n >> (cabalConfigDir start_dir <|> cabalFileAndWorkDir)\n >>= pure . simpleCabalCradle" ▫︎ Perhaps: "(cabalExecutable\n >> (cabalConfigDir start_dir <|> cabalFileAndWorkDir))\n Data.Functor.<&> simpleCabalCradle"
-- If we have a stack.yaml, use stack
<|> (stackExecutable >> stackConfigDir start_dir >>= stackCradle)
-- If we have a cabal file, use cabal
<|> (cabalExecutable >> cabalFileDir start_dir >>= pure . cabalCradle)
<|> (cabalExecutable >> cabalFileDir start_dir >>= pure . simpleCabalCradle)

Check warning on line 57 in ghcide/session-loader/Development/IDE/Session/Implicit.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Suggestion in inferCradleTree in module Development.IDE.Session.Implicit: Use <&> ▫︎ Found: "cabalExecutable >> cabalFileDir start_dir\n >>= pure . simpleCabalCradle" ▫︎ Perhaps: "(cabalExecutable >> cabalFileDir start_dir)\n Data.Functor.<&> simpleCabalCradle"

where
maybeItsBios = (\wdir -> (Bios (Program $ wdir </> ".hie-bios") Nothing Nothing, wdir)) <$> biosWorkDir start_dir

cabalFileAndWorkDir = cabalFileDir start_dir >>= (\dir -> cabalWorkDir dir >> pure dir)

stackCradle :: FilePath -> MaybeT IO (CradleTree a, FilePath)
stackCradle fp = do
pkgs <- stackYamlPkgs fp
pkgsWithComps <- liftIO $ catMaybes <$> mapM (nestedPkg fp) pkgs
let yaml = fp </> "stack.yaml"
pure $ (,fp) $ case pkgsWithComps of
[] -> Stack (StackType Nothing (Just yaml))
ps -> StackMulti mempty $ do
Package n cs <- ps
c <- cs
let (prefix, comp) = Implicit.stackComponent n c
pure (prefix, StackType (Just comp) (Just yaml))
cabalCradle fp = (Cabal $ CabalType Nothing Nothing, fp)
-- | Generate a stack cradle given a filepath.
--
-- Since we assume there was proof that this file belongs to a stack cradle
-- we look immediately for the relevant @*.cabal@ and @stack.yaml@ files.
-- We do not look for package.yaml, as we assume the corresponding .cabal has
-- been generated already.
--
-- We parse the @stack.yaml@ to find relevant @*.cabal@ file locations, then
-- we parse the @*.cabal@ files to generate a mapping from @hs-source-dirs@ to
-- component names.
stackCradle :: FilePath -> MaybeT IO (CradleTree a, FilePath)
stackCradle fp = do
pkgs <- stackYamlPkgs fp
pkgsWithComps <- liftIO $ catMaybes <$> mapM (nestedPkg fp) pkgs
let yaml = fp </> "stack.yaml"
pure $ (,fp) $ case pkgsWithComps of
[] -> Stack (StackType Nothing (Just yaml))
ps -> StackMulti mempty $ do
Package n cs <- ps
c <- cs
let (prefix, comp) = Implicit.stackComponent n c
pure (prefix, StackType (Just comp) (Just yaml))

-- | By default, we generate a simple cabal cradle which is equivalent to the
-- following hie.yaml:
--
-- @
-- cradle:
-- cabal:
-- @
--
-- Note, this only works reliable for reasonably modern cabal versions >= 3.2.
simpleCabalCradle :: FilePath -> (CradleTree a, FilePath)
simpleCabalCradle fp = (Cabal $ CabalType Nothing Nothing, fp)

cabalExecutable :: MaybeT IO FilePath
cabalExecutable = MaybeT $ findExecutable "cabal"
Expand Down
Loading