Skip to content

Commit 11fe90e

Browse files
authored
Improve the way we pick the package-set to upgrade to (#721)
1 parent 54fd119 commit 11fe90e

File tree

7 files changed

+88
-32
lines changed

7 files changed

+88
-32
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
shell: bash
5757

5858
- name: Build
59-
run: stack build
59+
run: stack build --pedantic
6060
shell: bash
6161

6262
- name: Install

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ghc-options:
1616
- -Wincomplete-record-updates
1717
- -Wredundant-constraints
1818
- -fprint-potential-instances
19+
- -optP-Wno-nonportable-include-path
1920

2021
flags:
2122
static:

spago.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: c5171d8aacb92261a66e4da21145765f3560a8cc8d8e794ef140a95e58c1b3de
7+
-- hash: 8a93a8d0d935703bc0cef9a60dfcacc9776c88e3e5ae6c5f7bc004e775ef1a92
88

99
name: spago
10-
version: 0.18.0
10+
version: 0.18.1
1111
description: Please see the README on GitHub at <https://github.com/purescript/spago#readme>
1212
homepage: https://github.com/purescript/spago#readme
1313
bug-reports: https://github.com/purescript/spago/issues

src/Spago/Config.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ import qualified Dhall.TypeCheck
3030
import qualified Web.Bower.PackageMeta as Bower
3131

3232
import qualified Spago.Dhall as Dhall
33-
import qualified Spago.GitHub as GitHub
3433
import qualified Spago.Messages as Messages
3534
import qualified Spago.PackageSet as PackageSet
3635
import qualified Spago.PscPackage as PscPackage
3736
import qualified Spago.Templates as Templates
37+
import qualified Spago.Purs as Purs
3838

3939

4040
type Expr = Dhall.DhallExpr Dhall.Import
@@ -164,17 +164,20 @@ ensureConfig = do
164164
-- | Create a Config in memory
165165
-- | For use by `spago script` and `spago repl`
166166
makeTempConfig
167-
:: (HasLogFunc env, HasGlobalCache env)
167+
:: HasLogFunc env
168168
=> [PackageName]
169169
-> Maybe Text
170170
-> [SourcePath]
171171
-> Maybe Text
172172
-> RIO env Config
173173
makeTempConfig dependencies alternateBackend configSourcePaths maybeTag = do
174174
tag <- case maybeTag of
175-
Nothing -> GitHub.getLatestPackageSetsTag "purescript" "package-sets" >>= (\case
176-
Left _ -> die [ "Failed to fetch latest package set tag" ]
177-
Right tag -> pure tag)
175+
Nothing -> do
176+
Purs.pursVersion >>= \case
177+
Left err -> die [ display err ]
178+
Right compilerVersion -> (PackageSet.getLatestSetForCompilerVersion compilerVersion "purescript" "package-sets") >>= \case
179+
Left _ -> die [ "spago script: failed to fetch latest package set tag" ]
180+
Right tag -> pure tag
178181
Just tag -> pure tag
179182

180183
expr <- liftIO $ Dhall.inputExpr $ "https://github.com/purescript/package-sets/releases/download/" <> tag <> "/packages.dhall"

src/Spago/GitHub.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{-# LANGUAGE ViewPatterns #-}
2-
module Spago.GitHub where
2+
module Spago.GitHub
3+
( getLatestPackageSetsTag
4+
, getLatestReleasesFile
5+
) where
36

47
import Spago.Prelude
58
import Spago.Env
@@ -10,13 +13,12 @@ import qualified Data.Text as Text
1013
import qualified Data.Text.Encoding
1114
import qualified Network.HTTP.Client as Http
1215
import qualified Network.HTTP.Simple as Http
16+
import qualified Data.Versions as Version
17+
import qualified Data.Map.Strict as Map
1318

1419
import qualified Spago.Messages as Messages
1520

1621

17-
tokenCacheFile :: IsString t => t
18-
tokenCacheFile = "github-token.txt"
19-
2022
tagCacheFile :: Text -> Text -> Text
2123
tagCacheFile org repo = org <> "-" <> repo <> "-tag.txt"
2224

@@ -63,3 +65,15 @@ getLatestPackageSetsTag org repo = do
6365
logWarn "Error following GitHub redirect, response:"
6466
logWarn $ displayShow response
6567
empty
68+
69+
getLatestReleasesFile
70+
:: (HasLogFunc env, MonadReader env m, MonadThrow m, MonadIO m)
71+
=> Text -> Text -> m (Map Version.SemVer Text)
72+
getLatestReleasesFile org repo = do
73+
logDebug $ "Getting `latest-compatible-sets.json` from " <> display org <> "/" <> display repo
74+
request <- Http.parseRequest $ "https://raw.githubusercontent.com/" <> (Text.unpack org) <> "/" <> (Text.unpack repo) <> "/master/latest-compatible-sets.json"
75+
response <- Http.responseBody <$> Http.httpJSON request
76+
let parseVersion (k, v) = case Version.semver k of
77+
Left _ -> Nothing
78+
Right version -> Just (version, v)
79+
pure $ Map.fromList $ mapMaybe parseVersion $ Map.toList response

src/Spago/Messages.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ failedToCopyToGlobalCache err = makeMessage
170170
, tshow err
171171
]
172172

173+
incompatiblePurs :: [Text] -> Text
174+
incompatiblePurs versions = makeMessage $
175+
[ "It seems that you're using a compiler version that is not supported by package-sets at the moment."
176+
, "Please install one of the following versions of the compiler and try again: "
177+
]
178+
<> map ("- " <>) versions
179+
<> [""]
180+
173181
pursVersionMismatch :: Text -> Text -> Text
174182
pursVersionMismatch currentVersion minVersion = makeMessage
175183
[ "Oh noes! It looks like the PureScript version installed on your system is not compatible with the package-set you're using."
@@ -180,7 +188,7 @@ pursVersionMismatch currentVersion minVersion = makeMessage
180188
, "There are a few ways to solve this:"
181189
, "- install a compatible `purs` version (i.e. in the same 'semver range' as the one in the package set)"
182190
, "- if the `purs` version is 'too new', you can try using `spago upgrade-set` to upgrade to the latest package set"
183-
, "- if you know what you're doing and you want to avoid this check, you can override the `version` of the `metadata` package in the packages.dhall:"
191+
, "- if you know what you're doing and you want to disable this check, you can override the `version` of the `metadata` package in the packages.dhall:"
184192
, ""
185193
, " let overrides = { metadata = upstream.metadata // { version = \"v" <> currentVersion <> "\" } }"
186194
, ""

src/Spago/PackageSet.hs

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Spago.PackageSet
22
( updatePackageSetVersion
3+
, getLatestSetForCompilerVersion
34
, checkPursIsUpToDate
45
, makePackageSetFile
56
, freeze
@@ -13,6 +14,8 @@ import Spago.Env
1314

1415
import qualified Control.Exception as Exception
1516
import Data.Dynamic (fromDynamic)
17+
import qualified Data.List as List
18+
import qualified Data.Map.Strict as Map
1619
import qualified Data.Text as Text
1720
import qualified Data.Versions as Version
1821
import qualified Dhall.Freeze
@@ -45,6 +48,26 @@ makePackageSetFile force comments = do
4548
else logWarn $ display $ Messages.foundExistingProject packagesPath
4649
Dhall.format packagesPath
4750

51+
getLatestSetForCompilerVersion
52+
:: HasLogFunc env
53+
=> Version.SemVer
54+
-> Text
55+
-> Text
56+
-> RIO env (Either SomeException Text)
57+
getLatestSetForCompilerVersion compilerVersion org repo = do
58+
logDebug $ "Got a compiler version: " <> displayShow compilerVersion
59+
-- we first try getting the file with the index of the latest sets
60+
eitherLatestReleases <- try $ GitHub.getLatestReleasesFile org repo
61+
logDebug $ "Latest releases: " <> displayShow eitherLatestReleases
62+
for eitherLatestReleases $ \latestReleases -> do
63+
-- if we get that, we just try to pick a set for the current compiler
64+
case Map.lookup compilerVersion latestReleases of
65+
-- but if the current compiler is not there we complain about compatibility
66+
Nothing -> die [ display $ Messages.incompatiblePurs (Version.prettySemVer <$> List.reverse (Map.keys latestReleases))]
67+
Just newTag -> do
68+
logDebug $ "Found a new tag in the releases file: " <> display newTag
69+
pure $ newTag
70+
4871
-- | Use the specified version of the package set (if specified).
4972
-- Otherwise, get the latest version of the package set if possible
5073
updatePackageSetVersion
@@ -65,30 +88,37 @@ updatePackageSetVersion maybeTag = do
6588

6689
maybe
6790
(useLatestRelease org repo currentTag)
68-
(useSpecificRelease org repo currentTag)
91+
(updateTag org repo currentTag)
6992
maybeTag
7093
where
71-
-- | Tries to upgrade the Package-Sets release of the local package set.
72-
-- It will:
73-
-- - try to read the latest tag from GitHub
74-
-- - try to read the current package-set file
75-
-- - try to replace the git tag to which the package-set imports point to
76-
-- (if they point to the Package-Sets repo. This can be eventually made GitHub generic)
77-
-- - if all of this succeeds, it will regenerate the hashes and write to file
94+
-- | Try to fetch the latest compatible tag in various ways, and updates
95+
-- the packages.dhall with it
7896
useLatestRelease
7997
:: Text -> Text -> Text -> RIO env ()
8098
useLatestRelease org repo currentTag = do
81-
GitHub.getLatestPackageSetsTag org repo >>= \case
82-
Right tag -> updateTag org repo currentTag tag
83-
Left (err :: SomeException) -> do
84-
logWarn "Was not possible to upgrade the package-sets release"
85-
logDebug $ "Error: " <> display err
86-
87-
useSpecificRelease
88-
:: Text -> Text -> Text -> Text -> RIO env ()
89-
useSpecificRelease org repo currentTag tag =
90-
updateTag org repo currentTag tag
99+
let getLatestFromGitHub = GitHub.getLatestPackageSetsTag org repo >>= \case
100+
Right tag -> updateTag org repo currentTag tag
101+
Left (err :: SomeException) -> do
102+
logWarn "Was not possible to upgrade the package-sets release"
103+
logDebug $ "Error: " <> display err
104+
-- first let's get the current compiler version
105+
Purs.pursVersion >>= \case
106+
Left err -> do
107+
-- if we cannot we just get the latest release from GitHub
108+
logDebug $ display err
109+
getLatestFromGitHub
110+
Right compilerVersion ->
111+
getLatestSetForCompilerVersion compilerVersion org repo >>= \case
112+
Right newTag -> updateTag org repo currentTag newTag
113+
-- if that fails then we fetch the latest set from GitHub
114+
-- TODO: check that it's compatible with the current compiler
115+
Left _err -> getLatestFromGitHub
91116

117+
-- | Tries to upgrade the package-sets release of the local package set.
118+
-- It will:
119+
-- - try to read the current package-set file
120+
-- - try to replace the git tag to which the package-set imports point to
121+
-- - if all of this succeeds, it will regenerate the hashes and write to file
92122
updateTag :: Text -> Text -> Text -> Text -> RIO env ()
93123
updateTag org repo currentTag specificTag = do
94124
let quotedTag = surroundQuote specificTag
@@ -136,7 +166,7 @@ updatePackageSetVersion maybeTag = do
136166
} = [(org, repo, currentTag)]
137167
getCurrentTag _ = []
138168

139-
-- | Given an import and a new purescript/package-sets tag,
169+
-- | Given an import and a new package-sets tag,
140170
-- upgrades the import to the tag and resets the hash
141171
upgradeImports :: Text -> Text -> Text -> Dhall.Import -> Dhall.Import
142172
upgradeImports org repo newTag (Dhall.Import

0 commit comments

Comments
 (0)