1- // Copyright (c) .NET Foundation. All rights reserved.
1+ // Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5+ using System . Diagnostics ;
56using System . IO ;
7+ using System . Runtime . InteropServices ;
68
79namespace Microsoft . CodeDom . Providers . DotNetCompilerPlatform {
810 internal sealed class CompilerSettings : ICompilerSettings {
@@ -34,9 +36,33 @@ int ICompilerSettings.CompilerServerTimeToLive {
3436
3537 internal static class CompilationSettingsHelper {
3638 private const int DefaultCompilerServerTTL = 10 ; //seconds
39+ private const int DefaultCompilerServerTTLInDevEnvironment = 60 * 15 ;
40+ private const string DevEnvironmentVariableName = "DEV_ENVIRONMENT" ;
41+ private const string DebuggerAttachedEnvironmentVariable = "IN_DEBUG_MODE" ;
3742
38- private static ICompilerSettings _csc = new CompilerSettings ( CompilerFullPath ( @"bin\roslyn\csc.exe" ) , DefaultCompilerServerTTL ) ;
39- private static ICompilerSettings _vb = new CompilerSettings ( CompilerFullPath ( @"bin\roslyn\vbc.exe" ) , DefaultCompilerServerTTL ) ;
43+ private static ICompilerSettings _csc ;
44+ private static ICompilerSettings _vb ;
45+
46+ static CompilationSettingsHelper ( ) {
47+ var ttl = DefaultCompilerServerTTL ;
48+ var devEnvironmentSetting = Environment . GetEnvironmentVariable ( DevEnvironmentVariableName , EnvironmentVariableTarget . Process ) ;
49+ var debuggerAttachedEnvironmentSetting = Environment . GetEnvironmentVariable ( DebuggerAttachedEnvironmentVariable ,
50+ EnvironmentVariableTarget . Process ) ;
51+ var isDebuggerAttached = IsDebuggerAttached ;
52+
53+ if ( ! string . IsNullOrEmpty ( devEnvironmentSetting ) ||
54+ ! string . IsNullOrEmpty ( debuggerAttachedEnvironmentSetting ) ||
55+ isDebuggerAttached ) {
56+ ttl = DefaultCompilerServerTTLInDevEnvironment ;
57+ }
58+
59+ _csc = new CompilerSettings ( CompilerFullPath ( @"bin\roslyn\csc.exe" ) , ttl ) ;
60+ _vb = new CompilerSettings ( CompilerFullPath ( @"bin\roslyn\vbc.exe" ) , ttl ) ;
61+
62+ if ( isDebuggerAttached ) {
63+ Environment . SetEnvironmentVariable ( DebuggerAttachedEnvironmentVariable , "1" , EnvironmentVariableTarget . Process ) ;
64+ }
65+ }
4066
4167 public static ICompilerSettings CSC2 {
4268 get {
@@ -54,5 +80,14 @@ private static string CompilerFullPath(string relativePath) {
5480 string compilerFullPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , relativePath ) ;
5581 return compilerFullPath ;
5682 }
83+
84+ private static bool IsDebuggerAttached {
85+ get {
86+ return IsDebuggerPresent ( ) || Debugger . IsAttached ;
87+ }
88+ }
89+
90+ [ DllImport ( "kernel32.dll" ) ]
91+ private extern static bool IsDebuggerPresent ( ) ;
5792 }
5893}
0 commit comments