@@ -82,6 +82,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
8282 }
8383 }
8484
85+ var uploadRepositoryChangesTask = Task . CompletedTask ;
86+
8587 // Set Agentless configuration from the command line options
8688 ciVisibilitySettings . SetAgentlessConfiguration ( agentless , apiKey , applicationKey , ciVisibilitySettings . AgentlessUrl ) ;
8789
@@ -103,7 +105,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
103105 var lazyItrClient = new Lazy < IntelligentTestRunnerClient > ( ( ) => new ( CIEnvironmentValues . Instance . WorkspacePath , ciVisibilitySettings ) ) ;
104106 if ( ciVisibilitySettings . GitUploadEnabled != false || ciVisibilitySettings . IntelligentTestRunnerEnabled )
105107 {
106- await lazyItrClient . Value . UploadRepositoryChangesAsync ( ) . ConfigureAwait ( false ) ;
108+ // If we are in git upload only then we can defer the await until the child command exits.
109+ uploadRepositoryChangesTask = Task . Run ( ( ) => lazyItrClient . Value . UploadRepositoryChangesAsync ( ) ) ;
107110
108111 // Once the repository has been uploaded we switch off the git upload in children processes
109112 profilerEnvironmentVariables [ Configuration . ConfigurationKeys . CIVisibility . GitUploadEnabled ] = "0" ;
@@ -241,9 +244,16 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
241244 {
242245 AnsiConsole . WriteLine ( "Running: " + command ) ;
243246
244- if ( Program . CallbackForTests != null )
247+ if ( ciVisibilitySettings . IntelligentTestRunnerEnabled || Program . CallbackForTests is not null )
248+ {
249+ // Awaiting git repository task before running the command if ITR is enabled.
250+ Log . Debug ( "RunCiCommand: Awaiting for the Git repository upload." ) ;
251+ await uploadRepositoryChangesTask . ConfigureAwait ( false ) ;
252+ }
253+
254+ if ( Program . CallbackForTests is { } callbackForTests )
245255 {
246- Program . CallbackForTests ( program , arguments , profilerEnvironmentVariables ) ;
256+ callbackForTests ( program , arguments , profilerEnvironmentVariables ) ;
247257 return 0 ;
248258 }
249259
@@ -257,6 +267,14 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
257267 exitCode = Utils . RunProcess ( processInfo , _applicationContext . TokenSource . Token ) ;
258268 session ? . SetTag ( TestTags . CommandExitCode , exitCode ) ;
259269 Log . Debug < int > ( "RunCiCommand: Finished with exit code: {Value}" , exitCode ) ;
270+
271+ if ( ! ciVisibilitySettings . IntelligentTestRunnerEnabled )
272+ {
273+ // Awaiting git repository task after running the command if ITR is disabled.
274+ Log . Debug ( "RunCiCommand: Awaiting for the Git repository upload." ) ;
275+ await uploadRepositoryChangesTask . ConfigureAwait ( false ) ;
276+ }
277+
260278 return exitCode ;
261279 }
262280 catch ( Exception ex )
0 commit comments