Skip to content

Commit 3e079d6

Browse files
committed
Fix:read file encoding
1 parent 360b0e7 commit 3e079d6

File tree

8 files changed

+793
-40
lines changed

8 files changed

+793
-40
lines changed

pnpm-lock.yaml

Lines changed: 42 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/tools/applyDiffTool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path"
2-
import fs from "fs/promises"
2+
import { readFileWithEncoding } from "../../integrations/misc/readFileWithEncoding"
33

44
import { TelemetryService } from "@roo-code/telemetry"
55

@@ -86,7 +86,7 @@ export async function applyDiffTool(
8686
return
8787
}
8888

89-
let originalContent: string | null = await fs.readFile(absolutePath, "utf-8")
89+
let originalContent: string | null = (await readFileWithEncoding(absolutePath)).content
9090

9191
// Apply the diff to the original content
9292
const diffResult = (await cline.diffStrategy?.applyDiff(

src/core/tools/insertContentTool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import delay from "delay"
2-
import fs from "fs/promises"
2+
import { readFileWithEncoding } from "../../integrations/misc/readFileWithEncoding"
33
import path from "path"
44

55
import { getReadablePath } from "../../utils/path"
@@ -89,7 +89,7 @@ export async function insertContentTool(
8989
cline.consecutiveMistakeCount = 0
9090

9191
// Read the file
92-
const fileContent = await fs.readFile(absolutePath, "utf8")
92+
const fileContent = (await readFileWithEncoding(absolutePath)).content
9393
cline.diffViewProvider.editType = "modify"
9494
cline.diffViewProvider.originalContent = fileContent
9595
const lines = fileContent.split("\n")

src/core/tools/searchAndReplaceTool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Core Node.js imports
22
import path from "path"
3-
import fs from "fs/promises"
3+
import { readFileWithEncoding } from "../../integrations/misc/readFileWithEncoding"
44
import delay from "delay"
55

66
// Internal imports
@@ -143,7 +143,7 @@ export async function searchAndReplaceTool(
143143
// Read and process file content
144144
let fileContent: string
145145
try {
146-
fileContent = await fs.readFile(absolutePath, "utf-8")
146+
fileContent = (await readFileWithEncoding(absolutePath)).content
147147
} catch (error) {
148148
cline.consecutiveMistakeCount++
149149
cline.recordToolError("search_and_replace")

src/integrations/editor/DiffViewProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { formatResponse } from "../../core/prompts/responses"
1111
import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics"
1212
import { ClineSayTool } from "../../shared/ExtensionMessage"
1313
import { Task } from "../../core/task/Task"
14+
import { readFileWithEncoding } from "../misc/readFileWithEncoding"
1415

1516
import { DecorationController } from "./DecorationController"
1617

@@ -59,7 +60,7 @@ export class DiffViewProvider {
5960
this.preDiagnostics = vscode.languages.getDiagnostics()
6061

6162
if (fileExists) {
62-
this.originalContent = await fs.readFile(absolutePath, "utf-8")
63+
this.originalContent = (await readFileWithEncoding(absolutePath)).content
6364
} else {
6465
this.originalContent = ""
6566
}

src/integrations/misc/extract-text.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "path"
33
import pdf from "pdf-parse/lib/pdf-parse"
44
import mammoth from "mammoth"
55
import fs from "fs/promises"
6-
import { isBinaryFile } from "isbinaryfile"
6+
import { readFileWithEncoding } from "./readFileWithEncoding"
77

88
async function extractTextFromPDF(filePath: string): Promise<string> {
99
const dataBuffer = await fs.readFile(filePath)
@@ -61,12 +61,11 @@ export async function extractTextFromFile(filePath: string): Promise<string> {
6161
return extractor(filePath)
6262
}
6363

64-
// Handle other files
65-
const isBinary = await isBinaryFile(filePath).catch(() => false)
66-
67-
if (!isBinary) {
68-
return addLineNumbers(await fs.readFile(filePath, "utf8"))
69-
} else {
64+
// Handle other files using readFileWithEncoding with binary detection
65+
try {
66+
const { content } = await readFileWithEncoding(filePath, { acceptTextOnly: true })
67+
return addLineNumbers(content)
68+
} catch (error) {
7069
throw new Error(`Cannot read text for file type: ${fileExtension}`)
7170
}
7271
}

0 commit comments

Comments
 (0)