Skip to content

Assemblies with generic attributes applied to their assembly cannot be referenced #6734

@davidwrighton

Description

@davidwrighton

Issue Description

With the new C# feature Generic Attributes (dotnet/roslyn#55190) assemblies can be produced which cannot be referenced via standard msbuild references.

Steps to Reproduce

Using the not yet available C# compiler from Roslyn build a binary with an assembly level attribute which is generic, or sync the dotnet/runtime testbed as of 8/4/2021 and build test src/tests/reflection/GenericAttribute/GenericAttributeTests.csproj (I am about to checkin a change which will workaround the issue)

Note, I was unable to figure out how trigger this failure outside of the runtime build environment, and I was unsuccessful at building a standalone repro using a pre-built IL component.

Expected Behavior

Application builds successfully.

Actual Behavior

Build fails with the following stack trace.

Microsoft (R) Build Engine version 17.0.0-preview-21329-01+1b7661f36 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  GenericAttributeMetadata -> c:\git\runtime\artifacts\tests\coreclr\windows.x64.Debug\reflection\GenericAttribute\GenericAttributeMetadata\GenericAttributeMetadata.dll
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018: The "ResolveAssemblyReference" task failed unexpectedly. [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018: System.InvalidCastException: Specified cast is not valid. [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at System.Reflection.Throw.InvalidCast() in System.Reflection.Metadata.dll:token 0x600009f+0x5 [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle) in System.Reflection.Metadata.dll:token 0x60003a9+0xe [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.CorePopulateMetadata() in Microsoft.Build.Tasks.Core.dll:token 0x600013f+0x13a [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.get_Dependencies() in Microsoft.Build.Tasks.Core.dll:token 0x6000138+0x1c [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyDependency.AssemblyMetadata..ctor(String path) in Microsoft.Build.Tasks.Core.dll:token 0x6000d22+0xd [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.<>c.<GetAssemblyMetadata>b__12_0(String p) in Microsoft.Build.Tasks.Core.dll:token 0x600106d+0x0 [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) in System.Collections.Concurrent.dll:token 0x60000d3+0x4f [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.GetAssemblyMetadata(String path, ConcurrentDictionary`2 assemblyMetadataCache, AssemblyNameExtension[]& dependencies, String[]& scatterFiles, FrameworkName& frameworkName) in Microsoft.Build.Tasks.Core.dll:token 0x600013b+0x21 [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.SystemState.GetAssemblyMetadata(String path, ConcurrentDictionary`2 assemblyMetadataCache, AssemblyNameExtension[]& dependencies, String[]& scatterFiles, FrameworkName& frameworkName) in Microsoft.Build.Tasks.Core.dll:token 0x60008d0+0x10 [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.GetUnifiedAssemblyMetadata(Reference reference, IEnumerable`1& unifiedDependencies, String[]& scatterFiles) in Microsoft.Build.Tasks.Core.dll:token 0x60001fa+0x17 [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.FindDependenciesAndScatterFiles(Reference reference, List`1 newEntries) in Microsoft.Build.Tasks.Core.dll:token 0x60001fc+0x3a [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.FindAssociatedFiles() in Microsoft.Build.Tasks.Core.dll:token 0x6000208+0xea [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.ComputeClosure() in Microsoft.Build.Tasks.Core.dll:token 0x6000207+0xa [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.ComputeClosure(IEnumerable`1 remappedAssembliesValue, ITaskItem[] referenceAssemblyFiles, ITaskItem[] referenceAssemblyNames, List`1 exceptions) in Microsoft.Build.Tasks.Core.dll:token 0x6000206+0x38 [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.ResolveAssemblyReference.Execute(FileExists fileExists, DirectoryExists directoryExists, GetDirectories getDirectories, GetAssemblyName getAssemblyName, GetAssemblyMetadata getAssemblyMetadata, GetLastWriteTime getLastWriteTime, GetAssemblyRuntimeVersion getRuntimeVersion, GetAssemblyPathInGac getAssemblyPathInGac, IsWinMDFile isWinMDFile, ReadMachineTypeFromPEHeader readMachineTypeFromPEHeader) in Microsoft.Build.Tasks.Core.dll:token 0x60002b4+0x50f [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.Tasks.ResolveAssemblyReference.Execute() in Microsoft.Build.Tasks.Core.dll:token 0x60002c5+0x9f [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() in Microsoft.Build.dll:token 0x60015fc+0x3e [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(2203,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) in Microsoft.Build.dll:token 0x600147b+0x2b5 [c:\git\runtime\src\tests\reflection\GenericAttribute\GenericAttributeTests.csproj]

Analysis

The CorePopulateMetadata function assumes that the parent of a custom attribute on an assembly is a TypeReference. This is not a reliable assumption if the type is generic.

Versions & Configurations

I can find evidence that this failure has been in the product for some years, but it hadn't been surfaced to the msbuild team until I tried to fix issue dotnet/runtime#56492

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions