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
17 changes: 13 additions & 4 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public sealed class ResolvePackageAssets : TaskBase
/// </summary>
public string RuntimeIdentifier { get; set; }

/// <summary>
/// The `any` RID can be passed to indicate that the assets should be resolved as a RID-agnostic application.
/// We use this field to detect that case and ensure that we treat `any` the same as no RID at all.
/// Essentially, if you see use of `RuntimeIdentifier` directly, you should ask "why?".
/// </summary>
private string EffectiveRuntimeIdentifier => !string.IsNullOrEmpty(RuntimeIdentifier) && RuntimeIdentifier != "any" ? RuntimeIdentifier : null;

/// <summary>
/// The platform library name for resolving copy local assets.
/// </summary>
Expand Down Expand Up @@ -478,6 +485,8 @@ internal byte[] HashSettings()
writer.Write(ProjectLanguage ?? "");
writer.Write(CompilerApiVersion ?? "");
writer.Write(ProjectPath);
// we want to ensure uniqueness of results, so even though `any` is No RID for purposes of Task logic,
// we continue to treat it distinctly for hashing
writer.Write(RuntimeIdentifier ?? "");
if (ShimRuntimeIdentifiers != null)
{
Expand Down Expand Up @@ -730,7 +739,7 @@ public CacheWriter(ResolvePackageAssets task)
if (task.DesignTimeBuild)
{
_compileTimeTarget = _lockFile.GetTargetAndReturnNullIfNotFound(_targetFramework, runtimeIdentifier: null);
_runtimeTarget = _lockFile.GetTargetAndReturnNullIfNotFound(_targetFramework, _task.RuntimeIdentifier);
_runtimeTarget = _lockFile.GetTargetAndReturnNullIfNotFound(_targetFramework, runtimeIdentifier: _task.EffectiveRuntimeIdentifier);
if (_compileTimeTarget == null)
{
_compileTimeTarget = new LockFileTarget();
Expand All @@ -745,7 +754,7 @@ public CacheWriter(ResolvePackageAssets task)
else
{
_compileTimeTarget = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, runtimeIdentifier: null);
_runtimeTarget = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, _task.RuntimeIdentifier);
_runtimeTarget = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, runtimeIdentifier: _task.EffectiveRuntimeIdentifier);
}


Expand Down Expand Up @@ -1271,11 +1280,11 @@ private void WriteAdditionalLogMessages()

private void WriteUnsupportedRuntimeIdentifierMessageIfNecessary()
{
if (_task.EnsureRuntimePackageDependencies && !string.IsNullOrEmpty(_task.RuntimeIdentifier))
if (_task.EnsureRuntimePackageDependencies && !string.IsNullOrEmpty(_task.EffectiveRuntimeIdentifier))
{
if (_compileTimeTarget.Libraries.Count >= _runtimeTarget.Libraries.Count)
{
WriteItem(string.Format(Strings.UnsupportedRuntimeIdentifier, _task.RuntimeIdentifier));
WriteItem(string.Format(Strings.UnsupportedRuntimeIdentifier, _task.EffectiveRuntimeIdentifier));
WriteMetadata(MetadataKeys.Severity, nameof(LogLevel.Error));
}
}
Expand Down
10 changes: 9 additions & 1 deletion test/Microsoft.DotNet.PackageInstall.Tests/EndToEndToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ public void PackageToolWithAnyRid()
// Ensure that the package with the "any" RID is present
var anyRidPackage = packages.FirstOrDefault(p => p.EndsWith($"{packageIdentifier}.any.{toolSettings.ToolPackageVersion}.nupkg"));
anyRidPackage.Should().NotBeNull($"Package {packageIdentifier}.any.{toolSettings.ToolPackageVersion}.nupkg should be present in the tool packages directory")
.And.Satisfy<string>(EnsurePackageIsFdd);
.And.Satisfy<string>(EnsurePackageIsFdd)
.And.Satisfy<string>(EnsureFddPackageHasAllRuntimeAssets);

// top-level package should declare all of the rids
var topLevelPackage = packages.First(p => p.EndsWith($"{packageIdentifier}.{toolSettings.ToolPackageVersion}.nupkg"));
Expand Down Expand Up @@ -387,6 +388,13 @@ static void EnsurePackageIsFdd(string packagePath)
runner.Should().Be("dotnet", "The tool should be packaged as a framework-dependent executable (FDD) with a 'dotnet' runner.");
}

static void EnsureFddPackageHasAllRuntimeAssets(string packagePath)
{
using var zipArchive = ZipFile.OpenRead(packagePath);
var runtimesEntries = zipArchive.Entries.Select(e => e.Name.Contains("/runtimes/"));
runtimesEntries.Should().NotBeNull("The runtimes-assets should be present in the package.");
}

static void EnsurePackageHasNoRunner(string packagePath)
{
var settingsXml = GetToolSettingsFile(packagePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public string CreateTestTool(ITestOutputHelper log, TestToolSettings toolSetting
}

testProject.SourceFiles.Add("Program.cs", "Console.WriteLine(\"Hello Tool!\");");
testProject.PackageReferences.Add(new("Microsoft.Data.Sqlite", version: "9.0.8"));

var testAssetManager = new TestAssetsManager(log);
var testAsset = testAssetManager.CreateTestProject(testProject, identifier: toolSettings.GetIdentifier());
Expand Down