Skip to content

Commit de2e98c

Browse files
authored
Run Spago from a nested directory other than workspace root (#1310)
1 parent 8c735d5 commit de2e98c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+593
-235
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Other improvements:
4343
- When the `publish.location` field is missing, `spago publish` will attempt to
4444
figure out the location from Git remotes and write it back to `spago.yaml`.
4545
- Internally Spago uses stricter-typed file paths.
46+
- Spago can now be launched from a directory nested within the workspace, not
47+
just from workspace root.
4648
- `spago install` warns the user when the installed versions of packages are outside
4749
their specified dependency ranges.
4850
- `spago publish` no longer tries to validate all workspace dependencies, but

bin/src/Main.purs

+31-22
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ main = do
536536
\c -> Aff.launchAff_ case c of
537537
Cmd'SpagoCmd (SpagoCmd globalArgs@{ offline, migrateConfig } command) -> do
538538
logOptions <- mkLogOptions startingTime globalArgs
539-
rootPath <- Path.mkRoot =<< Paths.cwd
540-
runSpago { logOptions, rootPath } case command of
539+
runSpago { logOptions } case command of
541540
Sources args -> do
542541
{ env } <- mkFetchEnv
543542
{ packages: mempty
@@ -552,6 +551,7 @@ main = do
552551
void $ runSpago env (Sources.run { json: args.json })
553552
Init args@{ useSolver } -> do
554553
-- Fetch the registry here so we can select the right package set later
554+
rootPath <- Path.mkRoot =<< Paths.cwd
555555
env <- mkRegistryEnv offline <#> Record.union { rootPath }
556556
setVersion <- parseSetVersion args.setVersion
557557
void $ runSpago env $ Init.run { mode: args.mode, setVersion, useSolver }
@@ -599,7 +599,8 @@ main = do
599599
void $ runSpago publishEnv (Publish.publish {})
600600

601601
Repl args@{ selectedPackage } -> do
602-
packages <- FS.exists (rootPath </> "spago.yaml") >>= case _ of
602+
cwd <- Paths.cwd
603+
packages <- FS.exists (cwd </> "spago.yaml") >>= case _ of
603604
true -> do
604605
-- if we have a config then we assume it's a workspace, and we can run a repl in the project
605606
pure mempty -- TODO newPackages
@@ -661,13 +662,14 @@ main = do
661662
testEnv <- runSpago env (mkTestEnv args buildEnv)
662663
runSpago testEnv Test.run
663664
LsPaths args -> do
664-
runSpago { logOptions, rootPath } $ Ls.listPaths args
665+
let fetchArgs = { packages: mempty, selectedPackage: Nothing, pure: false, ensureRanges: false, testDeps: false, isRepl: false, migrateConfig, offline }
666+
{ env } <- mkFetchEnv fetchArgs
667+
runSpago env $ Ls.listPaths args
665668
LsPackages args@{ pure } -> do
666669
let fetchArgs = { packages: mempty, selectedPackage: Nothing, pure, ensureRanges: false, testDeps: false, isRepl: false, migrateConfig, offline }
667-
{ env: env@{ workspace }, fetchOpts } <- mkFetchEnv fetchArgs
670+
{ env, fetchOpts } <- mkFetchEnv fetchArgs
668671
dependencies <- runSpago env (Fetch.run fetchOpts)
669-
let lsEnv = { workspace, dependencies, logOptions, rootPath }
670-
runSpago lsEnv (Ls.listPackageSet args)
672+
runSpago (Record.union env { dependencies }) (Ls.listPackageSet args)
671673
LsDeps { selectedPackage, json, transitive, pure } -> do
672674
let fetchArgs = { packages: mempty, selectedPackage, pure, ensureRanges: false, testDeps: false, isRepl: false, migrateConfig, offline }
673675
{ env, fetchOpts } <- mkFetchEnv fetchArgs
@@ -690,13 +692,11 @@ main = do
690692
GraphModules args -> do
691693
{ env, fetchOpts } <- mkFetchEnv { packages: mempty, selectedPackage: Nothing, pure: false, ensureRanges: false, testDeps: false, isRepl: false, migrateConfig, offline }
692694
dependencies <- runSpago env (Fetch.run fetchOpts)
693-
purs <- Purs.getPurs
694-
runSpago { dependencies, logOptions, rootPath, purs, workspace: env.workspace } (Graph.graphModules args)
695+
runSpago (Record.union env { dependencies }) (Graph.graphModules args)
695696
GraphPackages args -> do
696697
{ env, fetchOpts } <- mkFetchEnv { packages: mempty, selectedPackage: Nothing, pure: false, ensureRanges: false, testDeps: false, isRepl: false, migrateConfig, offline }
697698
dependencies <- runSpago env (Fetch.run fetchOpts)
698-
purs <- Purs.getPurs
699-
runSpago { dependencies, logOptions, rootPath, purs, workspace: env.workspace } (Graph.graphPackages args)
699+
runSpago (Record.union env { dependencies }) (Graph.graphPackages args)
700700

701701
Cmd'VersionCmd v -> when v do
702702
output (OutputLines [ BuildInfo.packages."spago-bin" ])
@@ -951,7 +951,14 @@ mkReplEnv replArgs dependencies supportPackage = do
951951
, selected
952952
}
953953

954-
mkFetchEnv :: forall a b. { offline :: OnlineStatus, migrateConfig :: Boolean, isRepl :: Boolean | FetchArgsRow b } -> Spago (SpagoBaseEnv a) { env :: Fetch.FetchEnv (), fetchOpts :: Fetch.FetchOpts }
954+
mkFetchEnv
955+
:: a b
956+
. { offline :: OnlineStatus
957+
, migrateConfig :: Boolean
958+
, isRepl :: Boolean
959+
| FetchArgsRow b
960+
}
961+
-> Spago { logOptions :: LogOptions | a } { env :: Fetch.FetchEnv (), fetchOpts :: Fetch.FetchOpts }
955962
mkFetchEnv args@{ migrateConfig, offline } = do
956963
let
957964
parsePackageName p =
@@ -966,24 +973,26 @@ mkFetchEnv args@{ migrateConfig, offline } = do
966973
Left _err -> die $ "Failed to parse selected package name, was: " <> show args.selectedPackage
967974

968975
env <- mkRegistryEnv offline
969-
{ rootPath } <- ask
970-
workspace <-
971-
runSpago (Record.union env { rootPath })
972-
(Config.readWorkspace { maybeSelectedPackage, pureBuild: args.pure, migrateConfig })
976+
cwd <- Paths.cwd
977+
{ workspace, rootPath } <-
978+
runSpago env
979+
(Config.discoverWorkspace { maybeSelectedPackage, pureBuild: args.pure, migrateConfig } cwd)
980+
981+
FS.mkdirp $ rootPath </> Paths.localCachePath
982+
FS.mkdirp $ rootPath </> Paths.localCachePackagesPath
983+
logDebug $ "Workspace root path: " <> Path.quote rootPath
984+
logDebug $ "Local cache: " <> Paths.localCachePath
985+
973986
let fetchOpts = { packages: packageNames, ensureRanges: args.ensureRanges, isTest: args.testDeps, isRepl: args.isRepl }
974987
pure { fetchOpts, env: Record.union { workspace, rootPath } env }
975988

976989
mkRegistryEnv :: forall a. OnlineStatus -> Spago (SpagoBaseEnv a) (Registry.RegistryEnv ())
977990
mkRegistryEnv offline = do
978-
{ logOptions, rootPath } <- ask
991+
{ logOptions } <- ask
979992

980993
-- Take care of the caches
981994
FS.mkdirp Paths.globalCachePath
982-
FS.mkdirp $ rootPath </> Paths.localCachePath
983-
FS.mkdirp $ rootPath </> Paths.localCachePackagesPath
984-
logDebug $ "Workspace root path: " <> Path.quote rootPath
985995
logDebug $ "Global cache: " <> Path.quote Paths.globalCachePath
986-
logDebug $ "Local cache: " <> Paths.localCachePath
987996

988997
-- Make sure we have git and purs
989998
git <- Git.getGit
@@ -1004,7 +1013,7 @@ mkRegistryEnv offline = do
10041013
, db
10051014
}
10061015

