Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MSBuild Logging APIs for Task output instead of StdOut #933

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Sidestep a timing issue with the TaskLoggingHelper in the GenerateSbo…
…mTask's Logging infrastructure

Also generate binlogs for the tests to allow precise investigation of the builds
  • Loading branch information
baronfel committed Mar 1, 2025
commit 7b90dd481a842936928242f367c4152efa252aaa
11 changes: 6 additions & 5 deletions src/Microsoft.Sbom.Targets/GenerateSbomTask.cs
Original file line number Diff line number Diff line change
@@ -28,15 +28,15 @@ namespace Microsoft.Sbom.Targets;
/// </summary>
public partial class GenerateSbom : Task
{
private ISbomGenerator Generator { get; set; }
private readonly IHost taskHost;

/// <summary>
/// Constructor for the GenerateSbomTask.
/// </summary>
public GenerateSbom()
{
var taskLoggingHelper = new TaskLoggingHelper(this);
var host = Host.CreateDefaultBuilder()
taskHost = Host.CreateDefaultBuilder()
.ConfigureServices((host, services) =>
services
.AddSbomTool(LogEventLevel.Information, taskLoggingHelper)
@@ -62,7 +62,6 @@ public GenerateSbom()
.AddSingleton<IManifestConfigHandler, SPDX22ManifestConfigHandler>()
.AddSingleton<IManifestConfigHandler, SPDX30ManifestConfigHandler>())
.Build();
this.Generator = host.Services.GetRequiredService<ISbomGenerator>();
}

/// <inheritdoc/>
@@ -76,7 +75,9 @@ public override bool Execute()
return false;
}

// Set other configurations. The GenerateSbomAsync() already sanitizes and checks for
var generator = taskHost.Services.GetRequiredService<ISbomGenerator>();

// Set other configurations. The GenerateSBOMAsync() already sanitizes and checks for
// a valid namespace URI and generates a random guid for NamespaceUriUniquePart if
// one is not provided.
var sbomMetadata = new SbomMetadata
@@ -93,7 +94,7 @@ public override bool Execute()
Verbosity = ValidateAndAssignVerbosity(),
};
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
var result = System.Threading.Tasks.Task.Run(() => this.Generator.GenerateSbomAsync(
var result = System.Threading.Tasks.Task.Run(() => generator.GenerateSbomAsync(
rootPath: this.BuildDropPath,
manifestDirPath: this.ManifestDirPath,
metadata: sbomMetadata,
14 changes: 9 additions & 5 deletions test/Microsoft.Sbom.Targets.E2E.Tests/GenerateSbomE2ETests.cs
Original file line number Diff line number Diff line change
@@ -7,8 +7,10 @@ namespace Microsoft.Sbom.Targets.E2E.Tests;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Locator;
using Microsoft.Build.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -109,23 +111,25 @@ private void SetDefaultProperties(Project sampleProject)
}
}

private void RestoreBuildPack(Project sampleProject)
private void RestoreBuildPack(Project sampleProject, [CallerMemberName] string callerName = null)
{
var logger = new ConsoleLogger();

// Restore the project to create project.assets.json file
var restore = sampleProject.Build("Restore", new[] { logger });
var restore = sampleProject.Build("Restore", new ILogger[] { GetBinLog(callerName, "Restore"), logger });
Assert.IsTrue(restore, "Failed to restore the project");

// Next, build the project
var build = sampleProject.Build(logger);
var build = sampleProject.Build(new ILogger[] { GetBinLog(callerName, "Build"), logger });
Assert.IsTrue(build, "Failed to build the project");

// Finally, pack the project
var pack = sampleProject.Build("Pack", new[] { logger });
var pack = sampleProject.Build("Pack", new ILogger[] { GetBinLog(callerName, "Pack"), logger });
Assert.IsTrue(pack, "Failed to pack the project");
}

// binlogs are unique per name, so this ensures distinct names for the different stages
private BinaryLogger GetBinLog(string callerName, string target) => new BinaryLogger { Parameters = $"{callerName}.{target}.binlog" };

private void InspectPackageIsWellFormed(bool isManifestPathGenerated = true)
{
const string backSlash = "\\";