Skip to content

Commit 1c125c9

Browse files
authored
Make the --offline flag a little more offline (#1364)
1 parent 963cc68 commit 1c125c9

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

src/Spago/Command/Fetch.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,14 @@ getGitPackageInLocalCache name package = do
519519
getPackageDependencies :: forall a. PackageName -> Package -> Spago (FetchEnv a) (Maybe (ByEnv (Map PackageName Range)))
520520
getPackageDependencies packageName package = case package of
521521
RegistryVersion v -> do
522+
-- Check if registry-index exists when offline
523+
whenM (asks _.offline <#> eq Offline) do
524+
unlessM (FS.exists Paths.registryIndexPath) do
525+
die
526+
[ "You are offline and the Registry Index is not cached locally."
527+
, "Cannot look up dependencies for " <> PackageName.print packageName <> "@" <> Version.print v
528+
, "Please connect to the internet and run 'spago install' first."
529+
]
522530
maybeManifest <- Registry.getManifestFromIndex packageName v
523531
pure $ maybeManifest <#> \(Manifest m) -> { core: m.dependencies, test: Map.empty }
524532
GitPackage p -> do

src/Spago/Git.purs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ checkout { repo, ref } = Except.runExceptT $ void $ runGit [ "checkout", ref ] (
9797

9898
fetch :: a path. Path.IsPath path => { repo :: path, remote :: String } -> Spago (GitEnv a) (Either String Unit)
9999
fetch { repo, remote } = do
100-
remoteUrl <- runGit [ "remote", "get-url", remote ] (Just $ Path.toGlobal repo) # Except.runExceptT >>= rightOrDie
101-
logInfo $ "Fetching from " <> remoteUrl
102-
Except.runExceptT $ runGit_ [ "fetch", remote, "--tags" ] (Just $ Path.toGlobal repo)
100+
{ offline } <- ask
101+
case offline of
102+
Offline -> do
103+
logDebug $ "Skipping fetch from remote '" <> remote <> "' because we are offline"
104+
pure $ Left "Cannot fetch from remote while offline."
105+
_ -> do
106+
remoteUrl <- runGit [ "remote", "get-url", remote ] (Just $ Path.toGlobal repo) # Except.runExceptT >>= rightOrDie
107+
logInfo $ "Fetching from " <> remoteUrl
108+
Except.runExceptT $ runGit_ [ "fetch", remote, "--tags" ] (Just $ Path.toGlobal repo)
103109

104110
getRefType :: a path. Path.IsPath path => { repo :: path, ref :: String } -> Spago (GitEnv a) (Either String String)
105111
getRefType { repo, ref } = Except.runExceptT $ runGit [ "cat-file", "-t", ref ] (Just $ Path.toGlobal repo)

src/Spago/Registry.purs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ getRegistryFns registryBox registryLock = do
188188

189189
-- Now that we are up to date with the Registry we init/refresh the database
190190
updatePackageSetsDb db
191+
192+
-- Check if registry directories exist when offline - the build should have already failed by now, but just in case..
193+
case offline of
194+
Offline -> do
195+
unlessM (FS.exists Paths.registryPath) $
196+
die "You are offline and the Registry is not cached locally. Please connect to the internet and run 'spago install' to cache the registry."
197+
unlessM (FS.exists Paths.registryIndexPath) $
198+
die "You are offline and the Registry Index is not cached locally. Please connect to the internet and run 'spago install' to cache the registry index."
199+
_ -> pure unit
200+
191201
pure fetchingFreshRegistry
192202

193203
-- | Update the database with the latest package sets
@@ -211,12 +221,19 @@ getRegistryFns registryBox registryLock = do
211221
-- | List all the package sets versions available in the Registry repo
212222
getAvailablePackageSets :: a. Spago (LogEnv a) (Array Version)
213223
getAvailablePackageSets = do
214-
{ success: setVersions, fail: parseFailures } <- map (partitionEithers <<< map parseSetVersion) $ FS.ls Paths.packageSetsPath
215-
216-
unless (Array.null parseFailures) do
217-
logDebug $ [ toDoc "Failed to parse some package-sets versions:" ] <> map (indent <<< toDoc <<< show) parseFailures
218-
219-
pure setVersions
224+
packageSetsExists <- FS.exists Paths.packageSetsPath
225+
if packageSetsExists then do
226+
{ success: setVersions, fail: parseFailures } <- map (partitionEithers <<< map parseSetVersion) $ FS.ls Paths.packageSetsPath
227+
228+
unless (Array.null parseFailures) do
229+
logDebug $ [ toDoc "Failed to parse some package-sets versions:" ] <> map (indent <<< toDoc <<< show) parseFailures
230+
231+
pure setVersions
232+
else do
233+
die
234+
[ "Package sets directory does not exist at " <> Path.quote Paths.packageSetsPath
235+
, "Please connect to the internet and run 'spago install' to populate the registry cache."
236+
]
220237
where
221238
parseSetVersion str = Version.parse case String.stripSuffix (Pattern ".json") str of
222239
Nothing -> str
@@ -286,7 +303,7 @@ getManifestFromIndexImpl db name version = do
286303
manifests <- map (map (\m@(Manifest m') -> Tuple m'.version m)) case maybeManifests of
287304
Right ms -> pure $ NonEmptyArray.toUnfoldable ms
288305
Left err -> do
289-
logWarn $ "Could not read package manifests from index, proceeding anyways. Error: " <> err
306+
logWarn $ "Could not read package manifests for '" <> PackageName.print name <> "' from index. Error: " <> err
290307
pure []
291308
let versions = Map.fromFoldable manifests
292309
-- and memoize it

0 commit comments

Comments
 (0)