Skip to content

Commit cdb39bd

Browse files
committed
filesystem search stops at .git successfully
1 parent 2119d76 commit cdb39bd

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

src/Spago/Config.purs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ type PrelimWorkspace =
180180
-- | packages to be integrated in the package set
181181
readWorkspace :: a. ReadWorkspaceOptions -> Spago (Registry.RegistryEnv a) Workspace
182182
readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
183-
logInfo "Reading Spago workspace configuration..."
183+
logInfo "Reading spago.yaml..."
184184

185185
let
186186
doMigrateConfig :: FilePath -> _ -> Spago (Registry.RegistryEnv _) Unit
@@ -196,40 +196,46 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
196196
higherPaths :: List FilePath
197197
higherPaths = Array.toUnfoldable $ Paths.toGitSearchPath Paths.cwd
198198

199-
checkForWorkspace :: forall a. FilePath
200-
-> Spago (LogEnv a) (Maybe PrelimWorkspace)
199+
checkForWorkspace :: forall b. FilePath
200+
-> Spago (LogEnv b) (Maybe PrelimWorkspace)
201201
checkForWorkspace config = do
202-
logInfo $ "Checking for workspace: " <> config
202+
logDebug $ "Checking for workspace: " <> config
203203
result <- map (map (\y -> y.yaml)) $ readConfig config
204204
case result of
205205
Left _ -> pure Nothing
206206
Right { workspace: Nothing } -> pure Nothing
207207
Right { workspace: Just ws } -> pure (Just ws)
208208

209-
searchHigherPaths :: forall a. List FilePath -> Spago (LogEnv a) (Maybe (Tuple FilePath PrelimWorkspace))
209+
searchHigherPaths :: forall c. List FilePath -> Spago (LogEnv c) (Maybe (Tuple FilePath PrelimWorkspace))
210210
searchHigherPaths Nil = pure Nothing
211211
searchHigherPaths (path : otherPaths) = do
212-
mGitRoot :: Maybe String <- map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./.git" ]
213-
mYaml :: Maybe String <- map (map (\yml -> path <> yml)) $ map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./spago.yaml" ]
214-
case mYaml of
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
222-
Just foundSpagoYaml -> do
223-
mWorkspace :: Maybe PrelimWorkspace <- checkForWorkspace foundSpagoYaml
212+
mGitRoot :: Maybe String <- map Array.head $ liftAff $ Glob.findGitGlob path
213+
case mGitRoot of
214+
Nothing -> logDebug $ "No project root (.git) found at: " <> path
215+
Just gitRoot -> logInfo $ "Project root (.git) found at: " <> path <> gitRoot
216+
mSpagoYaml :: Maybe String <- map (map (\yml -> path <> yml)) $ map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./spago.yaml" ]
217+
218+
case Tuple mSpagoYaml mGitRoot of
219+
Tuple Nothing Nothing -> searchHigherPaths otherPaths
220+
Tuple Nothing (Just gitRoot) -> do
221+
-- directory containing .git assumed to be the root of the project;
222+
-- do not search up the file tree further than this
223+
logInfo $ "No Spago workspace found in any directory up to project root: " <> path <> gitRoot
224+
pure Nothing
225+
Tuple (Just spagoYaml) (Just gitRoot) -> do
226+
mWorkspace :: Maybe PrelimWorkspace <- checkForWorkspace spagoYaml
224227
case mWorkspace of
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
232-
Just ws -> pure (pure (Tuple foundSpagoYaml ws))
228+
Nothing -> do
229+
-- directory containing .git assumed to be the root of the project;
230+
-- do not search up the file tree further than this
231+
logInfo $ "No Spago workspace found in any directory up to project root: " <> path <> gitRoot
232+
pure Nothing
233+
Just ws -> pure (pure (Tuple spagoYaml ws))
234+
Tuple (Just spagoYaml) Nothing -> do
235+
mWorkspace :: Maybe PrelimWorkspace <- checkForWorkspace spagoYaml
236+
case mWorkspace of
237+
Nothing -> searchHigherPaths otherPaths
238+
Just ws -> pure (pure (Tuple spagoYaml ws))
233239

