Skip to content

Commit 420b1bb

Browse files
facundomingueztek
authored andcommitted
Convert FilePath to OsPath in Types/Args.hs
1 parent 22fe806 commit 420b1bb

8 files changed

Lines changed: 40 additions & 28 deletions

File tree

ghc-server/lib/GhcServer/Cache.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import GhcServer.Data.Unit (Project (..), Unit (..), UnitCache (..), UnitDepNode
2929
import GhcServer.Path (fromOsPath, toOsPath)
3030
import System.Directory (doesFileExist)
3131
import System.Directory.OsPath (createDirectoryIfMissing)
32+
import qualified System.Directory.OsPath as OsPath (doesFileExist)
3233
import qualified System.File.OsPath as OsFile
3334
import System.OsPath (OsPath, (</>))
3435
import Types.CachedDeps (CachedBuildPlan (..), CachedBuildPlans (..), CachedUnit (..), JsonFs (..))
@@ -82,7 +83,7 @@ writeUnitCache _logger unitCache depPlans buildPlanPath ghcOptions =
8283
-- | Check whether a cache exists for a unit.
8384
cacheExists :: UnitCache -> IO Bool
8485
cacheExists unitCache =
85-
doesFileExist unitCache.cachedUnitPath
86+
OsPath.doesFileExist unitCache.cachedUnitPath
8687

8788
-- | Order the transitive dependencies of a unit for loading by 'loadCachedUnits'.
8889
--
@@ -122,12 +123,12 @@ depLoadOrder depGraph root =
122123
-- @cached_unit.json@ files.
123124
buildDepPlans :: Graph UnitDepNode -> Unit -> IO CachedBuildPlans
124125
buildDepPlans depGraph unit =
125-
CachedBuildPlans . fmap plan <$> filterM (doesFileExist . (.node_payload)) (depLoadOrder depGraph selfNode)
126+
CachedBuildPlans . fmap plan <$> filterM (OsPath.doesFileExist . (.node_payload)) (depLoadOrder depGraph selfNode)
126127
where
127128
plan node =
128129
CachedBuildPlan {
129130
name = JsonFs (toUnitId (stringToUnit node.node_key.string)),
130-
build_plan = node.node_payload
131+
build_plan = fromOsPath node.node_payload
131132
}
132133

133134
selfNode = Graph.DigraphNode {
@@ -140,16 +141,16 @@ buildDepPlans depGraph unit =
140141
-- | If the unit's @cached_unit.json@ exists from a prior build, return its path.
141142
--
142143
-- This is used before compilation to let 'withGhcMakeModule' restore the home unit via 'loadHomeUnit'.
143-
loadHomeUnitCache :: UnitCache -> IO (Maybe FilePath)
144+
loadHomeUnitCache :: UnitCache -> IO (Maybe OsPath)
144145
loadHomeUnitCache unitCache =
145-
whenMaybeM (doesFileExist unitCache.cachedUnitPath) (pure unitCache.cachedUnitPath)
146+
whenMaybeM (OsPath.doesFileExist unitCache.cachedUnitPath) (pure unitCache.cachedUnitPath)
146147

147148
-- | Check whether a module's interface file (@.dyn_hi@) exists.
148149
--
149150
-- The interface file is the reliable indicator that a module was compiled in a prior build.
150151
interfaceExists :: OsPath -> UnitName -> ModuleName -> IO Bool
151152
interfaceExists outputDir name modName =
152-
doesFileExist (moduleHiPath outputDir name modName)
153+
OsPath.doesFileExist (moduleHiPath outputDir name modName)
153154

154155
-- | Compute the set of all units with cache from a prior build.
155156
cachedUnitsForProject :: Project -> IO (Set UnitName)
@@ -179,12 +180,12 @@ mkBuildCache outputDir project =
179180
-- @Left err@ on decode failure.
180181
loadCachedUnit :: UnitCache -> IO (Either String (Maybe CachedUnit))
181182
loadCachedUnit unitCache =
182-
doesFileExist unitCache.cachedUnitPath >>= \case
183+
OsPath.doesFileExist unitCache.cachedUnitPath >>= \case
183184
False -> pure (Right Nothing)
184185
True ->
185-
eitherDecodeFileStrict' unitCache.cachedUnitPath >>= \case
186+
eitherDecodeFileStrict' (fromOsPath unitCache.cachedUnitPath) >>= \case
186187
Left err ->
187-
pure (Left ("Failed to decode cached unit " ++ unitCache.cachedUnitPath ++ ": " ++ err))
188+
pure (Left ("Failed to decode cached unit " ++ fromOsPath unitCache.cachedUnitPath ++ ": " ++ err))
188189
Right cu -> pure (Right (Just cu))
189190

190191

ghc-server/lib/GhcServer/Data/Unit.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data UnitCache =
3535
-- | The cache subdirectory for this unit (@cache/unitName/@).
3636
dir :: OsPath,
3737
-- | Path to @cached_unit.json@.
38-
cachedUnitPath :: FilePath,
38+
cachedUnitPath :: OsPath,
3939
-- | Path to @unit_args@.
4040
unitArgsPath :: FilePath,
4141
-- | Path to @dep_units.json@.
@@ -46,9 +46,9 @@ data UnitCache =
4646
-- | Compute the absolute path to a module's @.dyn_hi@ file.
4747
--
4848
-- The path is @outputDir/unitId/ModuleName.dyn_hi@.
49-
moduleHiPath :: OsPath -> UnitName -> ModuleName -> FilePath
49+
moduleHiPath :: OsPath -> UnitName -> ModuleName -> OsPath
5050
moduleHiPath outputDir name modName =
51-
fromOsPath (outputDir </> toOsPath (unitIdString (unitId name)) </> toOsPath (moduleNameString modName ++ ".dyn_hi"))
51+
outputDir </> toOsPath (unitIdString (unitId name)) </> toOsPath (moduleNameString modName ++ ".dyn_hi")
5252

5353
-- | A unit discovered in the project, identified by its directory name.
5454
data Unit =
@@ -72,7 +72,7 @@ data Unit =
7272
--
7373
-- Each node's key is the 'UnitName'; its payload is the unit's own cache path
7474
-- (used when building 'CachedBuildPlans' from the transitive closure).
75-
type UnitDepNode = Graph.Node UnitName FilePath
75+
type UnitDepNode = Graph.Node UnitName OsPath
7676

7777
-- | A project is the collection of all units in the build root.
7878
data Project =
@@ -106,7 +106,7 @@ mkUnitCache :: OsPath -> UnitName -> UnitCache
106106
mkUnitCache projectRoot name =
107107
UnitCache {
108108
dir = cDir,
109-
cachedUnitPath = fromOsPath (cDir </> toOsPath "cached_unit.json"),
109+
cachedUnitPath = cDir </> toOsPath "cached_unit.json",
110110
unitArgsPath = fromOsPath (cDir </> toOsPath "unit_args"),
111111
depUnitsPath = fromOsPath (cDir </> toOsPath "dep_units.json")
112112
}

internal/src/Internal/Cache/Hpt.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import Types.BuckArgs (decodeJsonArg)
4040
import Types.CachedDeps (CachedDep (..), CachedDeps (..), CachedUnit (..), JsonFs (..))
4141
import Types.Log (Logger (..))
4242
import Types.State (WorkerState)
43+
import System.OsPath (OsPath)
44+
import System.OsPath.Extra (toOsPath)
4345

4446
#if RECENT || defined(MWB)
4547

@@ -226,7 +228,7 @@ loadHomeUnit ::
226228
DynFlags ->
227229
UnitId ->
228230
HscEnv ->
229-
FilePath ->
231+
OsPath ->
230232
IO HscEnv
231233
loadHomeUnit log stateVar dflags0 unit hsc_env0 path
232234
| hasUnit unit hsc_env0
@@ -235,7 +237,7 @@ loadHomeUnit log stateVar dflags0 unit hsc_env0 path
235237
= do
236238
cachedUnit@CachedUnit {unit_args} <- decodeJsonArg "--home-unit" path
237239
hsc_env1 <- fmap (fromMaybe hsc_env0) $ for cachedUnit.dep_units \ file -> do
238-
deps <- decodeJsonArg "--home-unit" file
240+
deps <- decodeJsonArg "--home-unit" (toOsPath file)
239241
loadCachedUnits log stateVar dflags0 deps hsc_env0
240242
dflags <- maybe (pure dflags0) (readParseGHCArgs hsc_env1 dflags0) unit_args
241243

ops/packages.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
"filepath"
219219
"ghc"
220220
"ghc-paths"
221+
"os-string"
221222
"split"
222223
"text"
223224
];

test-common/lib/Test/Cache.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ moduleCache ::
142142
SessionEnv ->
143143
ModuleKey ->
144144
Set TaskKey ->
145-
(FilePath, CachedDeps)
145+
(OsPath, CachedDeps)
146146
moduleCache env key deps =
147147
(unitPath, CachedDeps (mkCachedDep <$> depKeys))
148148
where
@@ -153,7 +153,7 @@ moduleCache env key deps =
153153
interfaces = interfacePath dc :| []
154154
}
155155

