From b698ab9dd4e5c76f055e8dca3e7d77958b9370b7 Mon Sep 17 00:00:00 2001 From: vgcman16 <155417613+vgcman16@users.noreply.github.com> Date: Thu, 5 Jun 2025 14:46:20 -0500 Subject: [PATCH] avoid unnecessary file rewrites --- README.md | 2 +- app/lib/hooks/useGit.ts | 24 +++++++++++++++--------- app/lib/runtime/action-runner.ts | 13 +++++++++++++ app/lib/stores/files.ts | 12 +++++++++++- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cbcd8a4c..c7408d69 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ project, please check the [project management guide](./PROJECT.md) to get starte - ✅ Perplexity Integration (@meetpateltech) - ✅ AWS Bedrock Integration (@kunjabijukchhe) - ✅ Add a "Diff View" to see the changes (@toddyclipsgg) -- ⬜ **HIGH PRIORITY** - Prevent bolt from rewriting files as often (file locking and diffs) +- ✅ **HIGH PRIORITY** - Prevent bolt from rewriting files as often (file locking and diffs) - ⬜ **HIGH PRIORITY** - Better prompting for smaller LLMs (code window sometimes doesn't start) - ⬜ **HIGH PRIORITY** - Run agents in the backend as opposed to a single model call - ✅ Deploy directly to Netlify (@xKevIsDev) diff --git a/app/lib/hooks/useGit.ts b/app/lib/hooks/useGit.ts index 387f794f..77860981 100644 --- a/app/lib/hooks/useGit.ts +++ b/app/lib/hooks/useGit.ts @@ -196,17 +196,23 @@ const getFs = ( } try { - // Handle encoding properly based on data type if (data instanceof Uint8Array) { - // For binary data, don't pass encoding - const result = await webcontainer.fs.writeFile(relativePath, data); - return result; - } else { - // For text data, use the encoding if provided - const encoding = options?.encoding || 'utf8'; - const result = await webcontainer.fs.writeFile(relativePath, data, encoding); + const existing = await webcontainer.fs.readFile(relativePath).catch(() => null); - return result; + if (existing && Buffer.compare(existing, data) === 0) { + return; + } + + return await webcontainer.fs.writeFile(relativePath, data); + } else { + const encoding = options?.encoding || 'utf8'; + const existing = await webcontainer.fs.readFile(relativePath, encoding).catch(() => null); + + if (typeof existing === 'string' && existing === data) { + return; + } + + return await webcontainer.fs.writeFile(relativePath, data, encoding); } } catch (error) { throw error; diff --git a/app/lib/runtime/action-runner.ts b/app/lib/runtime/action-runner.ts index a3436a9f..20db0816 100644 --- a/app/lib/runtime/action-runner.ts +++ b/app/lib/runtime/action-runner.ts @@ -322,6 +322,19 @@ export class ActionRunner { } try { + let existing: string | null = null; + + try { + existing = await webcontainer.fs.readFile(relativePath, 'utf-8'); + } catch { + existing = null; + } + + if (existing !== null && existing === action.content) { + logger.debug(`Skipped writing ${relativePath} (no changes)`); + return; + } + await webcontainer.fs.writeFile(relativePath, action.content); logger.debug(`File written ${relativePath}`); } catch (error) { diff --git a/app/lib/stores/files.ts b/app/lib/stores/files.ts index e2d8e40f..d4b87198 100644 --- a/app/lib/stores/files.ts +++ b/app/lib/stores/files.ts @@ -563,7 +563,17 @@ export class FilesStore { unreachable('Expected content to be defined'); } - await webcontainer.fs.writeFile(relativePath, content); + try { + const existing = await webcontainer.fs.readFile(relativePath, 'utf-8'); + + if (existing === content) { + logger.debug(`Skipped writing ${relativePath} (no changes)`); + } else { + await webcontainer.fs.writeFile(relativePath, content); + } + } catch { + await webcontainer.fs.writeFile(relativePath, content); + } if (!this.#modifiedFiles.has(filePath)) { this.#modifiedFiles.set(filePath, oldContent);