234240
-- First try to read the config in the root.
235241
-- Else, look for a workspace in parent directories.
@@ -244,7 +250,9 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
244250
, toDoc "The configuration file help can be found here https://github.com/purescript/spago#the-configuration-file"
245251
]
246252
Right config@{ yaml: { workspace: Nothing, package }, doc } -> do
247-
logInfo "Looking for Spago workspace configuration higher in the filesystem, up to project root (.git)..."
253+
logInfo
254+
$ "Looking for Spago workspace configuration higher in the filesystem. Search limited to "
255+
<> show Paths.gitSearchDepth <> " levels, or project root (.git)..."
248256
mHigherWorkspace <- searchHigherPaths higherPaths
249257
case mHigherWorkspace of
250258
Nothing ->
@@ -255,15 +263,15 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
255263
, "See the relevant documentation here: https://github.com/purescript/spago#the-workspace"
256264
]
257265
Just (Tuple higherWorkspacePath higherWorkspace) -> do
258-
logInfo $ "Found workspace definition in " <> higherWorkspacePath
266+
logInfo $ "Found workspace definition in: " <> higherWorkspacePath
259267
-- TODO migrate workspace at higher directory?
260268
doMigrateConfig "spago.yaml" config
261269
pure { workspace: higherWorkspace, package, workspaceDoc: doc }
262270
Right config@{ yaml: { workspace: Just workspace, package }, doc } -> do
263271
doMigrateConfig "spago.yaml" config
264272
pure { workspace, package, workspaceDoc: doc }
265273

266-
logDebug "Gathering all the spago configs lower in the tree..."
274+
logDebug "Gathering all the spago configs lower in the filesystem..."
267275
otherLowerConfigPaths <- liftAff $ Glob.gitignoringGlob Paths.cwd [ "**/spago.yaml" ]
268276
unless (Array.null otherLowerConfigPaths) do
269277
logDebug $ [ toDoc "Found packages at these lower paths:", Log.indent $ Log.lines (map toDoc otherLowerConfigPaths) ]

src/Spago/Glob.purs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-- | All of this code (and the FFI file) is a series of attempts to make globbing
44
-- | reasonably performant while still supporting all of our usecases, like ignoring
55
-- | files based on `.gitignore` files.
6-
module Spago.Glob (gitignoringGlob) where
6+
module Spago.Glob (gitignoringGlob, findGitGlob) where
77

88
import Spago.Prelude
99

@@ -207,3 +207,7 @@ fsWalk cwd ignorePatterns includePatterns = Aff.makeAff \cb -> do
207207
gitignoringGlob :: String -> Array String -> Aff (Array String)
208208
gitignoringGlob dir patterns = map (withForwardSlashes <<< Path.relative dir <<< _.path)
209209
<$> fsWalk dir [ ".git" ] patterns
210+
211+
findGitGlob :: String -> Aff (Array String)
212+
findGitGlob dir = map (withForwardSlashes <<< Path.relative dir <<< _.path)
213+
<$> fsWalk dir mempty [ "./.git" ]

src/Spago/Paths.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ toLocalCachePackagesPath :: FilePath -> FilePath
4141
toLocalCachePackagesPath rootDir = Path.concat [ toLocalCachePath rootDir, "p" ]
4242

4343
-- search maximum 4 levels up the tree to find all other `spago.yaml`, which may contain workspace definition
44+
gitSearchDepth :: Int
45+
gitSearchDepth = 4
4446
toGitSearchPath :: FilePath -> Array FilePath
45-
toGitSearchPath rootDir = reverse $ makeSearchPaths rootDir 4 where
47+
toGitSearchPath rootDir = reverse $ makeSearchPaths rootDir gitSearchDepth where
4648
makeSearchPath :: FilePath -> Int -> FilePath
4749
makeSearchPath wd i = joinWith "" $ cons wd $ cons "/" $ replicate i "../"
4850

0 commit comments

Comments
 (0)