@@ -12,7 +12,7 @@ import { Uri } from 'vscode'
1212import { GitIgnoreFilter } from './gitignore'
1313
1414import AdmZip from 'adm-zip'
15- import { PrepareRepoFailedError } from '../errors'
15+ import { ContentLengthError , PrepareRepoFailedError } from '../errors'
1616import { getLogger } from '../../shared/logger/logger'
1717import { maxFileSizeBytes } from '../limits'
1818import { createHash } from 'crypto'
@@ -21,6 +21,7 @@ import { ToolkitError } from '../../shared/errors'
2121import { AmazonqCreateUpload , Metric } from '../../shared/telemetry/telemetry'
2222import { TelemetryHelper } from './telemetryHelper'
2323import { sanitizeFilename } from '../../shared/utilities/textUtilities'
24+ import { maxRepoSize } from '../constants'
2425
2526export function getExcludePattern ( additionalPatterns : string [ ] = [ ] ) {
2627 const globAlwaysExcludedDirs = getGlobDirExcludedPatterns ( ) . map ( pattern => `**/${ pattern } /*` )
@@ -94,6 +95,8 @@ export async function collectFiles(
9495 return prefix === '' ? path : `${ prefix } /${ path } `
9596 }
9697
98+ let totalSize = 0
99+ const decoder = new TextDecoder ( 'utf8' )
97100 for ( const rootPath of sourcePaths ) {
98101 const allFiles = await vscode . workspace . findFiles (
99102 new vscode . RelativePattern ( rootPath , '**' ) ,
@@ -103,7 +106,8 @@ export async function collectFiles(
103106
104107 for ( const file of files ) {
105108 try {
106- const fileContent = await SystemUtilities . readFile ( file , new TextDecoder ( 'utf8' , { fatal : true } ) )
109+ const fileStat = await vscode . workspace . fs . stat ( file )
110+ const fileContent = await SystemUtilities . readFile ( file , decoder )
107111 const relativePath = getWorkspaceRelativePath ( file . fsPath , { workspaceFolders } )
108112
109113 if ( relativePath ) {
@@ -115,9 +119,13 @@ export async function collectFiles(
115119 zipFilePath : prefixWithFolderPrefix ( relativePath . workspaceFolder , relativePath . relativePath ) ,
116120 } )
117121 }
122+ totalSize += fileStat . size
123+ if ( totalSize > maxRepoSize ) {
124+ throw new ContentLengthError ( )
125+ }
118126 } catch ( error ) {
119127 getLogger ( ) . debug (
120- `featureDev: Failed to read file ${ file . fsPath } when collecting repository: ${ error } . Skipping the file`
128+ `featureDev: Failed to read file ${ file . fsPath } when collecting repository. Skipping the file`
121129 )
122130 }
123131 }
0 commit comments