From 50bc875423babb16943feda9bb1fa052289147a0 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 11 Mar 2026 19:14:31 -0500 Subject: [PATCH] Fix stale template cache by re-cloning when force-pull fails Shallow clones (depth=1) cause isomorphic-git's fetch to fail when the remote template has new commits. Instead of showing an error toast and falling back to the stale cached template, delete the cache and re-clone fresh to ensure users always get the latest template. --- src/lib/ProjectsManager.ts | 56 ++++++++++++++++++++------------------ src/locales/en.json | 2 -- src/pages/Index.tsx | 8 ------ 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/lib/ProjectsManager.ts b/src/lib/ProjectsManager.ts index 381ebd7c..56459b42 100644 --- a/src/lib/ProjectsManager.ts +++ b/src/lib/ProjectsManager.ts @@ -57,50 +57,52 @@ export class ProjectsManager { * Returns the path to the cached template. * * @param templateUrl - Git repository URL of the template - * @param onUpdateError - Optional callback when template update fails (non-critical) * @returns Path to the cached template directory */ - async updateTemplate(templateUrl: string, onUpdateError?: (error: Error) => void): Promise { + async updateTemplate(templateUrl: string): Promise { // Generate a safe directory name from the template URL const templateName = this.getTemplateNameFromUrl(templateUrl); const templatePath = `${this.templatesDir}/${templateName}`; - try { - // Check if template already exists - await this.fs.stat(templatePath); + const templateExists = await this.fs.stat(templatePath).then(() => true, () => false); + if (templateExists) { // Template exists, try to update it with force pull try { await this.forcePullTemplate(templatePath); + return templatePath; } catch (updateError) { - // Update failed, but we can continue with the existing template - console.warn(`Failed to update template at ${templatePath}:`, updateError); - if (onUpdateError) { - onUpdateError(updateError instanceof Error ? updateError : new Error(String(updateError))); - } - } - } catch { - // Template doesn't exist, clone it shallowly - await this.fs.mkdir(templatePath, { recursive: true }); - - try { - await this.git.clone({ - dir: templatePath, - url: templateUrl, - singleBranch: true, - depth: 1, - }); - } catch (cloneError) { - // Clean up on clone failure + // Force pull failed (common with shallow clones when remote has new commits). + // Delete the stale cache and re-clone fresh below. + console.warn(`Force pull failed for template at ${templatePath}, re-cloning fresh:`, updateError); try { await this.deleteDirectory(templatePath); } catch { // Ignore cleanup errors } - throw cloneError; } } + // Template doesn't exist (or was deleted after failed update), clone it fresh + await this.fs.mkdir(templatePath, { recursive: true }); + + try { + await this.git.clone({ + dir: templatePath, + url: templateUrl, + singleBranch: true, + depth: 1, + }); + } catch (cloneError) { + // Clean up on clone failure + try { + await this.deleteDirectory(templatePath); + } catch { + // Ignore cleanup errors + } + throw cloneError; + } + return templatePath; } @@ -224,7 +226,7 @@ export class ProjectsManager { } } - async createProject(name: string, templateUrl: string, customId?: string, onTemplateUpdateError?: (error: Error) => void, templateMetadata?: { name: string; description: string }): Promise { + async createProject(name: string, templateUrl: string, customId?: string, templateMetadata?: { name: string; description: string }): Promise { const id = customId || await this.generateUniqueProjectId(name); // Check if project with this ID already exists when using custom ID @@ -235,7 +237,7 @@ export class ProjectsManager { const projectPath = `${this.dir}/${id}`; // Ensure template is cached and up-to-date - const templatePath = await this.updateTemplate(templateUrl, onTemplateUpdateError); + const templatePath = await this.updateTemplate(templateUrl); // Create project directory await this.fs.mkdir(projectPath, { recursive: true }); diff --git a/src/locales/en.json b/src/locales/en.json index 1d82021e..c327ebe4 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -537,8 +537,6 @@ "deleting": "Deleting...", "restoreToDefault": "Restore to default", "restoredToDefault": "Restored to Default", - "templateUpdateFailed": "Template Update Failed", - "templateUpdateFailedDescription": "Failed to update the project template from the remote repository. Using the cached version instead.", "templatesDirectory": "Templates", "terminal": "Terminal", "templateName": "Template Name", diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 8c44e3e0..c578f5de 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -234,14 +234,6 @@ export default function Index() { prompt.trim(), template.url, projectId, - () => { - // Show a toast if template update fails (non-critical) - toast({ - title: t('templateUpdateFailed'), - description: t('templateUpdateFailedDescription'), - variant: 'default', - }); - }, { name: template.name, description: template.description } );