aws / aws-toolkit-vscode

Amazon Q, CodeCatalyst, Local Lambda debug, SAM/CFN syntax, ECS Terminal, AWS resources
https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.amazon-q-vscode
Apache License 2.0
1.49k stars 418 forks source link

feature: get size of workspace utility #5264

Closed 32teeth closed 2 months ago

32teeth commented 3 months ago

Problem

Sometimes it takes long for the collectFiles to exit with an error if more than 200MB, depending on number of files

Solution

[!TIP] It now collects and returns the files to reduce redundancy in collectFiles


export async function getWorkspaceSize(sourcePaths: string[], respectGitIgnore: boolean = true): Promise<vscode.Uri[]> {
let totalSizeBytes = 0
let collection: vscode.Uri[] = []
for (const rootPath of sourcePaths) {
    const allFiles = await vscode.workspace.findFiles(
        new vscode.RelativePattern(rootPath, '**'),
        getExcludePattern()
    )

    const files = respectGitIgnore ? await filterOutGitignoredFiles(rootPath, allFiles) : allFiles
    const fileStats = await Promise.all(files.map(file => fsCommon.stat(file)))

    totalSizeBytes += fileStats.reduce((acc, fileStat) => acc + fileStat.size, 0)
    collection = collection.concat(files)
}

if (totalSizeBytes > maxRepoSizeBytes) {
    throw new ToolkitError(
        'The project you have selected for source code is too large to use as context. Please select a different folder to use',
        { code: 'ContentLengthError' }
    )
}

return collection

}

// collect files excerpt const collection = await getWorkspaceSize(sourcePaths, respectGitIgnore) for (const file of collection) { const relativePath = getWorkspaceRelativePath(file.fsPath, { workspaceFolders }) if (!relativePath) { continue }

    const fileContent = await readFile(file)
    if (fileContent === undefined) {
        continue
    }

    storage.push({
        workspaceFolder: relativePath.workspaceFolder,
        relativeFilePath: relativePath.relativePath,
        fileUri: file,
        fileContent: fileContent,
        zipFilePath: prefixWithFolderPrefix(relativePath.workspaceFolder, relativePath.relativePath),
    })
}


## License

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
32teeth commented 3 months ago

Woot 14/14 checks passed #firsttry

32teeth commented 2 months ago

Thanks for all the intake comments.

I did observe there was an unused utility for something similar, will investigate on how to implement

As per the double cost. Absolutely, worth a look on how to reduce cost on the fs.stat

Ideally I can retrofit it to

  1. Accept a max size
  2. If max size is exceeded, return error
  3. If within threshold, return the files to reduce cost of next call

Great feedback, thank you

justinmk3 commented 2 months ago

Set to "draft" since this still has lots of work needed. Un-draft it when it's ready for review.

32teeth commented 2 months ago

Closing for now