Skip to content

Commit

Permalink
Add explicit support for assembly aliases (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
dupdob authored Nov 13, 2024
1 parent 2248a7a commit 28cf9aa
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Build and Test
run: dotnet test --logger "trx;LogFileName=test-results.trx"
- name: Upload Test Results
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: test-results-${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion src/Buildalyzer.Workspaces/AnalyzerResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private static IEnumerable<DocumentInfo> GetAdditionalDocuments(IAnalyzerResult
private static IEnumerable<MetadataReference> GetMetadataReferences(IAnalyzerResult analyzerResult) =>
analyzerResult
.References?.Where(File.Exists)
.Select(x => MetadataReference.CreateFromFile(x))
.Select(x => MetadataReference.CreateFromFile(x, new MetadataReferenceProperties(aliases: analyzerResult.ReferenceAliases.GetValueOrDefault(x))))
?? [];

private static IEnumerable<AnalyzerReference> GetAnalyzerReferences(IAnalyzerResult analyzerResult, Workspace workspace)
Expand Down
3 changes: 3 additions & 0 deletions src/Buildalyzer/AnalyzerResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public string GetProperty(string name) =>
public string[] References =>
CompilerCommand?.MetadataReferences.ToArray() ?? [];

public ImmutableDictionary<string, ImmutableArray<string>> ReferenceAliases =>
CompilerCommand?.Aliases ?? ImmutableDictionary<string, ImmutableArray<string>>.Empty;

public string[] AnalyzerReferences =>
CompilerCommand?.AnalyzerReferences.Select(r => r.ToString()).ToArray() ?? [];

Expand Down
4 changes: 2 additions & 2 deletions src/Buildalyzer/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public static TCommand Enrich<TCommand>(TCommand command, CommandLineArguments a
AnalyzerReferences = arguments.AnalyzerReferences.Select(AsIOPath).ToImmutableArray(),
AnalyzerConfigPaths = arguments.AnalyzerConfigPaths.Select(IOPath.Parse).ToImmutableArray(),
MetadataReferences = arguments.MetadataReferences.Select(m => m.Reference).ToImmutableArray(),
Aliases = arguments.MetadataReferences.Where(m => !m.Properties.Aliases.IsEmpty).ToImmutableDictionary(m => m.Reference, m => m.Properties.Aliases),
PreprocessorSymbolNames = arguments.ParseOptions.PreprocessorSymbolNames.ToImmutableArray(),

SourceFiles = arguments.SourceFiles.Select(AsIOPath).ToImmutableArray(),
AdditionalFiles = arguments.AdditionalFiles.Select(AsIOPath).ToImmutableArray(),
EmbeddedFiles = arguments.EmbeddedFiles.Select(AsIOPath).ToImmutableArray(),
Expand All @@ -115,4 +115,4 @@ public static TCommand Enrich<TCommand>(TCommand command, CommandLineArguments a

[Pure]
internal static IOPath AsIOPath(CommandLineSourceFile file) => IOPath.Parse(file.Path);
}
}
5 changes: 5 additions & 0 deletions src/Buildalyzer/Compiler/CompilerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public abstract record CompilerCommand
/// <inheritdoc cref="CommandLineArguments.MetadataReferences" />
public ImmutableArray<string> MetadataReferences { get; init; }

/// <summary>
/// The aliases used in the command line arguments.
/// </summary>
public ImmutableDictionary<string, ImmutableArray<string>> Aliases { get; init; }

/// <inheritdoc />
[Pure]
public override string ToString() => Text ?? string.Empty;
Expand Down
2 changes: 2 additions & 0 deletions src/Buildalyzer/IAnalyzerResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface IAnalyzerResult

string[] References { get; }

ImmutableDictionary<string, ImmutableArray<string>> ReferenceAliases {get; }

string[] AnalyzerReferences { get; }

string[] SourceFiles { get; }
Expand Down
1 change: 0 additions & 1 deletion src/Buildalyzer/Logging/EventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ private void MessageRaised(object sender, BuildMessageEventArgs e)
}

bool IsRelevant() => string.IsNullOrEmpty(result.Command) || AnalyzerManager.NormalizePath(e.ProjectFile) == _projectFilePath;

}

private void BuildFinished(object sender, BuildFinishedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SimpleProjectsFixture
@"SdkNet6ImplicitUsings\SdkNet6ImplicitUsings.csproj",
@"SdkNet7Project\SdkNet7Project.csproj",
@"SdkNet8CS12FeaturesProject\SdkNet8CS12FeaturesProject.csproj",
@"SdkNet8Alias\SdkNet8Alias.csproj",
@"SdkNetCore2ProjectImport\SdkNetCore2ProjectImport.csproj",
@"SdkNetCore2ProjectWithReference\SdkNetCore2ProjectWithReference.csproj",
@"SdkNetCore2ProjectWithImportedProps\SdkNetCore2ProjectWithImportedProps.csproj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ public async Task SupportsLangVersion12Features()
diagnostics.ShouldBeEmpty();
}


[Test(Description = "Test Reference Alias support")]

public async Task SupportAssemblyAliases()
{
// Given
StringWriter log = new StringWriter();
IProjectAnalyzer analyzer = GetProjectAnalyzer(@"projects\SdkNet8Alias\SdkNet8Alias.csproj", log);
AdhocWorkspace workspace = analyzer.GetWorkspace();
Project project = workspace.CurrentSolution.Projects.Single();

// When
Compilation compilation = await project.GetCompilationAsync();

Diagnostic[] diagnostics = compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).ToArray();

diagnostics.ShouldBeEmpty();
}