156-
unitPath = fp (env.tempDir </> cachedUnitPath key.unit)
156+
unitPath = env.tempDir </> cachedUnitPath key.unit
157157

158158
depKeys = [m | TaskCompile m <- Set.toList deps]
159159

test-common/lib/Test/Data/Project.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Data.Set (Set)
66
import Numeric.Natural (Natural)
77
import Test.Data.Scheduler (Task (..))
88
import Types.CachedDeps (CachedBuildPlans, CachedDeps)
9+
import System.OsPath (OsPath)
910

1011
-- | Error variant for modules that should fail compilation.
1112
-- The variant determines both the generated source expression and the expected GHC diagnostic code.
@@ -132,7 +133,7 @@ data UnitCache =
132133
data ModuleCache =
133134
ModuleCache {
134135
-- | The home unit's build plan and arguments.
135-
cachedUnit :: FilePath,
136+
cachedUnit :: OsPath,
136137

137138
-- | The interfaces of the dependency closure across the project.
138139
-- Decoded outside of worker handlers, so no JSON file.

types/src/Types/Args.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ data Args =
9797
ghcOptions :: [String],
9898
cachedBuildPlans :: Maybe CachedBuildPlans,
9999
cachedDeps :: Maybe CachedDeps,
100-
homeUnit :: Maybe FilePath
100+
homeUnit :: Maybe OsPath
101101
}
102102
deriving stock (Eq, Show)
103103

