Fix package.json (#1080)

Fixes #554 #1049 
    
<!-- This is an auto-generated description by cubic. -->
---

## Summary by cubic
Stop mutating package.json when reading files. readFileWithCache now
returns raw content so all fields are preserved (e.g., packageManager).
Fixes #554 and #1049.

- **Bug Fixes**
  - Removed package.json "cleaning" logic and the cleanContent helper.
- Return and cache unmodified file content from both fs and
virtualFileSystem.

<!-- End of auto-generated description by cubic. -->
This commit is contained in:
Will Chen
2025-08-25 21:11:27 -07:00
committed by GitHub
parent 72be54e6c5
commit 1dfa0d1004
11 changed files with 12 additions and 38 deletions

View File

@@ -178,7 +178,7 @@ export async function readFileWithCache(
if (virtualFileSystem) {
const virtualContent = await virtualFileSystem.readFile(filePath);
if (virtualContent != null) {
return cleanContent({ content: virtualContent, filePath });
return virtualContent;
}
}
@@ -196,7 +196,7 @@ export async function readFileWithCache(
// Read file and update cache
const rawContent = await fsAsync.readFile(filePath, "utf-8");
const content = cleanContent({ content: rawContent, filePath });
const content = rawContent;
fileContentCache.set(filePath, {
content,
mtime: currentMtime,
@@ -221,32 +221,6 @@ export async function readFileWithCache(
}
}
function cleanContent({
content,
filePath,
}: {
content: string;
filePath: string;
}): string {
// Why are we cleaning package.json?
// 1. It contains unnecessary information for LLM context
// 2. Fields like packageManager cause diffs in e2e test snapshots.
if (path.basename(filePath) === "package.json") {
try {
const { dependencies, devDependencies } = JSON.parse(content);
const cleanPackageJson = {
dependencies,
devDependencies,
};
return JSON.stringify(cleanPackageJson, null, 2);
} catch (error) {
logger.error(`Error cleaning package.json: ${filePath}`, error);
return content;
}
}
return content;
}
/**
* Recursively walk a directory and collect all relevant files
*/