@@ -124,14 +124,15 @@ public int Execute()
124
124
125
125
Func < ProjectCollection , ProjectInstance > ? projectFactory = null ;
126
126
RunProperties ? cachedRunProperties = null ;
127
+ VirtualProjectBuildingCommand ? virtualCommand = null ;
127
128
if ( ShouldBuild )
128
129
{
129
130
if ( string . Equals ( "true" , launchSettings ? . DotNetRunMessages , StringComparison . OrdinalIgnoreCase ) )
130
131
{
131
132
Reporter . Output . WriteLine ( CliCommandStrings . RunCommandBuilding ) ;
132
133
}
133
134
134
- EnsureProjectIsBuilt ( out projectFactory , out cachedRunProperties ) ;
135
+ EnsureProjectIsBuilt ( out projectFactory , out cachedRunProperties , out virtualCommand ) ;
135
136
}
136
137
else
137
138
{
@@ -143,11 +144,11 @@ public int Execute()
143
144
if ( EntryPointFileFullPath is not null )
144
145
{
145
146
Debug . Assert ( ! ReadCodeFromStdin ) ;
146
- var command = CreateVirtualCommand ( ) ;
147
- command . MarkArtifactsFolderUsed ( ) ;
147
+ virtualCommand = CreateVirtualCommand ( ) ;
148
+ virtualCommand . MarkArtifactsFolderUsed ( ) ;
148
149
149
- var cacheEntry = command . GetPreviousCacheEntry ( ) ;
150
- projectFactory = CanUseRunPropertiesForCscBuiltProgram ( BuildLevel . None , cacheEntry ) ? null : command . CreateProjectInstance ;
150
+ var cacheEntry = virtualCommand . GetPreviousCacheEntry ( ) ;
151
+ projectFactory = CanUseRunPropertiesForCscBuiltProgram ( BuildLevel . None , cacheEntry ) ? null : virtualCommand . CreateProjectInstance ;
151
152
cachedRunProperties = cacheEntry ? . Run ;
152
153
}
153
154
}
@@ -164,7 +165,7 @@ public int Execute()
164
165
}
165
166
166
167
// Send telemetry about the run operation
167
- SendRunTelemetry ( launchSettings , projectFactory , cachedRunProperties ) ;
168
+ SendRunTelemetry ( launchSettings , virtualCommand ) ;
168
169
169
170
// Ignore Ctrl-C for the remainder of the command's execution
170
171
Console . CancelKeyPress += ( sender , e ) => { e . Cancel = true ; } ;
@@ -300,22 +301,23 @@ internal bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel
300
301
}
301
302
}
302
303
303
- private void EnsureProjectIsBuilt ( out Func < ProjectCollection , ProjectInstance > ? projectFactory , out RunProperties ? cachedRunProperties )
304
+ private void EnsureProjectIsBuilt ( out Func < ProjectCollection , ProjectInstance > ? projectFactory , out RunProperties ? cachedRunProperties , out VirtualProjectBuildingCommand ? virtualCommand )
304
305
{
305
306
int buildResult ;
306
307
if ( EntryPointFileFullPath is not null )
307
308
{
308
- var command = CreateVirtualCommand ( ) ;
309
- buildResult = command . Execute ( ) ;
310
- projectFactory = CanUseRunPropertiesForCscBuiltProgram ( command . LastBuild . Level , command . LastBuild . Cache ? . PreviousEntry ) ? null : command . CreateProjectInstance ;
311
- cachedRunProperties = command . LastBuild . Cache ? . CurrentEntry . Run ;
309
+ virtualCommand = CreateVirtualCommand ( ) ;
310
+ buildResult = virtualCommand . Execute ( ) ;
311
+ projectFactory = CanUseRunPropertiesForCscBuiltProgram ( virtualCommand . LastBuild . Level , virtualCommand . LastBuild . Cache ? . PreviousEntry ) ? null : virtualCommand . CreateProjectInstance ;
312
+ cachedRunProperties = virtualCommand . LastBuild . Cache ? . CurrentEntry . Run ;
312
313
}
313
314
else
314
315
{
315
316
Debug . Assert ( ProjectFileFullPath is not null ) ;
316
317
317
318
projectFactory = null ;
318
319
cachedRunProperties = null ;
320
+ virtualCommand = null ;
319
321
buildResult = new RestoringCommand (
320
322
MSBuildArgs . CloneWithExplicitArgs ( [ ProjectFileFullPath , .. MSBuildArgs . OtherMSBuildArgs ] ) ,
321
323
NoRestore ,
@@ -762,30 +764,28 @@ public static ParseResult ModifyParseResultForShorthandProjectOption(ParseResult
762
764
/// <summary>
763
765
/// Sends telemetry about the run operation.
764
766
/// </summary>
765
- /// <param name="launchSettings">Applied launch settings if any</param>
766
- /// <param name="projectFactory">Project factory for file-based apps using MSBuild</param>
767
- /// <param name="cachedRunProperties">Cached run properties if available</param>
768
767
private void SendRunTelemetry (
769
768
ProjectLaunchSettingsModel ? launchSettings ,
770
- Func < ProjectCollection , ProjectInstance > ? projectFactory ,
771
- RunProperties ? cachedRunProperties )
769
+ VirtualProjectBuildingCommand ? virtualCommand )
772
770
{
773
771
try
774
772
{
775
- bool isFileBased = EntryPointFileFullPath is not null ;
776
-
777
- if ( isFileBased )
773
+ if ( virtualCommand != null )
778
774
{
779
- SendFileBasedTelemetry ( launchSettings , projectFactory , cachedRunProperties ) ;
775
+ SendFileBasedTelemetry ( launchSettings , virtualCommand ) ;
780
776
}
781
777
else
782
778
{
783
779
SendProjectBasedTelemetry ( launchSettings ) ;
784
780
}
785
781
}
786
- catch
782
+ catch ( Exception ex )
787
783
{
788
784
// Silently ignore telemetry errors to not affect the run operation
785
+ if ( CommandLoggingContext . IsVerbose )
786
+ {
787
+ Reporter . Verbose . WriteLine ( $ "Failed to send run telemetry: { ex } ") ;
788
+ }
789
789
}
790
790
}
791
791
@@ -794,35 +794,16 @@ private void SendRunTelemetry(
794
794
/// </summary>
795
795
private void SendFileBasedTelemetry (
796
796
ProjectLaunchSettingsModel ? launchSettings ,
797
- Func < ProjectCollection , ProjectInstance > ? projectFactory ,
798
- RunProperties ? cachedRunProperties )
797
+ VirtualProjectBuildingCommand virtualCommand )
799
798
{
800
- var projectIdentifier = RunTelemetry . GetFileBasedIdentifier ( EntryPointFileFullPath ! , Sha256Hasher . Hash ) ;
801
-
802
- var virtualCommand = CreateVirtualCommand ( ) ;
799
+ Debug . Assert ( EntryPointFileFullPath != null ) ;
800
+ var projectIdentifier = RunTelemetry . GetFileBasedIdentifier ( EntryPointFileFullPath , Sha256Hasher . Hash ) ;
801
+
803
802
var directives = virtualCommand . Directives ;
804
-
805
803
var sdkCount = RunTelemetry . CountSdks ( directives ) ;
806
804
var packageReferenceCount = RunTelemetry . CountPackageReferences ( directives ) ;
807
805
var projectReferenceCount = RunTelemetry . CountProjectReferences ( directives ) ;
808
806
var additionalPropertiesCount = RunTelemetry . CountAdditionalProperties ( directives ) ;
809
-
810
- // Determine if MSBuild or Roslyn compiler was used
811
- bool ? usedMSBuild = null ;
812
- bool ? usedRoslynCompiler = null ;
813
-
814
- if ( cachedRunProperties != null )
815
- {
816
- // If we have cached properties, we used the optimized path
817
- usedRoslynCompiler = projectFactory == null ;
818
- usedMSBuild = projectFactory != null ;
819
- }
820
- else if ( ShouldBuild )
821
- {
822
- // Fresh build - check if we used MSBuild optimization
823
- usedMSBuild = ! directives . IsDefaultOrEmpty || projectFactory != null ;
824
- usedRoslynCompiler = directives . IsDefaultOrEmpty && projectFactory == null ;
825
- }
826
807
827
808
RunTelemetry . TrackRunEvent (
828
809
isFileBased : true ,
@@ -834,16 +815,17 @@ private void SendFileBasedTelemetry(
834
815
packageReferenceCount : packageReferenceCount ,
835
816
projectReferenceCount : projectReferenceCount ,
836
817
additionalPropertiesCount : additionalPropertiesCount ,
837
- usedMSBuild : usedMSBuild ,
838
- usedRoslynCompiler : usedRoslynCompiler ) ;
818
+ usedMSBuild : virtualCommand . LastBuild . Level is BuildLevel . All ,
819
+ usedRoslynCompiler : virtualCommand . LastBuild . Level is BuildLevel . Csc ) ;
839
820
}
840
821
841
822
/// <summary>
842
823
/// Builds and sends telemetry data for project-based app runs.
843
824
/// </summary>
844
825
private void SendProjectBasedTelemetry ( ProjectLaunchSettingsModel ? launchSettings )
845
826
{
846
- var projectIdentifier = RunTelemetry . GetProjectBasedIdentifier ( ProjectFileFullPath ! , GetRepositoryRoot ( ) , Sha256Hasher . Hash ) ;
827
+ Debug . Assert ( ProjectFileFullPath != null ) ;
828
+ var projectIdentifier = RunTelemetry . GetProjectBasedIdentifier ( ProjectFileFullPath , GetRepositoryRoot ( ) , Sha256Hasher . Hash ) ;
847
829
848
830
// Get package and project reference counts for project-based apps
849
831
int packageReferenceCount = 0 ;
@@ -859,7 +841,7 @@ private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSetting
859
841
globalProperties [ Constants . MSBuildExtensionsPath ] = AppContext . BaseDirectory ;
860
842
861
843
using var collection = new ProjectCollection ( globalProperties : globalProperties ) ;
862
- var project = collection . LoadProject ( ProjectFileFullPath ! ) . CreateProjectInstance ( ) ;
844
+ var project = collection . LoadProject ( ProjectFileFullPath ) . CreateProjectInstance ( ) ;
863
845
864
846
packageReferenceCount = RunTelemetry . CountPackageReferences ( project ) ;
865
847
projectReferenceCount = RunTelemetry . CountProjectReferences ( project ) ;
0 commit comments