From 2fb04eb2db3b617193afdc130c0dad15fd8793ce Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 17 Jul 2025 17:09:27 -0700 Subject: [PATCH] fix: clear globalContext.json if corrupt (#6683) --- core/util/GlobalContext.test.ts | 74 +++++++++++++++++++++++++++++---- core/util/GlobalContext.ts | 35 +++++++++++++++- 2 files changed, 98 insertions(+), 11 deletions(-) diff --git a/core/util/GlobalContext.test.ts b/core/util/GlobalContext.test.ts index b13f3b87b..6fdcda7e7 100644 --- a/core/util/GlobalContext.test.ts +++ b/core/util/GlobalContext.test.ts @@ -1,7 +1,6 @@ // core/util/GlobalContext.test.ts -import { GlobalContext, GlobalContextType } from "./GlobalContext"; import fs from "node:fs"; -import path from "path"; +import { GlobalContext } from "./GlobalContext"; import { getGlobalContextFilePath } from "./paths"; describe("GlobalContext", () => { @@ -69,13 +68,40 @@ describe("GlobalContext", () => { const value = globalContext.get("indexingPaused"); expect(value).toBeUndefined(); expect(consoleWarnMock).toHaveBeenCalledWith( - expect.stringContaining("Error parsing global context"), + expect.stringContaining( + "Error parsing global context, deleting corrupted file", + ), ); // Clean up consoleWarnMock.mockRestore(); }); + it("should delete corrupted file on get and allow fresh start", () => { + // Write invalid JSON to the file + fs.writeFileSync(globalContextFilePath, "{ invalid json }"); + expect(fs.existsSync(globalContextFilePath)).toBe(true); + + const consoleWarnMock = jest + .spyOn(console, "warn") + .mockImplementation(() => {}); + + // Try to get a value - this should delete the corrupted file + const value = globalContext.get("indexingPaused"); + expect(value).toBeUndefined(); + + // File should be deleted after corruption is detected + expect(fs.existsSync(globalContextFilePath)).toBe(false); + + // Now we should be able to update and get values normally + globalContext.update("indexingPaused", true); + const newValue = globalContext.get("indexingPaused"); + expect(newValue).toBe(true); + + // Clean up + consoleWarnMock.mockRestore(); + }); + it("should handle JSON parsing errors gracefully on update", () => { // Write invalid JSON to the file fs.writeFileSync(globalContextFilePath, "{ invalid json }"); @@ -87,15 +113,45 @@ describe("GlobalContext", () => { // Attempt to update globalContext.update("indexingPaused", true); - // The update should have been skipped due to JSON parsing error - // So attempting to get the value should return undefined - const value = globalContext.get("indexingPaused"); - expect(value).toBeUndefined(); - expect(consoleWarnMock).toHaveBeenCalledWith( - expect.stringContaining("Error updating global context"), + expect.stringContaining( + "Error updating global context, deleting corrupted file", + ), ); + // The update should have recreated the file with just the new value + const value = globalContext.get("indexingPaused"); + expect(value).toBe(true); + + // Clean up + consoleWarnMock.mockRestore(); + }); + + it("should delete and recreate corrupted file on update", () => { + // Write invalid JSON to the file + fs.writeFileSync(globalContextFilePath, "{ invalid json }"); + expect(fs.existsSync(globalContextFilePath)).toBe(true); + + const consoleWarnMock = jest + .spyOn(console, "warn") + .mockImplementation(() => {}); + + // Try to update - this should delete the corrupted file and recreate it + globalContext.update("indexingPaused", true); + + // File should still exist but with valid JSON now + expect(fs.existsSync(globalContextFilePath)).toBe(true); + + // Should be able to read the value + const value = globalContext.get("indexingPaused"); + expect(value).toBe(true); + + // Should be able to add more values + globalContext.update("hasAlreadyCreatedAPromptFile", false); + expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBe(false); + // Original value should still be there + expect(globalContext.get("indexingPaused")).toBe(true); + // Clean up consoleWarnMock.mockRestore(); }); diff --git a/core/util/GlobalContext.ts b/core/util/GlobalContext.ts index 640bf7a56..6885edde2 100644 --- a/core/util/GlobalContext.ts +++ b/core/util/GlobalContext.ts @@ -68,7 +68,28 @@ export class GlobalContext { try { parsed = JSON.parse(data); } catch (e: any) { - console.warn(`Error updating global context: ${e}`); + console.warn( + `Error updating global context, deleting corrupted file: ${e}`, + ); + // Delete the corrupted file and recreate it fresh + try { + fs.unlinkSync(filepath); + } catch (deleteError) { + console.warn( + `Error deleting corrupted global context file: ${deleteError}`, + ); + } + // Recreate the file with just the new value + fs.writeFileSync( + filepath, + JSON.stringify( + { + [key]: value, + }, + null, + 2, + ), + ); return; } @@ -90,7 +111,17 @@ export class GlobalContext { const parsed = JSON.parse(data); return parsed[key]; } catch (e: any) { - console.warn(`Error parsing global context: ${e}`); + console.warn( + `Error parsing global context, deleting corrupted file: ${e}`, + ); + // Delete the corrupted file so it can be recreated fresh + try { + fs.unlinkSync(filepath); + } catch (deleteError) { + console.warn( + `Error deleting corrupted global context file: ${deleteError}`, + ); + } return undefined; } }