Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Bug Fix",
"description": "Amazon Q /dev: Fix issue when files are deleted while preparing context"
}
25 changes: 22 additions & 3 deletions packages/core/src/amazonqFeatureDev/util/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getLogger } from '../../shared/logger/logger'
import { maxFileSizeBytes } from '../limits'
import { createHash } from 'crypto'
import { CurrentWsFolders } from '../types'
import { ToolkitError } from '../../shared/errors'
import { hasCode, ToolkitError } from '../../shared/errors'
import { AmazonqCreateUpload, Span, telemetry as amznTelemetry } from '../../shared/telemetry/telemetry'
import { TelemetryHelper } from './telemetryHelper'
import { maxRepoSizeBytes } from '../constants'
Expand All @@ -39,7 +39,16 @@ export async function prepareRepoData(
const ignoredExtensionMap = new Map<string, number>()

for (const file of files) {
const fileSize = (await fs.stat(file.fileUri)).size
let fileSize
try {
fileSize = (await fs.stat(file.fileUri)).size
} catch (error) {
if (hasCode(error) && error.code === 'ENOENT') {
// No-op: Skip if file does not exist
continue
}
throw error
}
const isCodeFile_ = isCodeFile(file.relativeFilePath)

if (fileSize >= maxFileSizeBytes || !isCodeFile_) {
Expand All @@ -58,7 +67,17 @@ export async function prepareRepoData(
totalBytes += fileSize

const zipFolderPath = path.dirname(file.zipFilePath)
zip.addLocalFile(file.fileUri.fsPath, zipFolderPath)

try {
zip.addLocalFile(file.fileUri.fsPath, zipFolderPath)
} catch (error) {
if (error instanceof Error && error.message.includes('File not found')) {
// No-op: Skip if file was deleted or does not exist
// Reference: https://github.com/cthackers/adm-zip/blob/1cd32f7e0ad3c540142a76609bb538a5cda2292f/adm-zip.js#L296-L321
Copy link
Contributor

@justinmk3 justinmk3 Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it set a code ? try hasCode

function hasCode<T>(error: T): error is T & { code: string } {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continue
}
throw error
}
}

const iterator = ignoredExtensionMap.entries()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shared/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export function isAwsError(error: unknown): error is AWSError & { error_descript
return error instanceof Error && hasCode(error) && hasTime(error)
}

function hasCode<T>(error: T): error is T & { code: string } {
export function hasCode<T>(error: T): error is T & { code: string } {
return typeof (error as { code?: unknown }).code === 'string'
}

Expand Down
Loading