Skip to content

Commit 8a6636e

Browse files
fix(cli): the latest version in _meta is never older than the running CLI (heygen-com#7049)
* fix(cli): the latest version in _meta is never older than the running CLI * fix(cli): keep the daily recheck cadence when the cached latest is older than the running CLI
1 parent eb35629 commit 8a6636e

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎packages/cli/src/utils/updateCheck.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ describe("updateCheckDue / cachedUpdateCheck — what the parent reads without f
403403
afterEach(() => {
404404
vi.doUnmock("../telemetry/config.js");
405405
vi.doUnmock("./env.js");
406+
vi.doUnmock("../version.js");
406407
vi.resetModules();
407408
vi.unstubAllEnvs();
408409
});
@@ -470,7 +471,19 @@ describe("updateCheckDue / cachedUpdateCheck — what the parent reads without f
470471
},
471472
);
472473

473-
it.each(["0.0.1", "999.0.0", "999.0.0-beta.1+build.5"])(
474+
it("treats a cached latest older than the running CLI as unknown, without re-checking sooner than the daily cadence", async () => {
475+
vi.doMock("../version.js", () => ({ VERSION: "0.9.6", PRODUCER_VERSION: "0.9.6" }));
476+
const mod = await load({ lastUpdateCheck: hoursAgo(1), latestVersion: "0.9.5" });
477+
expect(mod.updateCheckDue()).toBe(false);
478+
expect(mod.getUpdateMeta()).toEqual({
479+
version: "0.9.6",
480+
latestVersion: undefined,
481+
updateAvailable: false,
482+
});
483+
expect(mod.cachedUpdateCheck().latest).toBe("0.9.6");
484+
});
485+
486+
it.each(["999.0.0", "999.0.0-beta.1+build.5"])(
474487
"preserves safe cached version %s and the update-check result in metadata",
475488
async (latestVersion) => {
476489
const mod = await load({ latestVersion });

‎packages/cli/src/utils/updateCheck.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ export async function checkForUpdate(force?: boolean): Promise<UpdateCheckResult
9696
/** Whether the background check should ask the registry: not opted out, and no fresh safe cache. */
9797
export function updateCheckDue(config = readConfig()): boolean {
9898
if (updateCheckDisabled()) return false;
99-
if (!config.lastUpdateCheck || !config.latestVersion || !isSafeVersion(config.latestVersion)) {
100-
return true;
101-
}
99+
if (!config.lastUpdateCheck || !isSafeCache(config.latestVersion)) return true;
102100
const fresh = Date.now() - new Date(config.lastUpdateCheck).getTime() < CHECK_INTERVAL_MS;
103101
return !fresh;
104102
}
@@ -110,24 +108,27 @@ export function cachedUpdateCheck(): UpdateCheckResult {
110108
function fallbackResult(cachedLatest?: string): UpdateCheckResult {
111109
// Only surface a cached version we can prove is safe — a pre-existing
112110
// poisoned cache must not leak through the fallback path either.
113-
const safeCached = safeCachedVersion(cachedLatest);
111+
const safeCached = cachedVersionForThisInstall(cachedLatest);
114112
return {
115113
current: VERSION,
116114
latest: safeCached ?? VERSION,
117115
updateAvailable: safeCached ? isNewerSemver(safeCached, VERSION) : false,
118116
};
119117
}
120118

121-
function safeCachedVersion(value: unknown): string | undefined {
122-
return typeof value === "string" && isSafeVersion(value) ? value : undefined;
119+
const isSafeCache = (value: unknown): value is string =>
120+
typeof value === "string" && isSafeVersion(value);
121+
122+
function cachedVersionForThisInstall(value: unknown): string | undefined {
123+
return isSafeCache(value) && !isNewerSemver(VERSION, value) ? value : undefined;
123124
}
124125

125126
/**
126127
* Synchronous read from cache — for _meta envelope on --json commands.
127128
* Never fetches. Returns what the last background check found.
128129
*/
129130
export function getUpdateMeta(): UpdateMeta {
130-
const latestVersion = safeCachedVersion(readConfig().latestVersion);
131+
const latestVersion = cachedVersionForThisInstall(readConfig().latestVersion);
131132
return {
132133
version: VERSION,
133134
latestVersion,

0 commit comments

Comments
 (0)