Skip to content

Commit

Permalink
Avoid stale DAML_SDK_VERSION_LATEST in assistant (#12493)
Browse files Browse the repository at this point in the history
* Avoid stale "latest SDK version" in assistant

This PR changes how DAML_SDK_VERSION_LATEST is computed, so that its
value is never stale (it will be blank instead).

This PR also changes what gets printed in `daml version` to not rely on the
environment variable DAML_SDK_VERSION_LATEST. It instead shows the latest
version obtained from docs.daml.com, if available.

changelog_begin
changelog_end

* treat all values as stale with update-check: never
  • Loading branch information
sofiafaro-da authored Jan 20, 2022
1 parent 7a1b37c commit a644406
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion daml-assistant/exe/DA/Daml/Assistant.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ runCommand env@Env{..} = \case
let asstVersion = unwrapDamlAssistantSdkVersion <$> envDamlAssistantSdkVersion
envVersions = catMaybes
[ envSdkVersion
, envLatestStableSdkVersion
, guard vAssistant >> asstVersion
, projectVersionM
, defaultVersionM
Expand Down Expand Up @@ -241,6 +240,7 @@ runCommand env@Env{..} = \case

versions = nubSort . concat $
[ envVersions
, maybeToList latestVersionM
, fromRight [] installedVersionsE
, if vAll then fromRight [] availableVersionsE else []
, fromRight [] snapshotVersionsE
Expand Down
16 changes: 8 additions & 8 deletions daml-assistant/src/DA/Daml/Assistant/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module DA.Daml.Assistant.Cache
( cacheAvailableSdkVersions
, saveAvailableSdkVersions
, CacheAge (..)
) where

import DA.Daml.Assistant.Types
Expand Down Expand Up @@ -53,15 +54,15 @@ cacheAvailableSdkVersions
:: DamlPath
-> CachePath
-> IO [SdkVersion]
-> IO [SdkVersion]
-> IO ([SdkVersion], CacheAge)
cacheAvailableSdkVersions damlPath cachePath getVersions = do
damlConfigE <- tryConfig $ readDamlConfig damlPath
let updateCheckM = join $ eitherToMaybe (queryDamlConfig ["update-check"] =<< damlConfigE)
defaultUpdateCheck = UpdateCheckEvery (CacheTimeout 86400)
case fromMaybe defaultUpdateCheck updateCheckM of
UpdateCheckNever -> do
valueAgeM <- loadFromCacheWith cachePath versionsKey (CacheTimeout 0) deserializeVersions
pure (maybe [] fst valueAgeM)
pure $ fromMaybe ([], Stale) valueAgeM

UpdateCheckEvery timeout ->
cacheWith cachePath versionsKey timeout
Expand All @@ -86,22 +87,22 @@ cacheWith
-> Serialize t
-> Deserialize t
-> IO t
-> IO t
-> IO (t, CacheAge)
cacheWith cachePath key timeout serialize deserialize getFresh = do
valueAgeM <- loadFromCacheWith cachePath key timeout deserialize
case valueAgeM of
Just (value, Fresh) -> pure value
Just (value, Fresh) -> pure (value, Fresh)
Just (value, Stale) -> do
valueE <- tryAny getFresh
case valueE of
Left _ -> pure value
Left _ -> pure (value, Stale)
Right value' -> do
saveToCacheWith cachePath key serialize value'
pure value'
pure (value', Fresh)
Nothing -> do
value <- getFresh
saveToCacheWith cachePath key serialize value
pure value
pure (value, Fresh)

-- | A representation of the age of a cache value. We only care if the value is stale or fresh.
data CacheAge
Expand Down Expand Up @@ -141,4 +142,3 @@ loadFromCacheWith cachePath key timeout deserialize = do
(valueStr, age) <- valueAgeM
value <- deserialize valueStr
Just (value, age)

8 changes: 5 additions & 3 deletions daml-assistant/src/DA/Daml/Assistant/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ refreshAvailableSdkVersions cachePath = do

-- | Same as getAvailableSdkVersions, but result is cached based on the duration
-- of the update-check value in daml-config.yaml (defaults to 1 day).
getAvailableSdkVersionsCached :: DamlPath -> CachePath -> IO [SdkVersion]
getAvailableSdkVersionsCached :: DamlPath -> CachePath -> IO ([SdkVersion], CacheAge)
getAvailableSdkVersionsCached damlPath cachePath =
cacheAvailableSdkVersions damlPath cachePath getAvailableSdkVersions

Expand All @@ -181,8 +181,10 @@ getLatestSdkVersionCached :: DamlPath -> CachePath -> IO (Maybe SdkVersion)
getLatestSdkVersionCached damlPath cachePath = do
versionsE <- tryAssistant $ getAvailableSdkVersionsCached damlPath cachePath
pure $ do
versions <- eitherToMaybe versionsE
maximumMay versions
(versions, age) <- eitherToMaybe versionsE
case age of
Stale -> Nothing
Fresh -> maximumMay versions

-- | Get the latest snapshot SDK version.
getLatestSdkSnapshotVersion :: IO (Maybe SdkVersion)
Expand Down

0 comments on commit a644406

Please sign in to comment.