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

fix(reference alias): Properly support PackageReference Alias #3070

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public int Subtract(int first, int second)
}

[TestMethod]
public void CompilingProcessTests_ShouldSupportReferenceAliases()
public void CompilingProcessTests_ShouldSupportPackageReferenceAliases()
{
var syntaxTree = CSharpSyntaxTree.ParseText(@"
extern alias Texte;
using System;
extern alias TestAlias;
using TestAlias::System;

namespace ExampleProject
{
Expand All @@ -97,16 +97,31 @@ public int Subtract(int first, int second)
properties: new Dictionary<string, string>()
{
{ "TargetDir", "" },
{ "AssemblyName", "AssemblyName"},
{ "TargetFileName", "TargetFileName.dll"},
{ "AssemblyName", "AssemblyName" },
{ "TargetFileName", "TargetFileName.dll" },
},
// add a reference to system so the example code can compile
references: new[] { typeof(object).Assembly.Location, $"Texte={typeof(StringBuilder).Assembly.Location}" }
references: new[] { typeof(object).Assembly.Location },
packageReferences: new ReadOnlyDictionary<string, IReadOnlyDictionary<string, string>>(
new Dictionary<string, IReadOnlyDictionary<string, string>>
{
{
typeof(object).Assembly.GetName().Name,
new ReadOnlyDictionary<string, string>(
new Dictionary<string, string>
{
{ "Aliases", "TestAlias" }
}
)
}
}
)
).Object
}
};
var rollbackProcessMock = new Mock<ICSharpRollbackProcess>(MockBehavior.Strict);


var target = new CsharpCompilingProcess(input, rollbackProcessMock.Object);

using (var ms = new MemoryStream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,26 @@ public static IEnumerable<MetadataReference> LoadReferences(this IAnalyzerResult
{
foreach (var reference in analyzerResult.References)
{
if (reference.Contains('='))
var referenceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(reference);
string packageWithAlias = null;

if (analyzerResult.PackageReferences is not null) // Check if the analyzer result has package references because the Unit Tests don't have them.
rouke-broersma marked this conversation as resolved.
Show resolved Hide resolved
{
// Check for any matching package reference with an alias using LINQ
packageWithAlias = analyzerResult.PackageReferences
.Where(pr => pr.Key == referenceFileNameWithoutExtension && pr.Value.ContainsKey("Aliases"))
.Select(pr => pr.Value["Aliases"])
.FirstOrDefault();
}

if (packageWithAlias is not null)
{
// we have an alias
var split = reference.Split('=');
var aliases = split[0].Split(',');
yield return MetadataReference.CreateFromFile(split[1]).WithAliases(aliases);
// Return the reference with the alias
yield return MetadataReference.CreateFromFile(reference).WithAliases(new[] { packageWithAlias });
}
else
{
// If no alias is found, return the reference without aliases
yield return MetadataReference.CreateFromFile(reference);
}
}
Expand Down
Loading