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

Correctly handle multiple reference aliases #1905

Merged
merged 2 commits into from
Aug 21, 2020
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
18 changes: 16 additions & 2 deletions src/OmniSharp.MSBuild/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,14 @@ private void UpdateProjectReferences(Project project, ImmutableArray<string> pro
{
if (!string.IsNullOrEmpty(projectReferenceAliases))
{
aliases = projectReferenceAliases.Split(';').ToImmutableArray();
var trimmed = projectReferenceAliases.Split(',');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if both , and ; are supported here, I'm not able to find much documentation however.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not - in the case that the element contains a semicolon-separated list of aliases, the build process will fail:

Roslyn\Microsoft.CSharp.Core.targets(70,5): error MSB6001: Invalid command line switch for "csc.dll". System.ArgumentException: MSB3053: The assembly alias "abc;def" on reference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just write it in one line

!string.IsNullOrWhiteSpace(projectReferenceAliases)
                ? ImmutableArray.CreateRange(projectReferenceAliases.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(a => a.Trim()))
                : ImmutableArray<string>.Empty;

this is exactly what Roslyn uses internally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - I erred on the side of trying to avoid LINQ for performance, but I do suppose this isn't directly in the hotpath.


for(var i = 0; i < trimmed.Length; i++)
{
trimmed[i] = trimmed[i].Trim();
}

aliases = trimmed.ToImmutableArray();
_logger.LogDebug($"Setting aliases: {projectReferencePath}, {projectReferenceAliases} ");
}
}
Expand Down Expand Up @@ -740,7 +747,14 @@ private void UpdateReferences(Project project, ImmutableArray<string> projectRef
{
if (!string.IsNullOrEmpty(aliases))
{
reference = reference.WithAliases(aliases.Split(';'));
var trimmed = aliases.Split(',');

for(var i = 0; i < trimmed.Length; i++)
{
trimmed[i] = trimmed[i].Trim();
}

reference = reference.WithAliases(trimmed);
_logger.LogDebug($"setting aliases: {referencePath}, {aliases} ");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<Reference Include="ExternAlias.Lib">
<HintPath>$(ProjectDir)../ExternAlias.Lib/bin/$(Configuration)/netstandard2.0/ExternAlias.Lib.dll</HintPath>
<Aliases>abc</Aliases>
<Aliases>abc,def</Aliases>
</Reference>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern alias abc;
extern alias def;
using System;

namespace ExternAlias.App
Expand All @@ -8,6 +9,7 @@ class Program
static void Main(string[] args)
{
new abc::ExternAlias.Lib.Class1();
new def::ExternAlias.Lib.Class1();
Console.WriteLine("Hello World!");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<ProjectReference Include="../ExternAlias.Lib/ExternAlias.Lib.csproj">
<Aliases>abc</Aliases>
<Aliases>abc,def</Aliases>
</ProjectReference>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern alias abc;
extern alias def;
using System;

namespace ExternAlias.App
Expand All @@ -8,6 +9,7 @@ class Program
static void Main(string[] args)
{
new abc::ExternAlias.Lib.Class1();
new def::ExternAlias.Lib.Class1();
Console.WriteLine("Hello World!");
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/OmniSharp.MSBuild.Tests/ProjectFileInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async Task ExternAliasWithReference()

var libpath = Path.Combine(testProject.Directory, "ExternAlias.Lib", "bin", "Debug", "netstandard2.0", "ExternAlias.Lib.dll");
Assert.True(projectFileInfo.ReferenceAliases.ContainsKey(libpath));
Assert.Equal("abc", projectFileInfo.ReferenceAliases[libpath]);
Assert.Equal("abc,def", projectFileInfo.ReferenceAliases[libpath]);
}
}

Expand All @@ -179,7 +179,7 @@ public async Task ExternAliasWithProjectReference()

var projectReferencePath = Path.Combine(testProject.Directory, "ExternAlias.Lib", "ExternAlias.Lib.csproj");
Assert.True(projectFileInfo.ProjectReferenceAliases.ContainsKey(projectReferencePath));
Assert.Equal("abc", projectFileInfo.ProjectReferenceAliases[projectReferencePath]);
Assert.Equal("abc,def", projectFileInfo.ProjectReferenceAliases[projectReferencePath]);
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/OmniSharp.MSBuild.Tests/WorkspaceInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ public async Task TestProjectWithAliasOnProjectReference()
.ProjectReferences
.Single(x => x.ProjectId == lib.Id);

Assert.Equal("abc", projectReference.Aliases.Single());
Assert.Collection(projectReference.Aliases,
referenceA => Assert.Equal("abc",referenceA),
referenceB => Assert.Equal("def",referenceB));
}
}

Expand All @@ -337,7 +339,9 @@ public async Task TestProjectWithAliasOnReference()
.MetadataReferences
.Single(x => x.Display == "ExternAlias.Lib.dll");

Assert.Equal("abc", reference.Properties.Aliases.Single());
Assert.Collection(reference.Properties.Aliases,
referenceA => Assert.Equal("abc",referenceA),
referenceB => Assert.Equal("def",referenceB));
}
}
}
Expand Down