Skip to content

Commit 7dba16c

Browse files
committed
filesystem search stops at .git successfully
1 parent 658583d commit 7dba16c

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

src/Spago/Config.purs

Lines changed: 40 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,50 @@ 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 -> do
215+
logDebug "No project root (.git) found at: "
216+
logDebug path
217+
Just gitRoot -> do
218+
logInfo "Project root (.git) found at: "
219+
logInfo $ path <> gitRoot
220+
mSpagoYaml :: Maybe String <- map (map (\yml -> path <> yml)) $ map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./spago.yaml" ]
221+
222+
case Tuple mSpagoYaml mGitRoot of
223+
Tuple Nothing Nothing -> searchHigherPaths otherPaths
224+
Tuple Nothing (Just gitRoot) -> do
225+
-- directory containing .git assumed to be the root of the project;
226+
-- do not search up the file tree further than this
227+
logInfo $ "No Spago workspace found in any directory up to project root: " <> path <> gitRoot
228+
pure Nothing
229+
Tuple (Just spagoYaml) (Just gitRoot) -> do
230+
mWorkspace :: Maybe PrelimWorkspace <- checkForWorkspace spagoYaml
224231
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))
232+
Nothing -> do
233+
-- directory containing .git assumed to be the root of the project;
234+
-- do not search up the file tree further than this
235+
logInfo $ "No Spago workspace found in any directory up to project root: " <> path <> gitRoot
236+
pure Nothing
237+
Just ws -> pure (pure (Tuple spagoYaml ws))
238+
Tuple (Just spagoYaml) Nothing -> do
239+
mWorkspace :: Maybe PrelimWorkspace <- checkForWorkspace spagoYaml
240+
case mWorkspace of
241+
Nothing -> searchHigherPaths otherPaths
242+
Just ws -> pure (pure (Tuple spagoYaml ws))
233243

234244
-- First try to read the config in the root.
235245
-- Else, look for a workspace in parent directories.
@@ -244,7 +254,8 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
244254
, toDoc "The configuration file help can be found here https://github.com/purescript/spago#the-configuration-file"
245255
]
246256
Right config@{ yaml: { workspace: Nothing, package }, doc } -> do
247-
logInfo "Looking for Spago workspace configuration higher in the filesystem, up to project root (.git)..."
257+
logInfo "Looking for Spago workspace configuration higher in the filesystem."
258+
logInfo $ "Search limited to " <> show Paths.gitSearchDepth <> " levels, or project root (.git)..."
248259
mHigherWorkspace <- searchHigherPaths higherPaths
249260
case mHigherWorkspace of
250261
Nothing ->
@@ -255,15 +266,16 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
255266
, "See the relevant documentation here: https://github.com/purescript/spago#the-workspace"
256267
]
257268
Just (Tuple higherWorkspacePath higherWorkspace) -> do
258-
logInfo $ "Found workspace definition in " <> higherWorkspacePath
269+
logInfo "Found workspace definition in: "
270+
logInfo higherWorkspacePath
259271
-- TODO migrate workspace at higher directory?
260272
doMigrateConfig "spago.yaml" config
261273
pure { workspace: higherWorkspace, package, workspaceDoc: doc }
262274
Right config@{ yaml: { workspace: Just workspace, package }, doc } -> do
263275
doMigrateConfig "spago.yaml" config
264276
pure { workspace, package, workspaceDoc: doc }
265277

266-
logDebug "Gathering all the spago configs lower in the tree..."
278+
logDebug "Gathering all the spago configs lower in the filesystem..."
267279
otherLowerConfigPaths <- liftAff $ Glob.gitignoringGlob Paths.cwd [ "**/spago.yaml" ]
268280
unless (Array.null otherLowerConfigPaths) do
269281
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)