Skip to content

Commit 6d8e02f

Browse files
Detect yml instead of yaml (purescript#1155)
--------- Co-authored-by: Jordan Martinez <[email protected]>
1 parent 9fc429a commit 6d8e02f

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

core/src/Config.purs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import Data.Either as Either
4747
import Data.List as List
4848
import Data.Map as Map
4949
import Data.Profunctor as Profunctor
50+
import Data.String (stripSuffix) as String
51+
import Data.String.Pattern (Pattern(..)) as String
5052
import Partial.Unsafe (unsafeCrashWith)
5153
import Registry.Internal.Codec as Internal.Codec
5254
import Registry.License as License
@@ -477,11 +479,30 @@ legacyPackageSetEntryCodec = CA.object "LegacyPackageSetEntry"
477479
$ CA.recordProp (Proxy :: _ "dependencies") (CA.array PackageName.codec)
478480
$ CA.record
479481

480-
readConfig :: forall a. FilePath -> Spago (LogEnv a) (Either String { doc :: YamlDoc Config, yaml :: Config })
482+
readConfig :: forall a. FilePath -> Spago (LogEnv a) (Either (Array String) { doc :: YamlDoc Config, yaml :: Config })
481483
readConfig path = do
482484
logDebug $ "Reading config from " <> path
483485
FS.exists path >>= case _ of
484-
false -> pure $ Left $ case path of
485-
"spago.yaml" -> "Did not find " <> path <> " Run `spago init` to initialise a new project."
486-
_ -> "Did not find " <> path
487-
true -> liftAff $ FS.readYamlDocFile configCodec path
486+
false -> do
487+
let replaceExt = map (_ <> ".yml") <<< String.stripSuffix (String.Pattern ".yaml")
488+
yml <- map join $ for (replaceExt path) \yml -> do
489+
hasYml <- FS.exists yml
490+
pure $
491+
if hasYml then
492+
Just yml
493+
else
494+
Nothing
495+
pure $ Left $ case path, yml of
496+
"spago.yaml", Nothing ->
497+
[ "Did not find `" <> path <> "`. Run `spago init` to initialize a new project." ]
498+
"spago.yaml", Just y ->
499+
[ "Did not find `" <> path <> "`. Spago's configuration files must end with `.yaml`, not `.yml`. "
500+
, "Try renaming `" <> y <> "` to `" <> path <> "` or run `spago init` to initialize a new project."
501+
]
502+
_, Nothing ->
503+
[ "Did not find `" <> path <> "`." ]
504+
_, Just y ->
505+
[ "Did not find `" <> path <> "`. Spago's configuration files must end with `.yaml`, not `.yml`. "
506+
, "Try renaming `" <> y <> "` to `" <> path <> "`."
507+
]
508+
true -> liftAff $ map (lmap pure) $ FS.readYamlDocFile configCodec path

src/Spago/Command/Fetch.purs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,11 @@ getPackageDependencies packageName package = case package of
377377
Right { yaml: { package: Just { dependencies: (Dependencies deps) } } } -> do
378378
pure (Just (map (fromMaybe Config.widestRange) deps))
379379
Right _ -> die [ "Read valid configuration from " <> configLocation, "However, there was no `package` section to be read." ]
380-
Left err -> die [ "Could not read config at " <> configLocation, "Error: " <> err ]
380+
Left errLines -> die
381+
[ toDoc $ "Could not read config at " <> configLocation
382+
, toDoc "Error: "
383+
, indent $ toDoc errLines
384+
]
381385

382386
getWorkspacePackageDeps :: WorkspacePackage -> Dependencies
383387
getWorkspacePackageDeps pkg =

src/Spago/Config.purs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ readWorkspace { maybeSelectedPackage, pureBuild } = do
155155
-- First try to read the config in the root. It _has_ to contain a workspace
156156
-- configuration, or we fail early.
157157
{ workspace, package: maybePackage, workspaceDoc } <- Core.readConfig "spago.yaml" >>= case _ of
158-
Left err -> die [ "Couldn't parse Spago config, error:\n " <> err, "The configuration file help can be found here https://github.com/purescript/spago#the-configuration-file" ]
158+
Left errLines ->
159+
die
160+
[ toDoc "Couldn't parse Spago config, error:"
161+
, indent $ toDoc errLines
162+
, toDoc "The configuration file help can be found here https://github.com/purescript/spago#the-configuration-file"
163+
]
159164
Right { yaml: { workspace: Nothing } } -> die
160165
[ "Your spago.yaml doesn't contain a workspace section."
161166
, "See the relevant documentation here: https://github.com/purescript/spago#the-workspace"
@@ -188,15 +193,19 @@ readWorkspace { maybeSelectedPackage, pureBuild } = do
188193
-- We try to figure out if this package has tests - look for test sources
189194
hasTests <- FS.exists (Path.concat [ Path.dirname path, "test" ])
190195
pure $ case maybeConfig of
191-
Left e -> Left $ "Could not read config at path " <> path <> "\nError was: " <> e
192-
Right { yaml: { package: Nothing } } -> Left $ "No package found for config at path: " <> path
196+
Left eLines -> Left $ toDoc
197+
[ toDoc $ "Could not read config at path " <> path
198+
, toDoc "Error was: "
199+
, indent $ toDoc eLines
200+
]
201+
Right { yaml: { package: Nothing } } -> Left $ toDoc $ "No package found for config at path: " <> path
193202
Right { yaml: { package: Just package, workspace: configWorkspace }, doc } -> do
194203
-- We store the path of the package, so we can treat it basically as a LocalPackage
195204
Right $ Tuple package.name { path: Path.dirname path, package, configWorkspace, doc, hasTests }
196205
{ right: otherPackages, left: failedPackages } <- partitionMap identity <$> traverse readWorkspaceConfig otherConfigPaths
197206

198207
unless (Array.null failedPackages) do
199-
logWarn $ [ "Failed to read some configs:" ] <> failedPackages
208+
logWarn $ [ toDoc "Failed to read some configs:" ] <> failedPackages
200209

201210
-- We prune any configs that use a different workspace.
202211
-- For reasoning, see https://github.com/purescript/spago/issues/951
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Reading Spago workspace configuration...
2+
3+
❌ Couldn't parse Spago config, error:
4+
Did not find `spago.yaml`. Spago's configuration files must end with `.yaml`, not `.yml`.
5+
Try renaming `spago.yml` to `spago.yaml` or run `spago init` to initialize a new project.
6+
The configuration file help can be found here https://github.com/purescript/spago#the-configuration-file

test/Spago/Test.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,7 @@ spec = Spec.around withTempDir do
138138
Assert.fail $ "STDERR did not contain text:\n" <> exp <> "\n\nStderr was:\n" <> stdErr
139139
spago [ "test" ] >>= check { stdout: mempty, stderr: hasUnusedNameWarningError, result: isRight }
140140

141+
Spec.it "check for spago.yml" \{ spago, fixture } -> do
142+
spago [ "init", "--name", "aaa" ] >>= shouldBeSuccess
143+
FS.moveSync {src: "spago.yaml", dst: "spago.yml"}
144+
spago [ "build" ] >>= shouldBeFailureErr (fixture "spago-yml-check-stderr.txt")

0 commit comments

Comments
 (0)