Skip to content

Commit

Permalink
Notify MSBuild TerminalLogger of project cache plugin starting
Browse files Browse the repository at this point in the history
  • Loading branch information
mruxmohan4 committed Apr 17, 2024
1 parent 72d49c3 commit ffad290
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/MSBuild/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,12 @@
<data name="PossiblyOmittedMaxCPUSwitch" xml:space="preserve">
<value>Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.</value>
</data>
<data name="ProjectCacheHitWithOutputs" xml:space="preserve">
<value>{0} -> Cache Hit</value>
<comment>
{StrBegin="{0} -> "}LOCALIZATION: This string is used to indicate progress and matches the format for a log message from Microsoft.Common.CurrentVersion.targets. {0} is a project name.
</comment>
</data>
<data name="ProjectSchemaErrorHalt" xml:space="preserve">
<value>MSBUILD : MSB1045: Stopping because of syntax errors in project file.</value>
<comment>{StrBegin="MSBUILD : MSB1045: "}</comment>
Expand Down
7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/MSBuild/TerminalLogger/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public Project(string? targetFramework, StopwatchAbstraction? stopwatch)
/// </summary>
public bool IsTestProject { get; set; }

/// <summary>
/// True when the project has run target with name "_CachePluginRunStart" defined in <see cref="TerminalLogger._cachePluginStartTarget"/>.
/// </summary>
public bool IsCachePluginProject { get; set; }

/// <summary>
/// A lazily initialized list of build messages/warnings/errors raised during the build.
/// </summary>
Expand Down
47 changes: 42 additions & 5 deletions src/MSBuild/TerminalLogger/TerminalLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Microsoft.Build.Logging.TerminalLogger;
internal sealed partial class TerminalLogger : INodeLogger
{
private const string FilePathPattern = " -> ";
private const string MSBuildSenderName = "MSBuild";

#if NET7_0_OR_GREATER
[StringSyntax(StringSyntaxAttribute.Regex)]
Expand Down Expand Up @@ -200,6 +201,16 @@ public ProjectContext(BuildEventContext context)
/// </summary>
private DateTime? _testEndTime;

/// <summary>
/// Name of target that identifies the project cache plugin run has just started.
/// </summary>
private static string _cachePluginStartTarget = "_CachePluginRunStart";

/// <summary>
/// Whether to show high-priority messages corresponding to the cache plugin.
/// </summary>
private bool _cachePluginStarted = false;

/// <summary>
/// Whether to show TaskCommandLineEventArgs high-priority messages.
/// </summary>
Expand Down Expand Up @@ -637,6 +648,19 @@ private void ProjectFinished(object sender, ProjectFinishedEventArgs e)
{
Terminal.WriteLine(string.Empty);
}

if (!_cachePluginStarted && project.IsCachePluginProject)
{
// First non-traversal project results in cache miss
_cachePluginStarted = true;
}
}
else if (!_cachePluginStarted && project.IsCachePluginProject)
{
// First non-traversal project results in cache hit
Terminal.WriteLine(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("ProjectCacheHitWithOutputs",
projectFile));
_cachePluginStarted = true;
}

// Print diagnostic output under the Project -> Output line.
Expand Down Expand Up @@ -673,12 +697,16 @@ private void TargetStarted(object sender, TargetStartedEventArgs e)

string projectFile = Path.GetFileNameWithoutExtension(e.ProjectFile);

string targetName = e.TargetName;
if (targetName == "GetTargetPath" || targetName == _cachePluginStartTarget) // TODO: Change to _cachePluginStartTarget (from "GetTargetPath" for testing cache hit scenario)
{
project.IsCachePluginProject = true;
}

var isTestTarget = e.TargetName == _testStartTarget;

var targetName = isTestTarget ? "Testing" : e.TargetName;
if (isTestTarget)
if (targetName == _testStartTarget)
{
targetName = "Testing";

// Use the minimal start time, so if we run tests in parallel, we can calculate duration
// as this start time, minus time when tests finished.
_testStartTime = _testStartTime == null
Expand Down Expand Up @@ -778,6 +806,15 @@ private void MessageRaised(object sender, BuildMessageEventArgs e)
}
}

if (_cachePluginStarted)
{
if (e.SenderName is not null and MSBuildSenderName)
{
RenderImmediateMessage(message);
return;
}
}

if (hasProject && project!.IsTestProject)
{
var node = _nodes[NodeIndexForContext(buildEventContext)];
Expand Down Expand Up @@ -931,7 +968,7 @@ private void ErrorRaised(object sender, BuildErrorEventArgs e)
}
}

#endregion
#endregion

#region Refresher thread implementation

Expand Down

0 comments on commit ffad290

Please sign in to comment.