@@ -8,6 +8,7 @@ import { CommitIssue } from '../api/client'
88import { ProcessedSarifResult , runCodacyAnalyze } from '../commands/runCodacyAnalyze'
99import * as path from 'path'
1010import { isCLIInstalled } from '../commands/installAnalysisCLI'
11+ import Logger from '../common/logger'
1112// import * as os from 'os'
1213
1314const patternSeverityToDiagnosticSeverity = ( severity : 'Info' | 'Warning' | 'Error' ) : vscode . DiagnosticSeverity => {
@@ -152,6 +153,22 @@ export class ProblemsDiagnosticCollection implements vscode.Disposable {
152153 this . _collection . set ( document . uri , [ ...apiDiagnostics , ...cliDiagnostics ] )
153154 }
154155
156+ private async retryDeleteFile ( filePath : string , maxAttempts : number = 3 , delayMs : number = 1000 ) : Promise < boolean > {
157+ for ( let attempt = 1 ; attempt <= maxAttempts ; attempt ++ ) {
158+ try {
159+ await vscode . workspace . fs . delete ( vscode . Uri . file ( filePath ) )
160+ return true
161+ } catch ( err ) {
162+ if ( attempt === maxAttempts ) {
163+ Logger . error ( `Failed to delete temporary file after ${ maxAttempts } attempts:` , ( err as Error ) . message )
164+ return false
165+ }
166+ await new Promise ( ( resolve ) => setTimeout ( resolve , delayMs ) )
167+ }
168+ }
169+ return false
170+ }
171+
155172 private async runAnalysisAndUpdateDiagnostics ( document : vscode . TextDocument ) {
156173 // TODO: check if Codacy CLI is available and initialized
157174
@@ -214,11 +231,10 @@ export class ProblemsDiagnosticCollection implements vscode.Disposable {
214231 this . _isAnalysisRunning = false
215232
216233 if ( document . isDirty ) {
217- // Remove the temporary file after analysis
218- try {
219- await vscode . workspace . fs . delete ( vscode . Uri . file ( pathToFile ) )
220- } catch ( err ) {
221- console . error ( 'Failed to delete temporary file:' , err )
234+ // Remove the temporary file after analysis with retries
235+ const deleted = await this . retryDeleteFile ( pathToFile )
236+ if ( ! deleted ) {
237+ Logger . error ( 'Failed to delete temporary file after all attempts:' , pathToFile )
222238 }
223239 }
224240 }
0 commit comments