Skip to content

Commit f7611a2

Browse files
committed
remove makeAbsolute
1 parent 542ea26 commit f7611a2

File tree

8 files changed

+55
-35
lines changed

8 files changed

+55
-35
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,13 @@ getHieDbLoc dir = do
437437
loadSession :: Recorder (WithPriority Log) -> FilePath -> IO (Action IdeGhcSession)
438438
loadSession recorder = loadSessionWithOptions recorder def
439439

440+
toAbsolute :: FilePath -> FilePath -> FilePath
441+
toAbsolute dir file
442+
| isAbsolute file = file
443+
| otherwise = dir </> file
440444
loadSessionWithOptions :: Recorder (WithPriority Log) -> SessionLoadingOptions -> FilePath -> IO (Action IdeGhcSession)
441445
loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
446+
let toAbsolutePath = toAbsolute dir
442447
cradle_files <- newIORef []
443448
-- Mapping from hie.yaml file to HscEnv, one per hie.yaml file
444449
hscEnvs <- newVar Map.empty :: IO (Var HieMap)
@@ -459,7 +464,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
459464
-- Sometimes we get C:, sometimes we get c:, and sometimes we get a relative path
460465
-- try and normalise that
461466
-- e.g. see https://github.com/haskell/ghcide/issues/126
462-
res' <- traverse makeAbsolute res
467+
let res' = toAbsolutePath <$> res
463468
return $ normalise <$> res'
464469

465470
dummyAs <- async $ return (error "Uninitialised")
@@ -521,7 +526,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
521526
packageSetup (hieYaml, cfp, opts, libDir) = do
522527
-- Parse DynFlags for the newly discovered component
523528
hscEnv <- emptyHscEnv ideNc libDir
524-
newTargetDfs <- evalGhcEnv hscEnv $ setOptions cfp opts (hsc_dflags hscEnv)
529+
newTargetDfs <- evalGhcEnv hscEnv $ setOptions cfp opts (hsc_dflags hscEnv) dir
525530
let deps = componentDependencies opts ++ maybeToList hieYaml
526531
dep_info <- getDependencyInfo deps
527532
-- Now lookup to see whether we are combining with an existing HscEnv
@@ -588,7 +593,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
588593
-- HscEnv but set the active component accordingly
589594
hscEnv <- emptyHscEnv ideNc _libDir
590595
let new_cache = newComponentCache recorder optExtensions hieYaml _cfp hscEnv
591-
all_target_details <- new_cache old_deps new_deps
596+
all_target_details <- new_cache old_deps new_deps dir
592597

593598
this_dep_info <- getDependencyInfo $ maybeToList hieYaml
594599
let (all_targets, this_flags_map, this_options)
@@ -713,7 +718,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
713718
modifyVar_ hscEnvs (const (return Map.empty))
714719

715720
v <- Map.findWithDefault HM.empty hieYaml <$> readVar fileToFlags
716-
cfp <- makeAbsolute file
721+
let cfp = toAbsolutePath file
717722
case HM.lookup (toNormalizedFilePath' cfp) v of
718723
Just (opts, old_di) -> do
719724
deps_ok <- checkDependencyInfo old_di
@@ -735,7 +740,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
735740
-- before attempting to do so.
736741
let getOptions :: FilePath -> IO (IdeResult HscEnvEq, [FilePath])
737742
getOptions file = do
738-
ncfp <- toNormalizedFilePath' <$> makeAbsolute file
743+
let ncfp = toNormalizedFilePath' (toAbsolutePath file)
739744
cachedHieYamlLocation <- HM.lookup ncfp <$> readVar filesMap
740745
hieYaml <- cradleLoc file
741746
sessionOpts (join cachedHieYamlLocation <|> hieYaml, file) `Safe.catch` \e ->
@@ -747,7 +752,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} dir = do
747752
void $ wait as
748753
asyncRes <- async $ getOptions file
749754
return (asyncRes, wait asyncRes)
750-
pure opts
755+
pure $ (fmap . fmap) toAbsolutePath opts
751756

