@@ -73,17 +73,18 @@ public static async Task<ITestApplicationBuilder> CreateBuilderAsync(string[] ar
7373 string createBuilderEntryTime = createBuilderStart . ToString ( "HH:mm:ss.fff" , CultureInfo . InvariantCulture ) ;
7474 testApplicationOptions ??= new TestApplicationOptions ( ) ;
7575
76- LaunchAttachDebugger ( systemEnvironment ) ;
76+ SystemConsole systemConsole = new ( ) ;
77+ SystemProcessHandler systemProcess = new ( ) ;
78+ AttachDebuggerIfNeeded ( systemEnvironment , systemConsole , systemProcess ) ;
7779
7880 // First step is to parse the command line from where we get the second input layer.
7981 // The first one should be the env vars handled autonomously by extensions and part of the test platform.
8082 CommandLineParseResult parseResult = CommandLineParser . Parse ( args , systemEnvironment ) ;
8183 TestHostControllerInfo testHostControllerInfo = new ( parseResult ) ;
82- SystemProcessHandler systemProcess = new ( ) ;
8384 CurrentTestApplicationModuleInfo testApplicationModuleInfo = new ( systemEnvironment , systemProcess ) ;
8485
8586 // Create the UnhandledExceptionHandler that will be set inside the TestHostBuilder.
86- LazyInitializer . EnsureInitialized ( ref s_unhandledExceptionHandler , ( ) => new UnhandledExceptionHandler ( systemEnvironment , new SystemConsole ( ) , parseResult . IsOptionSet ( PlatformCommandLineProvider . TestHostControllerPIDOptionKey ) ) ) ;
87+ LazyInitializer . EnsureInitialized ( ref s_unhandledExceptionHandler , ( ) => new UnhandledExceptionHandler ( systemEnvironment , systemConsole , parseResult . IsOptionSet ( PlatformCommandLineProvider . TestHostControllerPIDOptionKey ) ) ) ;
8788 ApplicationStateGuard . Ensure ( s_unhandledExceptionHandler is not null ) ;
8889
8990 // First task is to setup the logger if enabled and we take the info from the command line or env vars.
@@ -234,12 +235,25 @@ public ValueTask DisposeAsync()
234235 public async Task < int > RunAsync ( )
235236 => await _testHost . RunAsync ( ) ;
236237
237- private static void LaunchAttachDebugger ( SystemEnvironment environment )
238+ private static void AttachDebuggerIfNeeded ( SystemEnvironment environment , SystemConsole console , SystemProcessHandler systemProcess )
238239 {
239240 if ( environment . GetEnvironmentVariable ( EnvironmentVariableConstants . TESTINGPLATFORM_LAUNCH_ATTACH_DEBUGGER ) == "1" )
240241 {
241242 Debugger . Launch ( ) ;
242243 }
244+
245+ if ( environment . GetEnvironmentVariable ( EnvironmentVariableConstants . TESTINGPLATFORM_WAIT_ATTACH_DEBUGGER ) == "1" )
246+ {
247+ IProcess currentProcess = systemProcess . GetCurrentProcess ( ) ;
248+ console . WriteLine ( $ "Waiting for debugger to attach... Process Id: { currentProcess . Id } , Name: { currentProcess . Name } ") ;
249+
250+ while ( ! Debugger . IsAttached )
251+ {
252+ Thread . Sleep ( 1000 ) ;
253+ }
254+
255+ Debugger . Break ( ) ;
256+ }
243257 }
244258
245259 /*
0 commit comments