Skip to content

Chrome DevTools for coding agents

fix: ignore an invalid cached version in the update check (#3392)

kevin3a9f3780d23a
diff --git a/src/utils/check-for-updates.ts b/src/utils/check-for-updates.ts
index fb18ec14..668e938d 100644
--- a/src/utils/check-for-updates.ts
+++ b/src/utils/check-for-updates.ts
@@ -42,7 +42,13 @@ export async function checkForUpdates(message: string): Promise<void> {
try {
stats = await fs.stat(cachePath);
const data = await fs.readFile(cachePath, 'utf8');
- cachedVersion = JSON.parse(data).version;
+ const version: unknown = JSON.parse(data).version;
+ // A cache entry that is not a valid version would make `semver.lt` throw
+ // before the refresh below runs, so every start would fail on the same
+ // file. Ignore such an entry and let the refresh replace it.
+ if (typeof version === 'string' && semver.valid(version)) {
+ cachedVersion = version;
+ }
} catch {
// Ignore errors reading cache.
}
diff --git a/tests/utils/check-for-updates.test.ts b/tests/utils/check-for-updates.test.ts
index 7d55c3af..6c3b3c5d 100644
--- a/tests/utils/check-for-updates.test.ts
+++ b/tests/utils/check-for-updates.test.ts
@@ -141,6 +141,32 @@ describe('checkForUpdates', () => {
sinon.assert.calledOnce(unrefSpy);
});
+ it('ignores a cached version that is not valid semver and refreshes it', async () => {
+ sinon.stub(os, 'homedir').returns('/home/user');
+ sinon.stub(fs, 'stat').resolves({
+ mtimeMs: Date.now() - 25 * 60 * 60 * 1000, // 25 hours ago
+ } as unknown as Stats);
+ sinon.stub(fs, 'readFile').callsFake(async filePath => {
+ if (filePath.toString().includes('latest.json')) {
+ return JSON.stringify({
+ version: 'not-a-version',
+ });
+ }
+ throw new Error(`File not found: ${filePath}`);
+ });
+ const warnStub = sinon.stub(console, 'warn');
+ const unrefSpy = sinon.spy();
+ const spawnStub = sinon.stub(child_process, 'spawn').returns({
+ unref: unrefSpy,
+ } as unknown as child_process.ChildProcess);
+
+ await checkForUpdates('Run `npm update` to update.');
+
+ sinon.assert.notCalled(warnStub);
+ sinon.assert.calledOnce(spawnStub);
+ sinon.assert.calledOnce(unrefSpy);
+ });
+
it('spawns detached process if cache is missing', async () => {
sinon.stub(os, 'homedir').returns('/home/user');
sinon.stub(fs, 'stat').rejects(new Error('File not found'));