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
4 changes: 3 additions & 1 deletion src/Tasks/Common/MetadataKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ internal static class MetadataKeys
public const string DestinationSubPath = "DestinationSubPath";
public const string AssetType = "AssetType";

public const string ReferenceOnly = "ReferenceOnly";
public const string ReferenceOnly = "ReferenceOnly";

public const string Aliases = "Aliases";
}
}
11 changes: 9 additions & 2 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Build.Evaluation;
Expand Down Expand Up @@ -869,7 +868,15 @@ private void WriteCompileTimeAssemblies()
{
WriteItems(
_compileTimeTarget,
package => package.CompileTimeAssemblies);
package => package.CompileTimeAssemblies,
filter: null,
writeMetadata: (package, asset) =>
{
if (asset.Properties.TryGetValue(LockFileItem.AliasesProperty, out var aliases))
{
WriteMetadata(MetadataKeys.Aliases, aliases);
}
});
}

private void WriteContentFilesToPreprocess()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeHaveAPackageReferenceWithAliases : SdkTest
{

public GivenThatWeHaveAPackageReferenceWithAliases(ITestOutputHelper log) : base(log)
{ }

[CoreMSBuildOnlyFact]
public void CanBuildProjectWithPackageReferencesWithConflictingTypes()
{
var targetFramework = "netcoreapp3.1";
var packageReferences = GetPackageReferencesWithConflictingTypes(targetFramework, "A", "B");

TestProject testProject = new TestProject()
{
Name = "Project",
IsSdkProject = true,
IsExe = false,
TargetFrameworks = targetFramework,
};

testProject.PackageReferences.Add(packageReferences.First());
testProject.PackageReferences.Add(
new TestPackageReference(
packageReferences.Last().ID,
packageReferences.Last().Version,
packageReferences.Last().NupkgPath,
packageReferences.Last().PrivateAssets,
aliases: "Special"));
var packagesPaths = packageReferences.Select(e => Path.GetDirectoryName(e.NupkgPath));

testProject.AdditionalProperties.Add("RestoreSources",
"$(RestoreSources);" + string.Join(";", packagesPaths));

// Use a test-specific packages folder
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\..\pkg";
testProject.SourceFiles[$"{testProject.Name}.cs"] = ConflictingClassLibUsage;
var testAsset = _testAssetsManager.CreateTestProject(testProject);

var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
buildCommand.Execute()
.Should()
.Pass();
}

[CoreMSBuildOnlyFact]
public void CanBuildProjectWithMultiplePackageReferencesWithAliases()
{
var targetFramework = "netcoreapp3.1";

var packageReferenceA = GetPackageReference(targetFramework, "A", ClassLibClassA);
var packageReferenceB = GetPackageReference(targetFramework, "B", ClassLibClassB);

TestProject testProject = new TestProject()
{
Name = "Project",
IsSdkProject = true,
IsExe = false,
TargetFrameworks = targetFramework,
};

testProject.PackageReferences.Add(
new TestPackageReference(
packageReferenceA.ID,
packageReferenceA.Version,
packageReferenceA.NupkgPath,
packageReferenceA.PrivateAssets,
aliases: "First"));
testProject.PackageReferences.Add(
new TestPackageReference(
packageReferenceB.ID,
packageReferenceB.Version,
packageReferenceB.NupkgPath,
packageReferenceB.PrivateAssets,
aliases: "Second"));

testProject.AdditionalProperties.Add("RestoreSources",
"$(RestoreSources);" + Path.GetDirectoryName(packageReferenceA.NupkgPath) + ";" + Path.GetDirectoryName(packageReferenceB.NupkgPath));

// Use a test-specific packages folder
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\..\pkg";
testProject.SourceFiles[$"{testProject.Name}.cs"] = ClassLibAandBUsage;
var testAsset = _testAssetsManager.CreateTestProject(testProject);

var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
buildCommand.Execute()
.Should()
.Pass();
}

[CoreMSBuildOnlyFact]
public void CanBuildProjectWithAPackageReferenceWithMultipleAliases()
{
var targetFramework = "netcoreapp3.1";

var packageReferenceA = GetPackageReference(targetFramework, "A", ClassLibMultipleClasses);

TestProject testProject = new TestProject()
{
Name = "Project",
IsSdkProject = true,
IsExe = false,
TargetFrameworks = targetFramework,
};

testProject.PackageReferences.Add(
new TestPackageReference(
packageReferenceA.ID,
packageReferenceA.Version,
packageReferenceA.NupkgPath,
packageReferenceA.PrivateAssets,
aliases: "First,Second"));

testProject.AdditionalProperties.Add("RestoreSources",
"$(RestoreSources);" + Path.GetDirectoryName(packageReferenceA.NupkgPath));

// Use a test-specific packages folder
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\..\pkg";
testProject.SourceFiles[$"{testProject.Name}.cs"] = ClassLibAandBUsage;
var testAsset = _testAssetsManager.CreateTestProject(testProject);

var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
buildCommand.Execute()
.Should()
.Pass();
}

private IEnumerable<TestPackageReference> GetPackageReferencesWithConflictingTypes(string targetFramework, params string[] packageNames)
{
foreach (var packageName in packageNames)
{
yield return GetPackageReference(targetFramework, packageName, ClassLibConflictingMethod);
}
}

private TestPackageReference GetPackageReference(string targetFramework, string packageName, string projectFileContent)
{
var project = GetProject(targetFramework, packageName, projectFileContent);
var packCommand = new PackCommand(Log, _testAssetsManager.CreateTestProject(project).TestRoot, packageName);

packCommand
.Execute()
.Should()
.Pass();
return new TestPackageReference(packageName, "1.0.0", packCommand.GetNuGetPackage(packageName));
}

private static TestProject GetProject(string targetFramework, string referenceProjectName, string projectFileContent)
{
var project = new TestProject()
{
Name = referenceProjectName,
TargetFrameworks = targetFramework,
IsSdkProject = true
};
project.SourceFiles[$"{referenceProjectName}.cs"] = projectFileContent;
return project;
}

private static string ClassLibConflictingMethod = @"
using System;
public class ClassLib
{
public void ConflictingMethod()
{
}
}
";

private static string ClassLibClassA = @"
using System;
public class A
{
public void AMethod()
{
}
}
";

private static string ClassLibMultipleClasses = @"
using System;
public class A
{
public void AMethod()
{
}
}

public class B
{
public void BMethod()
{
}
}
";

private static string ClassLibClassB = @"
using System;
public class B
{
public void BMethod()
{
}
}
";

private static string ClassLibAandBUsage = @"
extern alias First;
extern alias Second;
using System;
public class ClassLibUsage
{
public void UsageMethod()
{
new First.A().AMethod();
new Second.B().BMethod();
}
}
";

private static string ConflictingClassLibUsage = @"
extern alias Special;
using System;
public class ClassLibUsage
{
public void UsageMethod()
{
new Special.ClassLib().ConflictingMethod();
}
}
";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder,
{
packageReferenceElement.Add(new XAttribute("PrivateAssets", packageReference.PrivateAssets));
}
if (packageReference.Aliases != null)
{
packageReferenceElement.Add(new XAttribute("Aliases", packageReference.Aliases));
}
packageReferenceItemGroup.Add(packageReferenceElement);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Tests/Microsoft.NET.TestFramework/TestPackageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ namespace Microsoft.NET.TestFramework
{
public class TestPackageReference
{
public TestPackageReference(string id, string version = null, string nupkgPath = null, string privateAssets = null)
public TestPackageReference(string id, string version = null, string nupkgPath = null, string privateAssets = null, string aliases = null)
{
ID = id;
Version = version;
NupkgPath = nupkgPath;
PrivateAssets = privateAssets;
Aliases = aliases;
}

public string ID { get; private set; }
public string Version { get; private set; }
public string NupkgPath { get; private set; }
public string PrivateAssets { get; private set; }

public string Aliases { get; private set; }
public bool NuGetPackageExists()
{
return File.Exists(Path.Combine(this.NupkgPath, String.Concat(this.ID + "." + this.Version + ".nupkg")));
Expand Down