types/src/Types/BuckArgs.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import GHC (mkModule, mkModuleName)
1717
import GHC.Paths (libdir)
1818
import GHC.Unit (Definite (..), GenUnit (RealUnit), stringToUnitId)
1919
import System.FilePath (takeDirectory)
20-
import System.OsPath (encodeFS)
20+
import System.OsPath (OsPath, encodeUtf, encodeFS)
21+
import System.OsPath.Extra (fromOsPath, toOsPath)
2122
import qualified Types.Args
2223
import Types.Args (
2324
Args (Args),
@@ -62,7 +63,7 @@ data BuckArgs =
6263
moduleName :: Maybe String,
6364
depModules :: Maybe String,
6465
depUnits :: Maybe String,
65-
homeUnit :: Maybe String,
66+
homeUnit :: Maybe OsPath,
6667
workerTargetId :: Maybe TargetId,
6768
pluginDb :: Maybe String,
6869
env :: Map String String,
@@ -120,7 +121,7 @@ options =
120121
withArg "--buck2-packagedb-dep" \ z a -> z {buck2PackageDbDep = Just a},
121122
withArg "--dep-modules" \ z a -> z {depModules = Just a},
122123
withArg "--dep-units" \ z a -> z {depUnits = Just a},
123-
withArg "--home-unit" \ z a -> z {homeUnit = Just a},
124+
withOsPathArg "--home-unit" \ z a -> z {homeUnit = Just a},
124125
withArg "--extra-env-key" \ z a -> z {envKey = Just a},
125126
withArgErr "--extra-env-value" \ z a -> addEnv z a,
126127
withArg "--worker-target-id" \ z a -> z {workerTargetId = Just (TargetId a)},
@@ -153,12 +154,18 @@ options =
153154

154155
withArgErr name f = (name, \ argv z -> takeArg name argv (f z))
155156

157+
withOsPathArg name f = (name, \ argv z -> takeOsPathArg name argv (Right . f z))
158+
156159
takeArg name argv store = case argv of
157160
[] -> Left (name ++ " needs an argument")
158161
arg : rest -> do
159162
new <- store arg
160163
Right (rest, new)
161164

165+
takeOsPathArg name argv store = takeArg name argv $ \arg -> case encodeUtf arg of
166+
Left e -> Left ("could not encode " ++ name ++ "=" ++ arg ++ ": " ++ show e)
167+
Right p -> store p
168+
162169
parseBuckArgs :: CommandEnv -> RequestArgs -> Either String BuckArgs
163170
parseBuckArgs env =
164171
spin (emptyBuckArgs (coerce env)) . coerce
@@ -184,12 +191,12 @@ parseBuckArgs env =
184191
decodeJsonArg ::
185192
FromJSON a =>
186193
String ->
187-
String ->
194+
OsPath ->
188195
IO a
189196
decodeJsonArg desc file =
190-
eitherDecodeFileStrict' file >>= \case
197+
eitherDecodeFileStrict' (fromOsPath file) >>= \case
191198
Right a -> pure a
192-
Left err -> throwIO (userError ("Invalid JSON in file for " ++ desc ++ ": " ++ err ++ " (" ++ file ++ ")"))
199+
Left err -> throwIO (userError ("Invalid JSON in file for " ++ desc ++ ": " ++ err ++ " (" ++ fromOsPath file ++ ")"))
193200

194201
-- | @CompileHpt@ can either process a source file or pick a previously constructed @ModSummary@ from the module graph.
195202
-- In the latter case, we need both a unit ID and a module name, which is ensured here.
@@ -217,8 +224,8 @@ parseField = \case
217224

218225
toGhcArgs :: BuckArgs -> IO Args
219226
toGhcArgs args = do
220-
cachedDeps <- traverse (decodeJsonArg "--dep-modules") args.depModules
221-
cachedBuildPlans <- traverse (decodeJsonArg "--dep-units") args.depUnits
227+
cachedDeps <- traverse (decodeJsonArg "--dep-modules" . toOsPath) args.depModules
228+
cachedBuildPlans <- traverse (decodeJsonArg "--dep-units" . toOsPath) args.depUnits
222229
-- Buck specifies @-B@, which can be used to include more packages in the global package DB.
223230
-- While this is done by @ghcWithPackages@ from nixpkgs, it is likely redundant, but doesn't hurt.
224231
-- In any case, we default to @libdir@ from @ghc-paths@, which returns the directory in the distribution used by the

0 commit comments

Comments
 (0)