752757
-- | Run the specific cradle on a specific FilePath via hie-bios.
753758
-- This then builds dependencies or whatever based on the cradle, gets the
@@ -814,19 +819,20 @@ fromTargetId :: [FilePath] -- ^ import paths
814819
-> TargetId
815820
-> IdeResult HscEnvEq
816821
-> DependencyInfo
822+
-> FilePath
817823
-> IO [TargetDetails]
818824
-- For a target module we consider all the import paths
819-
fromTargetId is exts (GHC.TargetModule modName) env dep = do
825+
fromTargetId is exts (GHC.TargetModule modName) env dep dir = do
820826
let fps = [i </> moduleNameSlashes modName -<.> ext <> boot
821827
| ext <- exts
822828
, i <- is
823829
, boot <- ["", "-boot"]
824830
]
825-
locs <- mapM (fmap toNormalizedFilePath' . makeAbsolute) fps
831+
let locs = fmap (toNormalizedFilePath' . toAbsolute dir) fps
826832
return [TargetDetails (TargetModule modName) env dep locs]
827833
-- For a 'TargetFile' we consider all the possible module names
828-
fromTargetId _ _ (GHC.TargetFile f _) env deps = do
829-
nf <- toNormalizedFilePath' <$> makeAbsolute f
834+
fromTargetId _ _ (GHC.TargetFile f _) env deps dir = do
835+
let nf = toNormalizedFilePath' $ toAbsolute dir f
830836
let other
831837
| "-boot" `isSuffixOf` f = toNormalizedFilePath' (L.dropEnd 5 $ fromNormalizedFilePath nf)
832838
| otherwise = toNormalizedFilePath' (fromNormalizedFilePath nf ++ "-boot")
@@ -915,8 +921,9 @@ newComponentCache
915921
-> HscEnv -- ^ An empty HscEnv
916922
-> [ComponentInfo] -- ^ New components to be loaded
917923
-> [ComponentInfo] -- ^ old, already existing components
924+
-> FilePath -- ^ root dir
918925
-> IO [ [TargetDetails] ]
919-
newComponentCache recorder exts cradlePath _cfp hsc_env old_cis new_cis = do
926+
newComponentCache recorder exts cradlePath _cfp hsc_env old_cis new_cis dir = do
920927
let cis = Map.unionWith unionCIs (mkMap new_cis) (mkMap old_cis)
921928
-- When we have multiple components with the same uid,
922929
-- prefer the new one over the old.
@@ -961,7 +968,7 @@ newComponentCache recorder exts cradlePath _cfp hsc_env old_cis new_cis = do
961968

962969
forM (Map.elems cis) $ \ci -> do
963970
let df = componentDynFlags ci
964-
let createHscEnvEq = maybe newHscEnvEqPreserveImportPaths newHscEnvEq cradlePath
971+
let createHscEnvEq = maybe newHscEnvEqPreserveImportPaths (newHscEnvEq dir) cradlePath
965972
thisEnv <- do
966973
#if MIN_VERSION_ghc(9,3,0)
967974
-- In GHC 9.4 we have multi component support, and we have initialised all the units
@@ -986,7 +993,7 @@ newComponentCache recorder exts cradlePath _cfp hsc_env old_cis new_cis = do
986993
logWith recorder Debug $ LogNewComponentCache (targetEnv, targetDepends)
987994
evaluate $ liftRnf rwhnf $ componentTargets ci
988995

989-
let mk t = fromTargetId (importPaths df) exts (targetId t) targetEnv targetDepends
996+
let mk t = fromTargetId (importPaths df) exts (targetId t) targetEnv targetDepends dir
990997
ctargets <- concatMapM mk (componentTargets ci)
991998

992999
return (L.nubOrdOn targetTarget ctargets)
@@ -1171,8 +1178,8 @@ addUnit unit_str = liftEwM $ do
11711178
putCmdLineState (unit_str : units)
11721179

11731180
-- | Throws if package flags are unsatisfiable
1174-
setOptions :: GhcMonad m => NormalizedFilePath -> ComponentOptions -> DynFlags -> m (NonEmpty (DynFlags, [GHC.Target]))
1175-
setOptions cfp (ComponentOptions theOpts compRoot _) dflags = do
1181+
setOptions :: GhcMonad m => NormalizedFilePath -> ComponentOptions -> DynFlags -> FilePath -> m (NonEmpty (DynFlags, [GHC.Target]))
1182+
setOptions cfp (ComponentOptions theOpts compRoot _) dflags dir = do
11761183
((theOpts',_errs,_warns),units) <- processCmdLineP unit_flags [] (map noLoc theOpts)
11771184
case NE.nonEmpty units of
11781185
Just us -> initMulti us
@@ -1195,7 +1202,7 @@ setOptions cfp (ComponentOptions theOpts compRoot _) dflags = do
11951202
--
11961203
-- If we don't end up with a target for the current file in the end, then
11971204
-- we will report it as an error for that file
1198-
abs_fp <- liftIO $ makeAbsolute (fromNormalizedFilePath cfp)
1205+
let abs_fp = toAbsolute dir (fromNormalizedFilePath cfp)
11991206
let special_target = Compat.mkSimpleTarget df abs_fp
12001207
pure $ (df, special_target : targets) :| []
12011208
where

ghcide/src/Development/IDE/Core/Rules.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ import Language.LSP.Server (LspT)
159159
import qualified Language.LSP.Server as LSP
160160
import Language.LSP.VFS
161161
import Prelude hiding (mod)
162-
import System.Directory (doesFileExist,
163-
makeAbsolute)
162+
import System.Directory (doesFileExist)
164163
import System.Info.Extra (isWindows)
165164

166165

@@ -719,8 +718,7 @@ loadGhcSession recorder ghcSessionDepsConfig = do
719718
-- add the deps to the Shake graph
720719
let addDependency fp = do
721720
-- VSCode uses absolute paths in its filewatch notifications
722-
afp <- liftIO $ makeAbsolute fp
723-
let nfp = toNormalizedFilePath' afp
721+
let nfp = toNormalizedFilePath' fp
724722
itExists <- getFileExists nfp
725723
when itExists $ void $ do
726724
use_ GetModificationTime nfp
@@ -848,7 +846,7 @@ getModIfaceFromDiskAndIndexRule recorder =
848846
hie_loc = Compat.ml_hie_file $ ms_location ms
849847
fileHash <- liftIO $ Util.getFileHash hie_loc
850848
mrow <- liftIO $ withHieDb (\hieDb -> HieDb.lookupHieFileFromSource hieDb (fromNormalizedFilePath f))
851-
hie_loc' <- liftIO $ traverse (makeAbsolute . HieDb.hieModuleHieFile) mrow
849+
let hie_loc' = HieDb.hieModuleHieFile <$> mrow
852850
case mrow of
853851
Just row
854852
| fileHash == HieDb.modInfoHash (HieDb.hieModInfo row)

ghcide/src/Development/IDE/Core/Service.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ initialise :: Recorder (WithPriority Log)
6767
-> WithHieDb
6868
-> IndexQueue
6969
-> Monitoring
70+
-> FilePath
7071
-> IO IdeState
71-
initialise recorder defaultConfig plugins mainRule lspEnv debouncer options withHieDb hiedbChan metrics = do
72+
initialise recorder defaultConfig plugins mainRule lspEnv debouncer options withHieDb hiedbChan metrics rootDir = do
7273
shakeProfiling <- do
7374
let fromConf = optShakeProfiling options
7475
fromEnv <- lookupEnv "GHCIDE_BUILD_PROFILING"
@@ -86,11 +87,12 @@ initialise recorder defaultConfig plugins mainRule lspEnv debouncer options with
8687
hiedbChan
8788
(optShakeOptions options)
8889
metrics
89-
$ do
90+
(do
9091
addIdeGlobal $ GlobalIdeOptions options
9192
ofInterestRules (cmapWithPrio LogOfInterest recorder)
9293
fileExistsRules (cmapWithPrio LogFileExists recorder) lspEnv
93-
mainRule
94+
mainRule)
95+
rootDir
9496

9597
-- | Shutdown the Compiler Service.
9698
shutdown :: IdeState -> IO ()

ghcide/src/Development/IDE/Core/Shake.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ data IdeState = IdeState
535535
,shakeExtras :: ShakeExtras
536536
,shakeDatabaseProfile :: ShakeDatabase -> IO (Maybe FilePath)
537537
,stopMonitoring :: IO ()
538+
,rootDir :: FilePath
538539
}
539540

540541

@@ -623,11 +624,12 @@ shakeOpen :: Recorder (WithPriority Log)
623624
-> ShakeOptions
624625
-> Monitoring
625626
-> Rules ()
627+
-> FilePath
626628
-> IO IdeState
627629
shakeOpen recorder lspEnv defaultConfig idePlugins debouncer
628630
shakeProfileDir (IdeReportProgress reportProgress)
629631
ideTesting@(IdeTesting testing)
630-
withHieDb indexQueue opts monitoring rules = mdo
632+
withHieDb indexQueue opts monitoring rules rootDir = mdo
631633

632634
#if MIN_VERSION_ghc(9,3,0)
633635
ideNc <- initNameCache 'r' knownKeyNames

ghcide/src/Development/IDE/Main.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
353353
withHieDb
354354
hieChan
355355
monitoring
356+
rootPath
356357
putMVar ideStateVar ide
357358
pure ide
358359

@@ -404,7 +405,7 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
404405
, optCheckProject = pure False
405406
, optModifyDynFlags = optModifyDynFlags def_options <> pluginModifyDynflags plugins
406407
}
407-
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins rules Nothing debouncer ideOptions hiedb hieChan mempty
408+
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins rules Nothing debouncer ideOptions hiedb hieChan mempty dir
408409
shakeSessionInit (cmapWithPrio LogShake recorder) ide
409410
registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing)
410411

@@ -442,7 +443,7 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
442443
, optCheckProject = pure False
443444
, optModifyDynFlags = optModifyDynFlags def_options <> pluginModifyDynflags plugins
444445
}
445-
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins rules Nothing debouncer ideOptions hiedb hieChan mempty
446+
ide <- initialise (cmapWithPrio LogService recorder) argsDefaultHlsConfig argsHlsPlugins rules Nothing debouncer ideOptions hiedb hieChan mempty root
446447
shakeSessionInit (cmapWithPrio LogShake recorder) ide
447448
registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing)
448449
c ide

