Skip to content

Commit d5c9d52

Browse files
committed
Refactor file operations to exclusively use the VS Code vscode.workspace.fs API instead of Node's fs module for asynchronous file handling
1 parent fae1c7a commit d5c9d52

1 file changed

Lines changed: 66 additions & 41 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/utils/file-operations.ts‎

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
3-
import * as fs from 'fs'
43
import { create_safe_path } from '@/utils/path-sanitizer'
54
import { dictionary } from '@shared/constants/dictionary'
65
import { Logger } from '@shared/utils/logger'
76
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
87

8+
// Helper to replace fs.existsSync with a non-throwing async version
9+
async function uri_exists(uri: vscode.Uri): Promise<boolean> {
10+
try {
11+
await vscode.workspace.fs.stat(uri)
12+
return true
13+
} catch {
14+
return false
15+
}
16+
}
17+
918
export const remove_directory_if_empty = async (params: {
1019
dir_path: string
1120
workspace_root: string
@@ -22,22 +31,23 @@ export const remove_directory_if_empty = async (params: {
2231
}
2332

2433
try {
25-
if (
26-
fs.existsSync(normalized_dir) &&
27-
fs.lstatSync(normalized_dir).isDirectory()
28-
) {
29-
const files = fs.readdirSync(normalized_dir)
30-
if (files.length == 0) {
31-
fs.rmdirSync(normalized_dir)
32-
Logger.info({
33-
function_name: 'remove_directory_if_empty',
34-
message: 'Removed empty directory',
35-
data: { dir_path: normalized_dir }
36-
})
37-
await remove_directory_if_empty({
38-
dir_path: path.dirname(normalized_dir),
39-
workspace_root: params.workspace_root
40-
})
34+
const dir_uri = vscode.Uri.file(normalized_dir)
35+
if (await uri_exists(dir_uri)) {
36+
const stat = await vscode.workspace.fs.stat(dir_uri)
37+
if (stat.type === vscode.FileType.Directory) {
38+
const files = await vscode.workspace.fs.readDirectory(dir_uri)
39+
if (files.length == 0) {
40+
await vscode.workspace.fs.delete(dir_uri)
41+
Logger.info({
42+
function_name: 'remove_directory_if_empty',
43+
message: 'Removed empty directory',
44+
data: { dir_path: normalized_dir }
45+
})
46+
await remove_directory_if_empty({
47+
dir_path: path.dirname(normalized_dir),
48+
workspace_root: params.workspace_root
49+
})
50+
}
4151
}
4252
}
4353
} catch (error) {
@@ -106,31 +116,37 @@ export const create_file_if_needed = async (params: {
106116
})
107117
return false
108118
}
119+
const file_uri = vscode.Uri.file(safe_path)
120+
const directory_uri = vscode.Uri.file(path.dirname(safe_path))
109121

110-
const directory = path.dirname(safe_path)
111-
if (!fs.existsSync(directory)) {
122+
if (!(await uri_exists(directory_uri))) {
112123
try {
113-
fs.mkdirSync(directory, { recursive: true })
124+
await vscode.workspace.fs.createDirectory(directory_uri)
114125
Logger.info({
115126
function_name: 'create_file_if_needed',
116127
message: 'Directory created',
117-
data: directory
128+
data: directory_uri.fsPath
118129
})
119130
} catch (error) {
120131
Logger.error({
121132
function_name: 'create_file_if_needed',
122133
message: 'Failed to create directory',
123-
data: { directory, error }
134+
data: { directory: directory_uri.fsPath, error }
124135
})
125136
vscode.window.showErrorMessage(
126-
dictionary.error_message.FAILED_TO_CREATE_DIRECTORY(directory)
137+
dictionary.error_message.FAILED_TO_CREATE_DIRECTORY(
138+
directory_uri.fsPath
139+
)
127140
)
128141
return false
129142
}
130143
}
131144

132145
try {
133-
fs.writeFileSync(safe_path, params.content)
146+
await vscode.workspace.fs.writeFile(
147+
file_uri,
148+
Buffer.from(params.content, 'utf8')
149+
)
134150
Logger.info({
135151
function_name: 'create_file_if_needed',
136152
message: 'File created',
@@ -176,7 +192,8 @@ const relocate_file = async (params: {
176192
return false
177193
}
178194

179-
if (!fs.existsSync(old_safe_path)) {
195+
const old_uri = vscode.Uri.file(old_safe_path)
196+
if (!(await uri_exists(old_uri))) {
180197
Logger.warn({
181198
function_name: 'relocate_file',
182199
message: 'Source file does not exist for relocation',
@@ -186,13 +203,13 @@ const relocate_file = async (params: {
186203
}
187204

188205
// Create directory for new path if needed
189-
const new_dir = path.dirname(new_safe_path)
190-
if (!fs.existsSync(new_dir)) {
191-
fs.mkdirSync(new_dir, { recursive: true })
206+
const new_uri = vscode.Uri.file(new_safe_path)
207+
const new_dir_uri = vscode.Uri.file(path.dirname(new_safe_path))
208+
if (!(await uri_exists(new_dir_uri))) {
209+
await vscode.workspace.fs.createDirectory(new_dir_uri)
192210
}
193211

194212
// Close any open editors for the old file
195-
const old_uri = vscode.Uri.file(old_safe_path)
196213
const text_editors = vscode.window.visibleTextEditors.filter(
197214
(editor) => editor.document.uri.toString() == old_uri.toString()
198215
)
@@ -204,7 +221,7 @@ const relocate_file = async (params: {
204221
await vscode.commands.executeCommand('workbench.action.closeActiveEditor')
205222
}
206223

207-
fs.renameSync(old_safe_path, new_safe_path)
224+
await vscode.workspace.fs.rename(old_uri, new_uri, { overwrite: true })
208225

209226
await remove_directory_if_empty({
210227
dir_path: path.dirname(old_safe_path),
@@ -323,13 +340,13 @@ export const undo_files = async (params: {
323340
console.error(`Cannot undo file with unsafe path: ${state.file_path}`)
324341
continue // Skip this file
325342
}
343+
let file_uri = vscode.Uri.file(safe_path)
326344

327345
if (state.is_new && !state.file_path_to_restore) {
328-
if (fs.existsSync(safe_path)) {
346+
if (await uri_exists(file_uri)) {
329347
// Close any open editors for this file before deleting
330-
const uri = vscode.Uri.file(safe_path)
331348
const text_editors = vscode.window.visibleTextEditors.filter(
332-
(editor) => editor.document.uri.toString() === uri.toString()
349+
(editor) => editor.document.uri.toString() === file_uri.toString()
333350
)
334351

335352
// Close all tabs for this file across all editor groups
@@ -361,7 +378,7 @@ export const undo_files = async (params: {
361378
}
362379

363380
try {
364-
fs.unlinkSync(safe_path)
381+
await vscode.workspace.fs.delete(file_uri)
365382
Logger.info({
366383
function_name: 'undo_files',
367384
message: 'New file deleted',
@@ -384,7 +401,7 @@ export const undo_files = async (params: {
384401
}
385402
} else {
386403
if (state.file_path_to_restore) {
387-
if (safe_path && fs.existsSync(safe_path)) {
404+
if (safe_path && (await uri_exists(vscode.Uri.file(safe_path)))) {
388405
await relocate_file({
389406
old_path: state.file_path,
390407
new_path: state.file_path_to_restore,
@@ -397,15 +414,20 @@ export const undo_files = async (params: {
397414
)
398415
}
399416
if (!safe_path) continue
417+
file_uri = vscode.Uri.file(safe_path)
400418
// For existing files that were modified, restore original content.
401419
// This also handles files that were deleted (by recreating them).
402-
if (!fs.existsSync(safe_path)) {
420+
if (!(await uri_exists(file_uri))) {
403421
try {
404-
const dir = path.dirname(safe_path)
405-
if (!fs.existsSync(dir)) {
406-
fs.mkdirSync(dir, { recursive: true })
422+
const dir_uri = vscode.Uri.file(path.dirname(safe_path))
423+
if (!(await uri_exists(dir_uri))) {
424+
await vscode.workspace.fs.createDirectory(dir_uri)
407425
}
408-
fs.writeFileSync(safe_path, state.content)
426+
await vscode.workspace.fs.writeFile(
427+
file_uri,
428+
Buffer.from(state.content, 'utf8')
429+
)
430+
409431
Logger.info({
410432
function_name: 'undo_files',
411433
message: 'Recreated deleted file.',
@@ -449,7 +471,10 @@ export const undo_files = async (params: {
449471

450472
await document.save()
451473
} else {
452-
fs.writeFileSync(safe_path, state.content)
474+
await vscode.workspace.fs.writeFile(
475+
file_uri,
476+
Buffer.from(state.content, 'utf8')
477+
)
453478
}
454479
Logger.info({
455480
function_name: 'undo_files',

0 commit comments

Comments
 (0)