@@ -14,10 +14,11 @@ import Distribution.Helper (Package, projectPackages, pUnits,
14
14
unChModuleName , Ex (.. ), ProjLoc (.. ),
15
15
QueryEnv , mkQueryEnv , runQuery ,
16
16
Unit , unitInfo , uiComponents ,
17
- ChEntrypoint (.. ), UnitInfo (.. ))
17
+ ChEntrypoint (.. ), UnitInfo (.. ),
18
+ pPackageName )
18
19
import Distribution.Helper.Discover (findProjects , getDefaultDistDir )
19
20
import Data.Function ((&) )
20
- import Data.List (isPrefixOf , sortOn , find )
21
+ import Data.List (isPrefixOf , sortOn , find , intercalate )
21
22
import qualified Data.List.NonEmpty as NonEmpty
22
23
import Data.List.NonEmpty (NonEmpty )
23
24
import qualified Data.Map as Map
@@ -304,18 +305,23 @@ FilePath is part of and decide which unit to load when 'runCradle' is executed.
304
305
Thus, to find the options required to compile and load the given FilePath,
305
306
we have to do the following:
306
307
307
- 1. Identify the package that contains the FilePath (should be unique)
308
+ 1. Find the project type of the project.
308
309
Happens in 'cabalHelperCradle'
309
- 2. Find the unit that that contains the FilePath (May be non- unique)
310
+ 2. Identify the package that contains the FilePath (should be unique).
310
311
Happens in 'cabalHelperAction'
311
- 3. Find the component that exposes the FilePath (May be non-unique)
312
+ 3. Find the unit that that contains the FilePath (May be non-unique).
313
+ Happens in 'cabalHelperAction'
314
+ 4. Find the component that exposes the FilePath (May be non-unique).
312
315
Happens in 'cabalHelperAction'
313
316
314
- === Identify the package that contains the FilePath
317
+ === Find the project type of the project.
315
318
316
319
The function 'cabalHelperCradle' does the first step only.
317
320
It starts by querying Cabal-Helper to find the project's root.
318
321
See 'findCabalHelperEntryPoint' for details how this is done.
322
+
323
+ === Identify the package that contains the FilePath
324
+
319
325
Once the root of the project is defined, we query Cabal-Helper for all packages
320
326
that are defined in the project and match by the packages source directory
321
327
which package the given FilePath is most likely to be a part of.
@@ -479,43 +485,16 @@ cabalHelperCradle file = do
479
485
debugm $ " Cabal-Helper dirs: " ++ show [root, file]
480
486
let dist_dir = getDefaultDistDir proj
481
487
env <- mkQueryEnv proj dist_dir
482
- packages <- runQuery projectPackages env
483
- -- Find the package the given file may belong to.
484
- -- If it does not belong to any package, create a none-cradle.
485
- -- We might want to find a cradle without actually loading anything.
486
- -- Useful if we only want to determine a ghc version to use.
487
- case packages `findPackageFor` file of
488
- Nothing -> do
489
- debugm $ " Could not find a package for the file: " ++ file
490
- debugm
491
- " This is perfectly fine if we only want to determine the GHC version."
492
- return
493
- Cradle { cradleRootDir = root
494
- , cradleOptsProg =
495
- CradleAction { actionName = Bios. Other (projectNoneType proj)
496
- , runCradle = \ _ _ -> return CradleNone
497
- }
498
- }
499
- Just realPackage -> do
500
- debugm $ " Cabal-Helper cradle package: " ++ show realPackage
501
- -- Field `pSourceDir` often has the form `<cwd>/./plugin`
502
- -- but we only want `<cwd>/plugin`
503
- normalisedPackageLocation <- canonicalizePath $ pSourceDir realPackage
504
- debugm
505
- $ " Cabal-Helper normalisedPackageLocation: "
506
- ++ normalisedPackageLocation
507
- return
508
- Cradle { cradleRootDir = normalisedPackageLocation
509
- , cradleOptsProg =
510
- CradleAction { actionName = Bios. Other actionNameSuffix
511
- , runCradle = \ _ fp -> cabalHelperAction
512
- (Ex proj)
513
- env
514
- realPackage
515
- normalisedPackageLocation
516
- fp
517
- }
518
- }
488
+ return
489
+ Cradle { cradleRootDir = root
490
+ , cradleOptsProg =
491
+ CradleAction { actionName = Bios. Other actionNameSuffix
492
+ , runCradle = \ _ fp -> cabalHelperAction
493
+ (Ex proj)
494
+ env
495
+ fp
496
+ }
497
+ }
519
498
520
499
-- | Cradle Action to query for the ComponentOptions that are needed
521
500
-- to load the given FilePath.
@@ -526,36 +505,59 @@ cabalHelperAction :: Ex ProjLoc -- ^ Project location, can be used
526
505
-- agnostic error messages.
527
506
-> QueryEnv v -- ^ Query Env created by 'mkQueryEnv'
528
507
-- with the appropriate 'distdir'
529
- -> Package v -- ^ Package this cradle is part for.
530
- -> FilePath -- ^ Root directory of the cradle
531
- -- this action belongs to.
532
508
-> FilePath -- ^ FilePath to load, expected to be an absolute path.
533
509
-> IO (CradleLoadResult ComponentOptions )
534
- cabalHelperAction proj env package root fp = do
535
- -- Get all unit infos the given FilePath may belong to
536
- let units = pUnits package
537
- -- make the FilePath to load relative to the root of the cradle.
538
- let relativeFp = makeRelative root fp
539
- debugm $ " Relative Module FilePath: " ++ relativeFp
540
- getComponent proj env (toList units) relativeFp
541
- >>= \ case
542
- Right comp -> do
543
- let fs' = getFlags comp
544
- let fs = map (fixImportDirs root) fs'
545
- let targets = getTargets comp relativeFp
546
- let ghcOptions = removeRTS (fs ++ targets)
547
- debugm $ " Flags for \" " ++ fp ++ " \" : " ++ show ghcOptions
548
- debugm $ " Component Infos: " ++ show comp
549
- return
550
- $ CradleSuccess
551
- ComponentOptions { componentOptions = ghcOptions
552
- , componentDependencies = []
553
- }
554
- Left err -> return
555
- $ CradleFail
556
- $ CradleError
557
- (ExitFailure 2 )
558
- err
510
+ cabalHelperAction proj env fp = do
511
+ -- This builds all packages in the project.
512
+ packages <- runQuery projectPackages env
513
+ -- Find the package the given file may belong to.
514
+ -- If it does not belong to any package, fail the loading process
515
+ case packages `findPackageFor` fp of
516
+ Nothing -> do
517
+ debugm $ " Failed to find a package for: " ++ fp
518
+ return $ CradleFail $
519
+ CradleError
520
+ (ExitFailure 1 )
521
+ [ " Failed to find a package for: " ++ fp,
522
+ " No Prefix matched." ,
523
+ " Following packages were searched: "
524
+ ++ intercalate " ; "
525
+ (map
526
+ (\ p -> pPackageName p ++ " (" ++ pSourceDir p ++ " )" )
527
+ $ NonEmpty. toList packages)
528
+ ]
529
+ Just package -> do
530
+ debugm $ " Cabal-Helper cradle package: " ++ show package
531
+ -- Field `pSourceDir` often has the form `<cwd>/./plugin`
532
+ -- but we only want `<cwd>/plugin`
533
+ packageRoot <- canonicalizePath $ pSourceDir package
534
+ debugm
535
+ $ " Cabal-Helper normalisedPackageLocation: "
536
+ ++ packageRoot
537
+ -- Get all unit infos the given FilePath may belong to
538
+ let units = pUnits package
539
+ -- make the FilePath to load relative to the root of the cradle.
540
+ let relativeFp = makeRelative packageRoot fp
541
+ debugm $ " Relative Module FilePath: " ++ relativeFp
542
+ getComponent proj env (toList units) relativeFp
543
+ >>= \ case
544
+ Right comp -> do
545
+ let fs' = getFlags comp
546
+ let fs = map (fixImportDirs packageRoot) fs'
547
+ let targets = getTargets comp relativeFp
548
+ let ghcOptions = removeRTS (fs ++ targets)
549
+ debugm $ " Flags for \" " ++ fp ++ " \" : " ++ show ghcOptions
550
+ debugm $ " Component Infos: " ++ show comp
551
+ return
552
+ $ CradleSuccess
553
+ ComponentOptions { componentOptions = ghcOptions
554
+ , componentDependencies = []
555
+ }
556
+ Left err -> return
557
+ $ CradleFail
558
+ $ CradleError
559
+ (ExitFailure 2 )
560
+ err
559
561
where
560
562
-- | Strip out any ["+RTS", ..., "-RTS"] sequences in the command string list.
561
563
removeRTS :: [String ] -> [String ]
0 commit comments