ghcide/src/Development/IDE/Types/HscEnvEq.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import Development.IDE.GHC.Util (lookupPackageConfig)
2929
import Development.IDE.Graph.Classes
3030
import Development.IDE.Types.Exports (ExportsMap, createExportsMap)
3131
import OpenTelemetry.Eventlog (withSpan)
32-
import System.Directory (makeAbsolute)
3332
import System.FilePath
3433

3534
-- | An 'HscEnv' with equality. Two values are considered equal
@@ -58,15 +57,19 @@ updateHscEnvEq oldHscEnvEq newHscEnv = do
5857
let update newUnique = oldHscEnvEq { envUnique = newUnique, hscEnv = newHscEnv }
5958
update <$> Unique.newUnique
6059

60+
toAbsolute :: FilePath -> FilePath -> FilePath
61+
toAbsolute root path
62+
| isAbsolute path = path
63+
| otherwise = root </> path
6164
-- | Wrap an 'HscEnv' into an 'HscEnvEq'.
62-
newHscEnvEq :: FilePath -> HscEnv -> [(UnitId, DynFlags)] -> IO HscEnvEq
63-
newHscEnvEq cradlePath hscEnv0 deps = do
65+
newHscEnvEq :: FilePath -> FilePath -> HscEnv -> [(UnitId, DynFlags)] -> IO HscEnvEq
66+
newHscEnvEq root cradlePath hscEnv0 deps = do
6467
let relativeToCradle = (takeDirectory cradlePath </>)
6568
hscEnv = removeImportPaths hscEnv0
6669