1007-
mkLsEnv :: forall a. Fetch.PackageTransitiveDeps -> Spago (Fetch.FetchEnv a) Ls.LsEnv
1016+
mkLsEnv :: a. Fetch.PackageTransitiveDeps -> Spago (Fetch.FetchEnv a) (Ls.LsEnv ())
10081017
mkLsEnv dependencies = do
10091018
{ logOptions, workspace, rootPath } <- ask
10101019
selected <- case workspace.selected of

core/src/Log.purs

+3-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module Spago.Log
2222
, rightOrDie
2323
, rightOrDie_
2424
, rightOrDieWith
25-
, rightOrDieWith'
2625
, toDoc
2726
) where
2827

@@ -185,25 +184,18 @@ justOrDieWith' value msg = case value of
185184
die' msg
186185

187186
rightOrDie :: b m err x. MonadEffect m => MonadAsk (LogEnv b) m => Loggable err => Either err x -> m x
188-
rightOrDie value = rightOrDieWith value identity
187+
rightOrDie = rightOrDieWith identity
189188

190189
rightOrDie_ :: b m err x. MonadEffect m => MonadAsk (LogEnv b) m => Loggable err => Either err x -> m Unit
191190
rightOrDie_ = void <<< rightOrDie
192191

193-
rightOrDieWith :: a b m err x. MonadEffect m => MonadAsk (LogEnv b) m => Loggable a => Either err x -> (err -> a) -> m x
194-
rightOrDieWith value toMsg = case value of
192+
rightOrDieWith :: a b m err x. MonadEffect m => MonadAsk (LogEnv b) m => Loggable a => (err -> a) -> Either err x -> m x
193+
rightOrDieWith toMsg value = case value of
195194
Right a ->
196195
pure a
197196
Left err ->
198197
die $ toMsg err
199198

