Skip to content

Commit 15c8951

Browse files
committed
Fall back to old cached releases if user doesn't want to update or downloading the releases fails
1 parent a350425 commit 15c8951

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/hlsBinaries.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const githubReleaseApiValidator: validate.Validator<IRelease[]> = validate.array
3939

4040
const cachedReleaseValidator: validate.Validator<IRelease[] | null> = validate.optional(githubReleaseApiValidator);
4141

42+
const cachedReleaseValidatorOld: validate.Validator<IRelease | null> = validate.optional(releaseValidator);
43+
4244
// On Windows the executable needs to be stored somewhere with an .exe extension
4345
const exeExt = process.platform === 'win32' ? '.exe' : '';
4446

@@ -221,10 +223,36 @@ async function getReleaseMetadata(context: ExtensionContext, storagePath: string
221223

222224
const offlineCache = path.join(storagePath, 'approvedReleases.cache.json');
223225

224-
async function readCachedReleaseData(): Promise<IRelease[] | null> {
226+
async function readCachedReleaseData(fallBack: boolean): Promise<IRelease[] | null> {
225227
try {
226228
const cachedInfo = await promisify(fs.readFile)(offlineCache, { encoding: 'utf-8' });
227229
return validate.parseAndValidate(cachedInfo, cachedReleaseValidator);
230+
} catch (err: any) {
231+
// If file doesn't exist, return null unless fallBack is true. In that case try to
232+
// read from the older cache file format (1.7.1 and earlier).
233+
// Consider everything else it a failure
234+
if (err.code === 'ENOENT') {
235+
if (fallBack) {
236+
return readOldCachedReleaseData();
237+
} else {
238+
return null;
239+
}
240+
}
241+
throw err;
242+
}
243+
}
244+
245+
const offlineCacheOld = path.join(storagePath, 'latestApprovedRelease.cache.json');
246+
247+
async function readOldCachedReleaseData(): Promise<IRelease[] | null> {
248+
try {
249+
const cachedInfo = await promisify(fs.readFile)(offlineCacheOld, { encoding: 'utf-8' });
250+
const cached = validate.parseAndValidate(cachedInfo, cachedReleaseValidatorOld);
251+
if (cached) {
252+
return [cached];
253+
} else {
254+
return null;
255+
}
228256
} catch (err: any) {
229257
// If file doesn't exist, return null, otherwise consider it a failure
230258
if (err.code === 'ENOENT') {
@@ -233,11 +261,12 @@ async function getReleaseMetadata(context: ExtensionContext, storagePath: string
233261
throw err;
234262
}
235263
}
264+
236265
// Not all users want to upgrade right away, in that case prompt
237266
const updateBehaviour = workspace.getConfiguration('haskell').get('updateBehavior') as UpdateBehaviour;
238267

239268
if (updateBehaviour === 'never-check') {
240-
return readCachedReleaseData();
269+
return readCachedReleaseData(true)
241270
}
242271

243272
try {
@@ -246,7 +275,7 @@ async function getReleaseMetadata(context: ExtensionContext, storagePath: string
246275
validate.parseAndValidate(releaseInfo, githubReleaseApiValidator).filter((x) => !x.prerelease) || null;
247276

248277
if (updateBehaviour === 'prompt') {
249-
const cachedInfoParsed = await readCachedReleaseData();
278+
const cachedInfoParsed = await readCachedReleaseData(false);
250279

251280
if (
252281
releaseInfoParsed !== null && releaseInfoParsed.length > 0 &&
@@ -261,7 +290,7 @@ async function getReleaseMetadata(context: ExtensionContext, storagePath: string
261290
const decision = await window.showInformationMessage(promptMessage, 'Download', 'Nevermind');
262291
if (decision !== 'Download') {
263292
// If not upgrade, bail and don't overwrite cached version information
264-
return cachedInfoParsed;
293+
return readCachedReleaseData(true);
265294
}
266295
}
267296
}
@@ -272,7 +301,7 @@ async function getReleaseMetadata(context: ExtensionContext, storagePath: string
272301
} catch (githubError: any) {
273302
// Attempt to read from the latest cached file
274303
try {
275-
const cachedInfoParsed = await readCachedReleaseData();
304+
const cachedInfoParsed = await readCachedReleaseData(true);
276305

277306
window.showWarningMessage(
278307
`Couldn't get the latest haskell-language-server releases from GitHub, used local cache instead:\n${githubError.message}`

0 commit comments

Comments
 (0)