Skip to content

Commit d2e22e9

Browse files
committed
look for workspace in parent directories only after not found in working directory
1 parent c69cd06 commit d2e22e9

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

src/Spago/Config.purs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
192192
false, true -> logWarn $ "Your " <> path <> " is using an outdated format. Run Spago with the --migrate flag to update it to the latest version."
193193
_, false -> pure unit
194194

195-
logInfo "Gathering all the spago configs higher in the tree..."
196195
let
197196
higherPaths :: List FilePath
198197
higherPaths = Array.toUnfoldable $ Paths.toGitSearchPath Paths.cwd
@@ -210,23 +209,31 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
210209
searchHigherPaths :: forall a. List FilePath -> Spago (LogEnv a) (Maybe (Tuple FilePath PrelimWorkspace))
211210
searchHigherPaths Nil = pure Nothing
212211
searchHigherPaths (path : otherPaths) = do
213-
-- TODO stop searching if .git is found, this is the root
214-
logInfo $ "Searching " <> path
212+
mGitRoot :: Maybe String <- map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./.git" ]
215213
mYaml :: Maybe String <- map (map (\yml -> path <> yml)) $ map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./spago.yaml" ]
216214
case mYaml of
217-
Nothing -> searchHigherPaths otherPaths
215+
Nothing -> case mGitRoot of
216+
Nothing -> searchHigherPaths otherPaths
217+
Just gitRoot -> do
218+
-- directory containing .git assumed to be the root of the project;
219+
-- do not search up the file tree further than this
220+
logInfo $ "No Spago workspace found in any directory up to root of project: " <> gitRoot
221+
pure Nothing
218222
Just foundSpagoYaml -> do
219223
mWorkspace :: Maybe PrelimWorkspace <- checkForWorkspace foundSpagoYaml
220224
case mWorkspace of
221-
Nothing -> searchHigherPaths otherPaths
225+
Nothing -> case mGitRoot of
226+
Nothing -> searchHigherPaths otherPaths
227+
Just gitRoot -> do
228+
-- directory containing .git assumed to be the root of the project;
229+
-- do not search up the file tree further than this
230+
logInfo $ "No Spago workspace found in any directory up to root of project: " <> gitRoot
231+
pure Nothing
222232
Just ws -> pure (pure (Tuple foundSpagoYaml ws))
223233

224-
mHigherWorkspace <- searchHigherPaths higherPaths
225-
for_ mHigherWorkspace $ \(Tuple filepath _) ->
226-
logInfo $ "Found workspace definition in " <> filepath
227-
228-
-- First try to read the config in the root. It _has_ to contain a workspace
229-
-- configuration, or we fail early.
234+
-- First try to read the config in the root.
235+
-- Else, look for a workspace in parent directories.
236+
-- Else fail.
230237
{ workspace, package: maybePackage, workspaceDoc } <- readConfig "spago.yaml" >>= case _ of
231238
Left errLines ->
232239
die
@@ -236,16 +243,22 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
236243
, Log.break
237244
, toDoc "The configuration file help can be found here https://github.com/purescript/spago#the-configuration-file"
238245
]
239-
Right config@{ yaml: { workspace: Nothing, package }, doc } -> case mHigherWorkspace of
240-
Nothing ->
241-
die
242-
[ "No workspace definition found in this spago.yaml or any spago.yaml in parent directory."
243-
, "See the relevant documentation here: https://github.com/purescript/spago#the-workspace"
244-
]
245-
Just (Tuple _ higherWorkspace) -> do
246-
-- TODO migrate workspace at higher directory?
247-
doMigrateConfig "spago.yaml" config
248-
pure { workspace: higherWorkspace, package, workspaceDoc: doc }
246+
Right config@{ yaml: { workspace: Nothing, package }, doc } -> do
247+
logInfo "Looking for Spago workspace configuration higher in the filesystem, up to project root (.git)..."
248+
mHigherWorkspace <- searchHigherPaths higherPaths
249+
case mHigherWorkspace of
250+
Nothing ->
251+
die
252+
[ "No workspace definition found in this directory"
253+
, "or in any directory up to root of project."
254+
, "Root determined by '.git' file."
255+
, "See the relevant documentation here: https://github.com/purescript/spago#the-workspace"
256+
]
257+
Just (Tuple higherWorkspacePath higherWorkspace) -> do
258+
logInfo $ "Found workspace definition in " <> higherWorkspacePath
259+
-- TODO migrate workspace at higher directory?
260+
doMigrateConfig "spago.yaml" config
261+
pure { workspace: higherWorkspace, package, workspaceDoc: doc }
249262
Right config@{ yaml: { workspace: Just workspace, package }, doc } -> do
250263
doMigrateConfig "spago.yaml" config
251264
pure { workspace, package, workspaceDoc: doc }

src/Spago/Paths.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ toGitSearchPath rootDir = reverse $ makeSearchPaths rootDir 4 where
4747
makeSearchPath wd i = joinWith "" $ cons wd $ cons "/" $ replicate i "../"
4848

4949
makeSearchPaths :: FilePath -> Int -> Array FilePath
50-
makeSearchPaths wd 0 = pure (wd <> "/")
50+
makeSearchPaths wd 0 = mempty
5151
makeSearchPaths wd i | i > 0 = cons (makeSearchPath wd i) (makeSearchPaths wd (i - 1))
5252
makeSearchPaths _ _ = mempty
5353

test/Spago/Paths.purs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import Spago.Paths (toGitSearchPath)
1111
spec :: Spec Unit
1212
spec = Spec.around withTempDir do
1313
Spec.describe "paths" do
14-
Spec.it "generate four paths to parent directories of working directory, plus working directory" \ _ -> do
14+
Spec.it "generate four paths to parent directories of working directory" \ _ -> do
1515
toGitSearchPath "~/a/b/c/d/e" `Assert.shouldEqual`
16-
[ "~/a/b/c/d/e"
17-
, "~/a/b/c/d/e/../"
16+
[ "~/a/b/c/d/e/../"
1817
, "~/a/b/c/d/e/../../"
1918
, "~/a/b/c/d/e/../../../"
2019
, "~/a/b/c/d/e/../../../../"

0 commit comments

Comments
 (0)