6770
-- Make Absolute since targets are also absolute
6871
importPathsCanon <-
69-
mapM makeAbsolute $ relativeToCradle <$> importPaths (hsc_dflags hscEnv0)
72+
mapM (return . toAbsolute root) $ relativeToCradle <$> importPaths (hsc_dflags hscEnv0)
7073

7174
newHscEnvEqWithImportPaths (Just $ Set.fromList importPathsCanon) hscEnv deps
7275

plugins/hls-module-name-plugin/src/Ide/Plugin/ModuleName.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import Language.LSP.Protocol.Message
5858
import Language.LSP.Protocol.Types
5959
import Language.LSP.Server
6060
import Language.LSP.VFS (virtualFileText)
61-
import System.Directory (makeAbsolute)
6261
import System.FilePath (dropExtension, normalise,
6362
pathSeparator,
6463
splitDirectories,
@@ -133,6 +132,10 @@ action recorder state uri = do
133132
in pure [Replace uri (Range (Position 0 0) (Position 0 0)) code code]
134133
_ -> pure []
135134

135+
toAbsolute :: FilePath -> FilePath -> FilePath
136+
toAbsolute root path
137+
| isAbsolute path = path
138+
| otherwise = root </> path
136139
-- | Possible module names, as derived by the position of the module in the
137140
-- source directories. There may be more than one possible name, if the source
138141
-- directories are nested inside each other.
@@ -150,7 +153,7 @@ pathModuleNames recorder state normFilePath filePath
150153
let paths = map (normalise . (<> pure pathSeparator)) srcPaths
151154
logWith recorder Debug (NormalisedPaths paths)
152155

153-
mdlPath <- liftIO $ makeAbsolute filePath
156+
mdlPath <- liftIO $ (toAbsolute $ rootDir state) filePath
154157
logWith recorder Debug (AbsoluteFilePath mdlPath)
155158

156159
let suffixes = mapMaybe (`stripPrefix` mdlPath) paths

plugins/hls-retrie-plugin/src/Ide/Plugin/Retrie.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ import Retrie.SYB (everything, extQ,
129129
listify, mkQ)
130130
import Retrie.Types
131131
import Retrie.Universe (Universe)
132-
import System.Directory (makeAbsolute)
133132

134133
#if MIN_VERSION_ghc(9,3,0)
135134
import GHC.Types.PkgQual
@@ -760,9 +759,14 @@ reuseParsedModule state f = do
760759
(fixities, pm') <- fixFixities state f (fixAnns pm)
761760
return (fixities, pm')
762761

762+
toAbsolute :: FilePath -> FilePath -> FilePath
763+
toAbsolute dir file
764+
| isAbsolute file = file
765+
| otherwise = dir </> file
766+
763767
getCPPmodule :: Recorder (WithPriority Log) -> IdeState -> HscEnv -> FilePath -> IO (FixityEnv, CPP AnnotatedModule)
764768
getCPPmodule recorder state session t = do
765-
nt <- toNormalizedFilePath' <$> makeAbsolute t
769+
nt <- toNormalizedFilePath' <$> (toAbsolute $ rootDir state) t
766770
let getParsedModule f contents = do
767771
modSummary <- msrModSummary <$>
768772
useOrFail state "Retrie.GetModSummary" (CallRetrieInternalError "file not found") GetModSummary nt

0 commit comments

Comments
 (0)