Skip to content

Commit 3dda687

Browse files
authored
fix: allow empty reads (#2961)
1 parent 194b1d2 commit 3dda687

File tree

12 files changed

+36
-45
lines changed

12 files changed

+36
-45
lines changed

apps/web/client/src/app/projects/import/local/_context/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ export const ProjectCreationProvider = ({ children, totalSteps }: ProjectCreatio
184184
(f) => f.path.endsWith('package.json') && f.type === ProcessedFileType.TEXT,
185185
);
186186

187-
if (!packageJsonFile?.content || typeof packageJsonFile.content !== 'string') {
188-
return { isValid: false, error: 'No package.json found' };
187+
if (typeof packageJsonFile?.content !== 'string') {
188+
return { isValid: false, error: 'Package.json is not a text file' };
189189
}
190190

191191
try {

apps/web/client/src/components/store/editor/chat/context.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export class ChatContext {
9090
}
9191

9292
const fileContent = await branchData.codeEditor.readFile(c.path);
93-
if (fileContent === null || fileContent instanceof Uint8Array) {
94-
console.error('No file content found or file is binary', c.path);
93+
if (fileContent instanceof Uint8Array) {
94+
console.error('File is binary', c.path);
9595
return c;
9696
}
9797
return { ...c, content: fileContent } satisfies FileMessageContext;
@@ -138,7 +138,8 @@ export class ChatContext {
138138
}
139139

140140
const content = await branchData.codeEditor.readFile(filePath);
141-
if (content === null || content instanceof Uint8Array) {
141+
if (content instanceof Uint8Array) {
142+
console.error('File is binary', filePath);
142143
continue;
143144
}
144145
fileContext.push({
@@ -248,10 +249,7 @@ export class ChatContext {
248249
return null;
249250
}
250251
const fileContent = await this.editorEngine.activeSandbox.readFile(filePath);
251-
if (fileContent === null) {
252-
return null;
253-
}
254-
if (fileContent instanceof Uint8Array) {
252+
if (fileContent === null || fileContent instanceof Uint8Array) {
255253
return null;
256254
}
257255
if (fileContent.trim().length === 0) {

apps/web/client/src/components/store/editor/font/font-config-manager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export const scanExistingFonts = async (layoutPath: string, editorEngine: Editor
4343

4444
try {
4545
const file = await sandbox.readFile(normalizedLayoutPath);
46-
if (!file || typeof file !== 'string') {
47-
console.log(`Layout file is empty or doesn't exist: ${layoutPath}`);
46+
if (typeof file !== 'string') {
47+
console.log(`Layout file is not text: ${layoutPath}`);
4848
return [];
4949
}
5050

@@ -188,13 +188,13 @@ export const readFontConfigFile = async (fontConfigPath: string, editorEngine: E
188188
| undefined
189189
> => {
190190
const codeEditor = editorEngine.fileSystem;
191-
191+
192192
// Ensure the font config file exists, create it if it doesn't
193193
await ensureFontConfigFileExists(fontConfigPath, editorEngine);
194194

195195
const file = await codeEditor.readFile(fontConfigPath);
196-
if (!file || typeof file !== 'string') {
197-
console.error("Font config file is empty or doesn't exist");
196+
if (typeof file !== 'string') {
197+
console.error("Font config file is not text");
198198
return;
199199
}
200200
const content = file;

apps/web/client/src/components/store/editor/font/layout-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export const traverseClassName = async (
231231

232232
try {
233233
const file = await sandbox.readFile(filePath);
234-
if (!file || typeof file !== 'string') {
234+
if (typeof file !== 'string') {
235235
console.error(`Failed to read file: ${filePath}`);
236236
return null;
237237
}
@@ -299,8 +299,8 @@ export const getLayoutContext = async (
299299
}
300300

301301
const file = await editorEngine.activeSandbox.readFile(layoutPath);
302-
if (!file || typeof file !== 'string') {
303-
console.error(`Failed to read file: ${layoutPath}`);
302+
if (typeof file !== 'string') {
303+
console.error(`Layout file is not text: ${layoutPath}`);
304304
return;
305305
}
306306

apps/web/client/src/components/store/editor/font/tailwind-config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const removeFontFromTailwindConfig = async (
1616
try {
1717
const file = await sandbox.readFile(tailwindConfigPath);
1818
if (typeof file !== 'string') {
19-
console.error("Tailwind config file is empty or doesn't exist");
19+
console.error("Tailwind config file is not text");
2020
return false;
2121
}
2222

@@ -43,8 +43,8 @@ export const addFontToTailwindConfig = async (
4343
): Promise<boolean> => {
4444
try {
4545
const file = await sandbox.readFile(tailwindConfigPath);
46-
if (!file || typeof file !== 'string') {
47-
console.error("Tailwind config file is empty or doesn't exist");
46+
if (typeof file !== 'string') {
47+
console.error("Tailwind config file is not text");
4848
return false;
4949
}
5050

apps/web/client/src/components/store/editor/pages/helper.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ const scanPagesDirectory = async (
356356
let metadata: PageMetadata | undefined;
357357
try {
358358
const fileContent = await sandboxManager.readFile(`${dir}/${entry.name}`);
359-
if (!fileContent || typeof fileContent !== 'string') {
360-
throw new Error(`File ${dir}/${entry.name} not found or is not a text file`);
359+
if (typeof fileContent !== 'string') {
360+
throw new Error(`File ${dir}/${entry.name} is not a text file`);
361361
}
362362
metadata = await extractMetadata(fileContent);
363363
} catch (error) {
@@ -763,8 +763,8 @@ export const updatePageMetadataInSandbox = async (
763763
}
764764

765765
const file = await sandboxManager.readFile(pageFilePath);
766-
if (!file || typeof file !== 'string') {
767-
throw new Error('Page file not found or is not a text file');
766+
if (typeof file !== 'string') {
767+
throw new Error('Page file is not a text file');
768768
}
769769
const pageContent = file;
770770
const hasUseClient =
@@ -795,8 +795,8 @@ async function updateMetadataInFile(
795795
) {
796796
// Read the current file content
797797
const file = await sandboxManager.readFile(filePath);
798-
if (!file || typeof file !== 'string') {
799-
throw new Error('File not found or is not a text file');
798+
if (typeof file !== 'string') {
799+
throw new Error('File is not a text file');
800800
}
801801
const content = file;
802802

@@ -1010,8 +1010,8 @@ export const addSetupTask = async (sandboxManager: SandboxManager) => {
10101010

10111011
export const updatePackageJson = async (sandboxManager: SandboxManager) => {
10121012
const file = await sandboxManager.readFile('./package.json');
1013-
if (!file || typeof file !== 'string') {
1014-
throw new Error('Package.json not found or is not a text file');
1013+
if (typeof file !== 'string') {
1014+
throw new Error('Package.json is not a text file');
10151015
}
10161016
const pkgJson = JSON.parse(file);
10171017

apps/web/client/src/components/store/editor/sandbox/preload-script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export async function injectPreloadScriptIntoLayout(provider: Provider, routerCo
4545
const layoutPath = `${routerConfig.basePath}/${layoutFile.name}`;
4646

4747
const layoutResponse = await provider.readFile({ args: { path: layoutPath } });
48-
if (!layoutResponse.file.content || typeof layoutResponse.file.content !== 'string') {
49-
throw new Error(`Layout file ${layoutPath} has no content`);
48+
if (typeof layoutResponse.file.content !== 'string') {
49+
throw new Error(`Layout file ${layoutPath} is not a text file`);
5050
}
5151

5252
const content = layoutResponse.file.content;

apps/web/client/src/components/store/editor/theme/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,8 @@ export class ThemeManager {
11511151
await Promise.all(
11521152
filesToUpdate.map(async (file) => {
11531153
const fileContent = await this.editorEngine.activeSandbox.readFile(file.path);
1154-
if (!fileContent || typeof fileContent !== 'string') {
1154+
if (typeof fileContent !== 'string') {
1155+
console.error(`File ${file.path} is not a text file`);
11551156
return;
11561157
}
11571158

packages/ai/src/tools/classes/fuzzy-edit-file.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ Make sure there's enough context for the other model to understand where the cha
3333
const fileSystem = await getFileSystem(args.branchId, editorEngine);
3434
const originalFile = await fileSystem.readFile(args.file_path);
3535

36-
if (!originalFile) {
37-
throw new Error('Error reading file');
38-
}
39-
4036
if (typeof originalFile !== 'string') {
4137
throw new Error('Binary files are not supported for editing');
4238
}

packages/ai/src/tools/classes/search-replace-edit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export class SearchReplaceEditTool extends ClientTool {
2222

2323
const fileSystem = await getFileSystem(args.branchId, editorEngine);
2424
const file = await fileSystem.readFile(args.file_path);
25-
if (!file || typeof file !== 'string') {
26-
throw new Error(`Cannot read file ${args.file_path}: file not found or not text`);
25+
if (typeof file !== 'string') {
26+
throw new Error(`Cannot read file ${args.file_path}: file is not text`);
2727
}
2828

2929
let newContent: string;

0 commit comments

Comments
 (0)