Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Globalization;

namespace Microsoft.NET.Build.Tasks
Expand All @@ -10,7 +9,7 @@ namespace Microsoft.NET.Build.Tasks
/// Represents an error that is neither avoidable in all cases nor indicative of a bug in this library.
/// It will be logged as a plain build error without the exception type or stack.
/// </summary>
internal sealed class BuildErrorException : Exception
internal class BuildErrorException : Exception
{
public BuildErrorException()
{
Expand Down
20 changes: 6 additions & 14 deletions src/tasks/Crossgen2Tasks/CommonFilePulledFromSdkRepo/LogAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Build.Utilities;

namespace Microsoft.NET.Build.Tasks
{
internal sealed class LogAdapter : Logger
internal sealed class LogAdapter(TaskLoggingHelper taskLogger) : Logger
{
private TaskLoggingHelper _taskLogger;

public LogAdapter(TaskLoggingHelper taskLogger)
{
_taskLogger = taskLogger;
}

protected override void LogCore(in Message message)
{
switch (message.Level)
{
case MessageLevel.Error:
_taskLogger.LogError(
taskLogger.LogError(
subcategory: default,
errorCode: message.Code,
helpKeyword: default,
Expand All @@ -33,7 +25,7 @@ protected override void LogCore(in Message message)
break;

case MessageLevel.Warning:
_taskLogger.LogWarning(
taskLogger.LogWarning(
subcategory: default,
warningCode: message.Code,
helpKeyword: default,
Expand All @@ -53,12 +45,12 @@ protected override void LogCore(in Message message)
// use shorter overload when there is no code and no file. Otherwise, msbuild
// will display:
//
// <project file>(<line>,<colunmn>): message : <text>
_taskLogger.LogMessage(message.Level.ToImportance(), message.Text);
// <project file>(<line>,<column>): message : <text>
taskLogger.LogMessage(message.Level.ToImportance(), message.Text);
}
else
{
_taskLogger.LogMessage(
taskLogger.LogMessage(
subcategory: default,
code: message.Code,
helpKeyword: default,
Expand Down
21 changes: 10 additions & 11 deletions src/tasks/Crossgen2Tasks/CommonFilePulledFromSdkRepo/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
Expand Down Expand Up @@ -40,8 +39,8 @@ namespace Microsoft.NET.Build.Tasks
/// Results in LogCore getting a Message instance with Code="NETSDK1234"
/// and Text="Something is wrong."
///
/// Pattern inspired by <se cref="TaskLoggingHelper.LogErrorWithCodeFromResources"/>,
/// but retains completion via generated <see cref="Strings"/> instead of
/// Pattern inspired by TaskLoggingHelper.LogErrorWithCodeFromResources,
/// but retains completion via generated Strings instead of
/// passing resource keys by name.
///
/// All actual logging is deferred to subclass in <see cref="LogCore"/>,
Expand All @@ -65,9 +64,6 @@ public void LogWarning(string format, params string[] args)
public void LogError(string format, params string[] args)
=> Log(CreateMessage(MessageLevel.Error, format, args));

public void LogNonSdkError(string code, string format, params string[] args)
=> Log(new Message(MessageLevel.Error, string.Format(format, args), code));

public void Log(in Message message)
{
HasLoggedErrors |= message.Level == MessageLevel.Error;
Expand All @@ -78,7 +74,7 @@ public void Log(in Message message)

private static Message CreateMessage(MessageLevel level, string format, string[] args)
{
string code;
string? code;

if (format.Length >= 12
&& format[0] == 'N'
Expand Down Expand Up @@ -111,7 +107,7 @@ private static Message CreateMessage(MessageLevel level, string format, string[]
}

[Conditional("DEBUG")]
private static void DebugThrowMissingOrIncorrectCode(string code, string message, MessageLevel level)
private static void DebugThrowMissingOrIncorrectCode(string? code, string message, MessageLevel level)
{
// NB: This is not localized because it represents a bug in our code base, not a user error.
// To log message with external codes, use Log.Log(in Message, string[]) directly.
Expand All @@ -130,11 +126,14 @@ private static void DebugThrowMissingOrIncorrectCode(string code, string message
break;

default:

if (code != null)
{
throw new ArgumentException(
"Message is prefixed with NETSDK error, but error codes should not be used for informational messages: "
+ $"{code}:{message}");
// Previously we would throw an error here, but that makes it hard to allow errors to be turned off but still displayed as messages
// (which ResolveApppHosts does). So let's just let messages have NETSDK error codes if they want to also.
//throw new ArgumentException(
// "Message is prefixed with NETSDK error, but error codes should not be used for informational messages: "
// + $"{code}:{message}");
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Microsoft.NET.Build.Tasks
internal readonly struct Message
{
public readonly MessageLevel Level;
public readonly string Code;
public readonly string? Code;
public readonly string Text;
public readonly string File;
public readonly string? File;

public Message(
MessageLevel level,
string text,
string code = default,
string file = default)
string? code = default,
string? file = default)
{
Level = level;
Code = code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Build.Framework;

namespace Microsoft.NET.Build.Tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ internal static class MetadataKeys
public const string FrameworkVersion = "FrameworkVersion";
public const string IsTrimmable = "IsTrimmable";
public const string RuntimeFrameworkName = "RuntimeFrameworkName";
public const string RuntimePackRuntimeIdentifiers = "RuntimePackRuntimeIdentifiers";

// SDK Metadata
public const string SDKPackageItemSpec = "SDKPackageItemSpec";
Expand Down Expand Up @@ -83,12 +82,17 @@ internal static class MetadataKeys

// Targeting packs
public const string PackageConflictPreferredPackages = "PackageConflictPreferredPackages";
public const string RuntimePackRuntimeIdentifiers = "RuntimePackRuntimeIdentifiers";
public const string RuntimePackExcludedRuntimeIdentifiers = "RuntimePackExcludedRuntimeIdentifiers";

// Runtime packs
public const string DropFromSingleFile = "DropFromSingleFile";
public const string RuntimePackLabels = "RuntimePackLabels";
public const string AdditionalFrameworkReferences = "AdditionalFrameworkReferences";

// Apphost packs
public const string ExcludedRuntimeIdentifiers = "ExcludedRuntimeIdentifiers";

// Content files
public const string PPOutputPath = "PPOutputPath";
public const string CodeLanguage = "CodeLanguage";
Expand Down Expand Up @@ -125,5 +129,15 @@ internal static class MetadataKeys
public const string IsVersion5 = "IsVersion5";
public const string CreateCompositeImage = "CreateCompositeImage";
public const string PerfmapFormatVersion = "PerfmapFormatVersion";

// Debug symbols
public const string RelatedProperty = "related";
public const string XmlExtension = ".xml";
public const string XmlFilePath = "XmlFilePath";
public const string PdbExtension = ".pdb";
public const string PdbFilePath = "PdbFilePath";

// Dependencies design time
public const string Resolved = "Resolved";
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using NuGet.Frameworks;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.RuntimeModel;

namespace Microsoft.NET.Build.Tasks
Expand All @@ -32,7 +25,7 @@ public static bool IsPlaceholderFile(string path)
return separator == '\\' || separator == '/';
}

public static string GetLockFileLanguageName(string projectLanguage)
public static string? GetLockFileLanguageName(string? projectLanguage)
{
switch (projectLanguage)
{
Expand All @@ -42,7 +35,7 @@ public static string GetLockFileLanguageName(string projectLanguage)
}
}

public static NuGetFramework ParseFrameworkName(string frameworkName)
public static NuGetFramework? ParseFrameworkName(string? frameworkName)
{
return frameworkName == null ? null : NuGetFramework.Parse(frameworkName);
}
Expand All @@ -59,8 +52,8 @@ bool IsAnalyzer()
&& !file.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase);
}

bool CS() => file.Contains("/cs/", StringComparison.OrdinalIgnoreCase);
bool VB() => file.Contains("/vb/", StringComparison.OrdinalIgnoreCase);
bool CS() => file.IndexOf("/cs/", StringComparison.OrdinalIgnoreCase) >= 0;
bool VB() => file.IndexOf("/vb/", StringComparison.OrdinalIgnoreCase) >= 0;

bool FileMatchesProjectLanguage()
{
Expand All @@ -80,22 +73,39 @@ bool FileMatchesProjectLanguage()
return IsAnalyzer() && FileMatchesProjectLanguage();
}

public static string GetBestMatchingRid(RuntimeGraph runtimeGraph, string runtimeIdentifier,
public static string? GetBestMatchingRid(RuntimeGraph runtimeGraph, string runtimeIdentifier,
IEnumerable<string> availableRuntimeIdentifiers, out bool wasInGraph)
{
return GetBestMatchingRidWithExclusion(runtimeGraph, runtimeIdentifier,
runtimeIdentifiersToExclude: null,
availableRuntimeIdentifiers, out wasInGraph);
}

public static string? GetBestMatchingRidWithExclusion(RuntimeGraph runtimeGraph, string runtimeIdentifier,
IEnumerable<string>? runtimeIdentifiersToExclude,
IEnumerable<string> availableRuntimeIdentifiers, out bool wasInGraph)
{
wasInGraph = runtimeGraph.Runtimes.ContainsKey(runtimeIdentifier);

HashSet<string> availableRids = new HashSet<string>(availableRuntimeIdentifiers);
string? bestMatch = null;

HashSet<string> availableRids = new(availableRuntimeIdentifiers, StringComparer.Ordinal);
HashSet<string>? excludedRids = runtimeIdentifiersToExclude switch { null => null, _ => new HashSet<string>(runtimeIdentifiersToExclude, StringComparer.Ordinal) };
foreach (var candidateRuntimeIdentifier in runtimeGraph.ExpandRuntime(runtimeIdentifier))
{
if (availableRids.Contains(candidateRuntimeIdentifier))
if (bestMatch == null && availableRids.Contains(candidateRuntimeIdentifier))
{
bestMatch = candidateRuntimeIdentifier;
}

if (excludedRids != null && excludedRids.Contains(candidateRuntimeIdentifier))
{
return candidateRuntimeIdentifier;
// Don't treat this as a match
return null;
}
}

// No compatible RID found in availableRuntimeIdentifiers
return null;
return bestMatch;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#nullable disable

using Microsoft.Build.Framework;
using NuGet.RuntimeModel;

namespace Microsoft.NET.Build.Tasks
{
internal sealed class RuntimeGraphCache
internal class RuntimeGraphCache
{
private IBuildEngine4 _buildEngine;
private Logger _log;
Expand Down
15 changes: 8 additions & 7 deletions src/tasks/Crossgen2Tasks/CommonFilePulledFromSdkRepo/TaskBase.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Build.Framework;
using Task = Microsoft.Build.Utilities.Task;

namespace Microsoft.NET.Build.Tasks
{
public abstract class TaskBase : Task
{
private Logger _logger;
private Logger? _logger;

internal TaskBase(Logger logger = null)
internal TaskBase(Logger? logger = null)
{
_logger = logger;
}
Expand All @@ -22,7 +20,10 @@ internal TaskBase(Logger logger = null)
{
get
{
_logger ??= new LogAdapter(base.Log);
if (_logger == null)
{
_logger = new LogAdapter(base.Log);
}

return _logger;
}
Expand Down
15 changes: 14 additions & 1 deletion src/tasks/Crossgen2Tasks/Crossgen2Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<OutputType>Library</OutputType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn),CA1050</NoWarn>
<NoWarn>$(NoWarn);CA1050;CA1852;CA1861;CA2249;IDE0059;IDE0060;IDE0074</NoWarn>
<Nullable>annotations</Nullable>
</PropertyGroup>
<ItemGroup>
<!--
This repository generally does not use global usings, but the SDK does.
Use the minimum set of usings required to avoid modifying the copied code.
-->
<Using Include="System" />
<Using Include="System.Collections.Generic" />
<Using Include="System.IO" />
<Using Include="System.Linq" />
<Using Include="System.Runtime.InteropServices" />
<Using Include="System.Text" />
</ItemGroup>
<ItemGroup>
<!-- Bring in references for these assemblies which are provided by the SDK.
We do this to avoid bringing the package closure for assemblies we don't use here. -->
Expand Down
Loading
Loading