Skip to content

Commit 685b86f

Browse files
committed
Improve
1 parent b1f782e commit 685b86f

File tree

2 files changed

+34
-52
lines changed

2 files changed

+34
-52
lines changed

src/Cli/dotnet/Commands/Run/RunCommand.cs

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,15 @@ public int Execute()
124124

125125
Func<ProjectCollection, ProjectInstance>? projectFactory = null;
126126
RunProperties? cachedRunProperties = null;
127+
VirtualProjectBuildingCommand? virtualCommand = null;
127128
if (ShouldBuild)
128129
{
129130
if (string.Equals("true", launchSettings?.DotNetRunMessages, StringComparison.OrdinalIgnoreCase))
130131
{
131132
Reporter.Output.WriteLine(CliCommandStrings.RunCommandBuilding);
132133
}
133134

134-
EnsureProjectIsBuilt(out projectFactory, out cachedRunProperties);
135+
EnsureProjectIsBuilt(out projectFactory, out cachedRunProperties, out virtualCommand);
135136
}
136137
else
137138
{
@@ -143,11 +144,11 @@ public int Execute()
143144
if (EntryPointFileFullPath is not null)
144145
{
145146
Debug.Assert(!ReadCodeFromStdin);
146-
var command = CreateVirtualCommand();
147-
command.MarkArtifactsFolderUsed();
147+
virtualCommand = CreateVirtualCommand();
148+
virtualCommand.MarkArtifactsFolderUsed();
148149

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;
151152
cachedRunProperties = cacheEntry?.Run;
152153
}
153154
}
@@ -164,7 +165,7 @@ public int Execute()
164165
}
165166

166167
// Send telemetry about the run operation
167-
SendRunTelemetry(launchSettings, projectFactory, cachedRunProperties);
168+
SendRunTelemetry(launchSettings, virtualCommand);
168169

169170
// Ignore Ctrl-C for the remainder of the command's execution
170171
Console.CancelKeyPress += (sender, e) => { e.Cancel = true; };
@@ -300,22 +301,23 @@ internal bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel
300301
}
301302
}
302303

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)
304305
{
305306
int buildResult;
306307
if (EntryPointFileFullPath is not null)
307308
{
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;
312313
}
313314
else
314315
{
315316
Debug.Assert(ProjectFileFullPath is not null);
316317

317318
projectFactory = null;
318319
cachedRunProperties = null;
320+
virtualCommand = null;
319321
buildResult = new RestoringCommand(
320322
MSBuildArgs.CloneWithExplicitArgs([ProjectFileFullPath, .. MSBuildArgs.OtherMSBuildArgs]),
321323
NoRestore,
@@ -762,30 +764,28 @@ public static ParseResult ModifyParseResultForShorthandProjectOption(ParseResult
762764
/// <summary>
763765
/// Sends telemetry about the run operation.
764766
/// </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>
768767
private void SendRunTelemetry(
769768
ProjectLaunchSettingsModel? launchSettings,
770-
Func<ProjectCollection, ProjectInstance>? projectFactory,
771-
RunProperties? cachedRunProperties)
769+
VirtualProjectBuildingCommand? virtualCommand)
772770
{
773771
try
774772
{
775-
bool isFileBased = EntryPointFileFullPath is not null;
776-
777-
if (isFileBased)
773+
if (virtualCommand != null)
778774
{
779-
SendFileBasedTelemetry(launchSettings, projectFactory, cachedRunProperties);
775+
SendFileBasedTelemetry(launchSettings, virtualCommand);
780776
}
781777
else
782778
{
783779
SendProjectBasedTelemetry(launchSettings);
784780
}
785781
}
786-
catch
782+
catch (Exception ex)
787783
{
788784
// 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+
}
789789
}
790790
}
791791

@@ -794,35 +794,16 @@ private void SendRunTelemetry(
794794
/// </summary>
795795
private void SendFileBasedTelemetry(
796796
ProjectLaunchSettingsModel? launchSettings,
797-
Func<ProjectCollection, ProjectInstance>? projectFactory,
798-
RunProperties? cachedRunProperties)
797+
VirtualProjectBuildingCommand virtualCommand)
799798
{
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+
803802
var directives = virtualCommand.Directives;
804-
805803
var sdkCount = RunTelemetry.CountSdks(directives);
806804
var packageReferenceCount = RunTelemetry.CountPackageReferences(directives);
807805
var projectReferenceCount = RunTelemetry.CountProjectReferences(directives);
808806
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-
}
826807

827808
RunTelemetry.TrackRunEvent(
828809
isFileBased: true,
@@ -834,16 +815,17 @@ private void SendFileBasedTelemetry(
834815
packageReferenceCount: packageReferenceCount,
835816
projectReferenceCount: projectReferenceCount,
836817
additionalPropertiesCount: additionalPropertiesCount,
837-
usedMSBuild: usedMSBuild,
838-
usedRoslynCompiler: usedRoslynCompiler);
818+
usedMSBuild: virtualCommand.LastBuild.Level is BuildLevel.All,
819+
usedRoslynCompiler: virtualCommand.LastBuild.Level is BuildLevel.Csc);
839820
}
840821

841822
/// <summary>
842823
/// Builds and sends telemetry data for project-based app runs.
843824
/// </summary>
844825
private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSettings)
845826
{
846-
var projectIdentifier = RunTelemetry.GetProjectBasedIdentifier(ProjectFileFullPath!, GetRepositoryRoot(), Sha256Hasher.Hash);
827+
Debug.Assert(ProjectFileFullPath != null);
828+
var projectIdentifier = RunTelemetry.GetProjectBasedIdentifier(ProjectFileFullPath, GetRepositoryRoot(), Sha256Hasher.Hash);
847829

848830
// Get package and project reference counts for project-based apps
849831
int packageReferenceCount = 0;
@@ -859,7 +841,7 @@ private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSetting
859841
globalProperties[Constants.MSBuildExtensionsPath] = AppContext.BaseDirectory;
860842

861843
using var collection = new ProjectCollection(globalProperties: globalProperties);
862-
var project = collection.LoadProject(ProjectFileFullPath!).CreateProjectInstance();
844+
var project = collection.LoadProject(ProjectFileFullPath).CreateProjectInstance();
863845

864846
packageReferenceCount = RunTelemetry.CountPackageReferences(project);
865847
projectReferenceCount = RunTelemetry.CountProjectReferences(project);

test/dotnet.Tests/CommandTests/Run/RunTelemetryTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public void GetProjectBasedIdentifier_ReturnsSameHashForSamePath()
5252
public void GetProjectBasedIdentifier_UsesRelativePathWhenRepoRootProvided()
5353
{
5454
// Arrange
55-
var repoRoot = "/repo/root";
56-
var projectPath = "/repo/root/src/project.csproj";
57-
var expectedRelativePath = "src/project.csproj";
55+
var repoRoot = "/repo/root".Replace('/', Path.DirectorySeparatorChar);
56+
var projectPath = "/repo/root/src/project.csproj".Replace('/', Path.DirectorySeparatorChar);
57+
var expectedRelativePath = "src/project.csproj".Replace('/', Path.DirectorySeparatorChar);
5858

5959
// Act
6060
var hashWithRepo = RunTelemetry.GetProjectBasedIdentifier(projectPath, repoRoot);
@@ -279,4 +279,4 @@ public void TrackRunEvent_WithDefaultLaunchProfile_MarksTelemetryCorrectly()
279279
TelemetryEventEntry.EntryPosted -= handler;
280280
}
281281
}
282-
}
282+
}

0 commit comments

Comments
 (0)