Skip to content

Commit 171b93f

Browse files
committed
Do not attempt to load subpackages nested under malformed config
1 parent 2b79ca1 commit 171b93f

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

src/Spago/Config.purs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,13 @@ discoverWorkspace options cwd = do
182182
logInfo "Reading Spago workspace configuration..."
183183
logDebug $ "Discovering nearest workspace " <> spagoYaml <> " starting at " <> Path.quote cwd
184184

185-
{ workspace, rootPath } /\ { loadedPackages, closestPackage } <-
185+
{ workspace, rootPath } /\ upTree@{ closestPackage } <-
186186
State.runStateT (walkDirectoriesUpFrom cwd)
187-
{ loadedPackages: Map.empty, otherWorkspaceRoots: [], misnamedConfigs: [], closestPackage: Nothing }
187+
{ loadedPackages: Map.empty, misnamedConfigs: [], closestPackage: Nothing }
188+
189+
{ loadedPackages } <-
190+
State.execStateT (loadSubprojectConfigs rootPath)
191+
{ loadedPackages: upTree.loadedPackages, blockedSubtrees: [] }
188192

189193
migrateConfigsWhereNeeded rootPath loadedPackages
190194

@@ -229,6 +233,7 @@ discoverWorkspace options cwd = do
229233
}
230234
}
231235
where
236+
readConfig' :: s. _ -> State.StateT s _ _
232237
readConfig' = State.lift <<< readConfig
233238

234239
walkDirectoriesUpFrom dir = do
@@ -249,7 +254,6 @@ discoverWorkspace options cwd = do
249254
Just { doc, yaml: { workspace: Just workspace, package } } -> do
250255
-- Finally, found the "workspace" config!
251256
rootPath <- Path.mkRoot dir
252-
loadSubprojectConfigs rootPath
253257
pure { workspace: { config: workspace, doc, rootPackage: package }, rootPath }
254258
_ -> do
255259
-- No workspace in this directory => recur to parent directory (unless it's already root)
@@ -277,19 +281,20 @@ discoverWorkspace options cwd = do
277281
let
278282
configFile = dir </> spagoYaml
279283
alreadyLoaded = st.loadedPackages # Map.member configFile
280-
anotherParentWorkspace = st.otherWorkspaceRoots # Array.find (_ `Path.isPrefixOf` dir)
281-
case alreadyLoaded, anotherParentWorkspace of
284+
blockedSubtree = st.blockedSubtrees # Array.find (_ `Path.isPrefixOf` dir)
285+
case alreadyLoaded, blockedSubtree of
282286
true, _ ->
283287
pure unit
284-
_, Just ws -> do
285-
logDebug $ "Not trying to load " <> Path.quote configFile <> " because it belongs to a different workspace at " <> Path.quote ws
288+
_, Just b -> do
289+
logDebug $ "Not trying to load " <> Path.quote configFile <> " because it is nested under " <> Path.quote b
286290
pure unit
287291
false, Nothing ->
288292
readConfig' configFile >>= case _ of
289-
Left _ ->
290-
logWarn $ "Failed to read config at " <> Path.quote configFile
293+
Left _ -> do
294+
logWarn [ "Failed to read config at " <> Path.quote configFile, "Skipping it and all its subprojects" ]
295+
State.modify_ \s -> s { blockedSubtrees = Array.cons dir s.blockedSubtrees }
291296
Right { yaml: { workspace: Just _ } } ->
292-
State.modify_ \s -> s { otherWorkspaceRoots = Array.cons dir s.otherWorkspaceRoots }
297+
State.modify_ \s -> s { blockedSubtrees = Array.cons dir s.blockedSubtrees }
293298
Right config@{ yaml: { package: Just package } } -> do
294299
logDebug $ "Loaded a subproject config at " <> Path.quote configFile
295300
State.modify_ \s -> s { loadedPackages = Map.insert dir { package, config } s.loadedPackages }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package:
2+
name: c
3+
dependencies: []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package:
2+
title: b # `title` is not a valid field, this config will error out on parsing.
3+
dependencies: []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package:
2+
name: d
3+
dependencies: []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package:
2+
name: a
3+
dependencies: []
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Reading Spago workspace configuration...
2+
‼ Failed to read config at "<test-dir>/a/b/spago.yaml"
3+
Skipping it and all its subprojects
4+
5+
✘ Selected package bogus was not found in the local packages.
6+
All available packages:
7+
a
8+
d
9+
root
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package:
2+
name: root
3+
dependencies: []
4+
workspace:
5+
packageSet:
6+
registry: 0.0.1

test/Spago/Config.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Registry.Version as Version
1111
import Spago.Core.Config (SetAddress(..))
1212
import Spago.Core.Config as C
1313
import Spago.FS as FS
14+
import Spago.Path as Path
1415
import Spago.Paths as Paths
1516
import Spago.Yaml as Yaml
1617
import Test.Spec (Spec)
@@ -118,6 +119,20 @@ spec =
118119
Paths.chdir $ testCwd </> "a" </> "b" </> "d"
119120
spago [ "build" ] >>= shouldBeFailureErr' (fixture "config/misnamed-configs/from-d.txt")
120121

122+
Spec.it "warns about a malformed config, but stops parsing down the tree" \{ spago, fixture, testCwd } -> do
123+
FS.copyTree { src: fixture "config/malformed-configs", dst: testCwd }
124+
125+
-- Running with "-p bogus" to get Spago to list all available
126+
-- packages. Packages `b` and `c` shouldn't be in that list because
127+
-- b's config is malformatted, so Spago should warn about it and stop
128+
-- loading configs down the tree from `b`, thus skipping `c`.
129+
spago [ "build", "-p", "bogus" ] >>= checkOutputs'
130+
{ result: isLeft
131+
, stdoutFile: Nothing
132+
, stderrFile: Just (fixture "config/malformed-configs/from-root.txt")
133+
, sanitize: String.trim >>> String.replaceAll (String.Pattern $ Path.toRaw testCwd) (String.Replacement "<test-dir>")
134+
}
135+
121136
where
122137
shouldFailWith result expectedError =
123138
case result of

0 commit comments

Comments
 (0)