200-
rightOrDieWith' :: a b m err x. MonadEffect m => MonadAsk (LogEnv b) m => Loggable a => Either err x -> (err -> Array a) -> m x
201-
rightOrDieWith' value toMsg = case value of
202-
Right a ->
203-
pure a
204-
Left err ->
205-
die' $ toMsg err
206-
207199
data OutputFormat a
208200
= OutputJson (CJ.Codec a) a
209201
| OutputYaml (CJ.Codec a) a

core/src/Prelude.purs

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Data.DateTime.Instant (Instant) as Extra
2121
import Data.Either (Either(..), isLeft, isRight, either, hush) as Extra
2222
import Data.Filterable (partition, partitionMap) as Extra
2323
import Data.Foldable (foldMap, for_, foldl, and, or) as Extra
24+
import Data.FoldableWithIndex (forWithIndex_) as Extra
2425
import Data.Function (on) as Extra
2526
import Data.Generic.Rep (class Generic) as Extra
2627
import Data.Identity (Identity(..)) as Extra
@@ -47,7 +48,7 @@ import Partial.Unsafe (unsafeCrashWith)
4748
import Registry.ManifestIndex (ManifestIndex) as Extra
4849
import Registry.Types (PackageName, Version, Range, Location, License, Manifest(..), Metadata(..), Sha256) as Extra
4950
import Spago.Json (printJson, parseJson) as Extra
50-
import Spago.Log (logDebug, logError, logInfo, Docc, logSuccess, logWarn, die, die', justOrDieWith, justOrDieWith', rightOrDie, rightOrDie_, rightOrDieWith, rightOrDieWith', toDoc, indent, indent2, output, LogEnv, LogOptions, OutputFormat(..)) as Extra
51+
import Spago.Log (logDebug, logError, logInfo, Docc, logSuccess, logWarn, die, die', justOrDieWith, justOrDieWith', rightOrDie, rightOrDie_, rightOrDieWith, toDoc, indent, indent2, output, LogEnv, LogOptions, OutputFormat(..)) as Extra
5152
import Spago.Path (RawFilePath, GlobalPath, LocalPath, RootPath, class AppendPath, appendPath, (</>)) as Extra
5253
import Spago.Yaml (YamlDoc, printYaml, parseYaml) as Extra
5354

src/Spago/Command/Ls.purs

+7-5
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,24 @@ type LsPathsArgs =
5050
{ json :: Boolean
5151
}
5252

53-
type LsSetEnv =
53+
type LsSetEnv r =
5454
{ dependencies :: Fetch.PackageTransitiveDeps
5555
, logOptions :: LogOptions
5656
, workspace :: Workspace
5757
, rootPath :: RootPath
58+
| r
5859
}
5960

60-
type LsEnv =
61+
type LsEnv r =
6162
{ dependencies :: Fetch.PackageTransitiveDeps
6263
, logOptions :: LogOptions
6364
, workspace :: Workspace
6465
, selected :: WorkspacePackage
6566
, rootPath :: RootPath
67+
| r
6668
}
6769

68-
listPaths :: LsPathsArgs -> Spago { logOptions :: LogOptions, rootPath :: RootPath } Unit
70+
listPaths :: r. LsPathsArgs -> Spago { logOptions :: LogOptions, rootPath :: RootPath | r } Unit
6971
listPaths { json } = do
7072
logDebug "Running `listPaths`"
7173
{ rootPath } <- ask
@@ -90,7 +92,7 @@ listPaths { json } = do
9092

9193
-- TODO: add LICENSE field
9294

93-
listPackageSet :: LsPackagesArgs -> Spago LsSetEnv Unit
95+
listPackageSet :: r. LsPackagesArgs -> Spago (LsSetEnv r) Unit
9496
listPackageSet { json } = do
9597
logDebug "Running `listPackageSet`"
9698
{ workspace, rootPath } <- ask
@@ -102,7 +104,7 @@ listPackageSet { json } = do
102104
true -> formatPackagesJson rootPath packages
103105
false -> formatPackagesTable packages
104106

105-
listPackages :: LsDepsOpts -> Spago LsEnv Unit
107+
listPackages :: r. LsDepsOpts -> Spago (LsEnv r) Unit
106108
listPackages { transitive, json } = do
107109
logDebug "Running `listPackages`"
108110
{ dependencies, selected, rootPath } <- ask

0 commit comments

Comments
 (0)