Skip to content

Commit

Permalink
fix(amazonq): skip including deleted files for FeatureDev context aws…
Browse files Browse the repository at this point in the history
…#6312

## Problem
When zipping context for /dev, customer build processes (file watchers,
etc.) may delete build artifacts we’ve already enumerated but have not
added to the archive. As a best practice, customers should `.gitignore`
these types of files, but in the event they don't, this has the
potential to block /dev from running.

## Solution
Skip affected files, which are not found when adding to zip context for
Feature Dev.
  • Loading branch information
neilk-aws authored Jan 7, 2025
1 parent d939f2b commit 5e1a94b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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
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

0 comments on commit 5e1a94b

Please sign in to comment.