#if Is_Windows
[Test]
public void HandlesWpfCustomControlLibrary()
Expand Down
15 changes: 15 additions & 0 deletions tests/projects/SdkNet8Alias/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern alias Alias;

using Alias::Xunit;

namespace SdkNet8Alias
{
public class Class1
{
[Fact]
public void Test1()
{

}
}
}
33 changes: 33 additions & 0 deletions tests/projects/SdkNet8Alias/SdkNet8Alias.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.extensibility.core" Version="2.9.2">
<Aliases>Alias</Aliases>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions tests/projects/TestProjects.sln
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNet7Project", "SdkNet7Pr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNet8CS12FeaturesProject", "SdkNet8CS12FeaturesProject\SdkNet8CS12FeaturesProject.csproj", "{6DDD74AB-8821-4267-8DC2-8864A3BB5871}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SdkNet8Alias", "SdkNet8Alias\SdkNet8Alias.csproj", "{1646FB0E-BAF2-45D0-9CD5-19EF7310FFC8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AzureFunctionProject", "AzureFunctionProject\AzureFunctionProject.csproj", "{A6411BE0-55A6-4D44-9F1C-FA6B674B832A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -178,6 +182,14 @@ Global
{6DDD74AB-8821-4267-8DC2-8864A3BB5871}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DDD74AB-8821-4267-8DC2-8864A3BB5871}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DDD74AB-8821-4267-8DC2-8864A3BB5871}.Release|Any CPU.Build.0 = Release|Any CPU
{1646FB0E-BAF2-45D0-9CD5-19EF7310FFC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1646FB0E-BAF2-45D0-9CD5-19EF7310FFC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1646FB0E-BAF2-45D0-9CD5-19EF7310FFC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1646FB0E-BAF2-45D0-9CD5-19EF7310FFC8}.Release|Any CPU.Build.0 = Release|Any CPU
{A6411BE0-55A6-4D44-9F1C-FA6B674B832A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6411BE0-55A6-4D44-9F1C-FA6B674B832A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6411BE0-55A6-4D44-9F1C-FA6B674B832A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6411BE0-55A6-4D44-9F1C-FA6B674B832A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 28cf9aa

Please sign in to comment.