Skip to content

Commit 0d19acf

Browse files
committed
Warns users about non-ghcup channels
1 parent c07d28e commit 0d19acf

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

ghcup.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,4 @@ test-suite ghcup-optparse-test
538538
, template-haskell
539539
, text
540540
, uri-bytestring
541-
, versions
541+
, versions

lib/GHCup.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,4 @@ rmTmp = do
675675
ghcup_dirs <- liftIO getGHCupTmpDirs
676676
forM_ ghcup_dirs $ \f -> do
677677
logDebug $ "rm -rf " <> T.pack (fromGHCupPath f)
678-
rmPathForcibly f
679-
680-
678+
rmPathForcibly f

lib/GHCup/Download.hs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ import qualified Data.Text as T
8989
import qualified Data.Text.IO as T
9090
import qualified Data.Text.Encoding as E
9191
import qualified Data.Yaml.Aeson as Y
92+
import Data.List (isPrefixOf)
93+
import Control.Monad.IO.Class (liftIO)
9294

9395

9496

@@ -103,7 +105,42 @@ import qualified Data.Yaml.Aeson as Y
103105

104106

105107

108+
officialPrefixes :: [String]
109+
officialPrefixes =
110+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/"
111+
, "https://mirror.sjtu.edu.cn/ghcup/yaml/haskell/ghcup-metadata/"
112+
]
113+
106114
-- | Downloads the download information! But only if we need to ;P
115+
isDefaultURL :: NewURLSource -> Bool
116+
isDefaultURL NewGHCupURL = True
117+
isDefaultURL NewStackSetupURL = True
118+
isDefaultURL (NewChannelAlias StackChannel) = True
119+
isDefaultURL (NewChannelAlias _) = True
120+
isDefaultURL (NewGHCupInfo _) = False -- Custom GHCupInfo is not a default source
121+
isDefaultURL (NewSetupInfo _) = False -- Custom SetupInfo is not a default source
122+
isDefaultURL (NewURI uri) = show uri `elem` defaultURLs
123+
124+
-- List of default/known URLs
125+
defaultURLs :: [String]
126+
defaultURLs =
127+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-0.0.9.yaml"
128+
, "https://raw.githubusercontent.com/commercialhaskell/stackage-content/master/stack/stack-setup-2.yaml"
129+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-cross-0.0.9.yaml"
130+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-prereleases-0.0.9.yaml"
131+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-vanilla-0.0.9.yaml"
132+
]
133+
134+
-- Extract URL string from NewURLSource if possible, for checking
135+
getUrlStringFromSource :: NewURLSource -> Maybe String
136+
getUrlStringFromSource NewGHCupURL = Just $ show ghcupURL
137+
getUrlStringFromSource NewStackSetupURL = Just $ show stackSetupURL
138+
getUrlStringFromSource (NewChannelAlias c) = Just $ show $ channelURL c
139+
getUrlStringFromSource (NewGHCupInfo _) = Nothing
140+
getUrlStringFromSource (NewSetupInfo _) = Nothing
141+
getUrlStringFromSource (NewURI uri) = Just $ show uri
142+
143+
-- Modified getDownloadsF to include URL prefix check
107144
getDownloadsF :: ( FromJSONKey Tool
108145
, FromJSONKey Version
109146
, FromJSON VersionInfo
@@ -124,6 +161,23 @@ getDownloadsF :: ( FromJSONKey Tool
124161
GHCupInfo
125162
getDownloadsF pfreq@(PlatformRequest arch plat _) = do
126163
Settings { urlSource } <- lift getSettings
164+
165+
-- Check for custom URL sources that aren't in the default list
166+
forM_ urlSource $ \src ->
167+
case src of
168+
NewURI uri -> do
169+
let url = show uri
170+
logDebug $ "Checking URI: " <> T.pack url
171+
when (not (url `elem` defaultURLs) &&
172+
not (any (`isPrefixOf` url) officialPrefixes) ||
173+
url == "https://ghc.gitlab.haskell.org/ghcup-metadata/ghcup-nightlies-0.0.7.yaml") $
174+
logWarn $ "The URL " <> T.pack url <> " is not an official GHCup metadata source and may not be maintained or QA'd by GHCup."
175+
NewGHCupInfo _ ->
176+
logWarn "Using custom GHCupInfo which is not an official GHCup metadata source"
177+
NewSetupInfo _ ->
178+
logWarn "Using custom SetupInfo which is not an official GHCup metadata source"
179+
_ -> pure ()
180+
127181
infos <- liftE $ mapM dl' urlSource
128182
keys <- if any isRight infos
129183
then liftE . reThrowAll @_ @_ @'[StackPlatformDetectError] StackPlatformDetectError $ getStackPlatformKey pfreq
@@ -133,7 +187,17 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
133187
Right si -> pure $ fromStackSetupInfo si keys
134188
mergeGhcupInfo ghcupInfos
135189
where
136-
190+
-- Default URLs that are known to be official
191+
defaultURLs :: [String]
192+
defaultURLs =
193+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-0.0.9.yaml"
194+
, "https://raw.githubusercontent.com/commercialhaskell/stackage-content/master/stack/stack-setup-2.yaml"
195+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-cross-0.0.9.yaml"
196+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-prereleases-0.0.9.yaml"
197+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-vanilla-0.0.9.yaml"
198+
, "https://ghc.gitlab.haskell.org/ghcup-metadata/ghcup-nightlies-0.0.7.yaml" -- Nightly channel mentioned in config example
199+
]
200+
137201
dl' :: ( FromJSONKey Tool
138202
, FromJSONKey Version
139203
, FromJSON VersionInfo
@@ -164,6 +228,7 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
164228
Right <$> decodeMetadata @Stack.SetupInfo base)
165229
$ fmap Left (decodeMetadata @GHCupInfo base >>= \gI -> warnOnMetadataUpdate uri gI >> pure gI)
166230

231+
167232
fromStackSetupInfo :: MonadThrow m
168233
=> Stack.SetupInfo
169234
-> [String]
@@ -890,4 +955,4 @@ applyMirrors (DM ms) uri@(URI { uriAuthority = Just (Authority { authorityHost =
890955
}
891956
Just (DownloadMirror auth Nothing) ->
892957
uri { uriAuthority = Just auth }
893-
applyMirrors _ uri = uri
958+
applyMirrors _ uri = uri

0 commit comments

Comments
 (0)