microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2k stars 84 forks source link

Additional metadata file fails with BadImageFormatException or bad namespace syntax #999

Closed AArnott closed 11 months ago

AArnott commented 11 months ago

Check out https://github.com/youyuanwu/service-fabric-cs/issues/1 for a repro.

With marshaling

Message: 
Assert.Empty() Failure
Expected: <empty>
Actual:   [ServiceFabric.NativeMethods.g.cs(16,37): error CS0234: The type or namespace name 'ServiceFabric' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)]

  Stack Trace: 
GeneratorTestBase.AssertNoDiagnostics(CSharpCompilation compilation, Boolean logAllGeneratedCode) line 235
GeneratorTestBase.AssertNoDiagnostics(Boolean logAllGeneratedCode) line 202
MultiMetadataTests.BasicServiceFabric(Boolean allowMarshaling) line 17

  Standard Output: 
Emitted 2 syntax trees totalling 5,326 in size.
The largest syntax trees are:
      4,229 Windows.Win32.NativeMethods.g.cs
      1,097 ServiceFabric.NativeMethods.g.cs
1 diagnostics reported.
ServiceFabric.NativeMethods.g.cs(16,37): error CS0234: The type or namespace name 'ServiceFabric' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Without marshaling

 MultiMetadataTests.BasicServiceFabric(allowMarshaling: False)
   Source: MultiMetadataTests.cs line 12
   Duration: 876 ms

  Message: 
Microsoft.Windows.CsWin32.GenerationFailedException : Failed to generate IFabricStringResult (unmanaged)
---- System.BadImageFormatException : Read out of bounds.

  Stack Trace: 
Generator.RequestInteropTypeHelper(TypeDefinitionHandle typeDefHandle, Context context) line 1271
<>c__DisplayClass117_0.<RequestInteropType>b__0() line 883
GeneratedCode.GenerateType(TypeDefinitionHandle typeDefinitionHandle, Boolean hasUnmanagedName, Action generator) line 293
Generator.RequestInteropType(TypeDefinitionHandle typeDefHandle, Context context) line 881
<>c__DisplayClass107_0.<TryGenerateType>b__1() line 536
GeneratedCode.GenerationTransaction(Action generator) line 200
Generator.TryGenerateType(String possiblyQualifiedName, IReadOnlyList`1& preciseApi) line 534
Generator.TryGenerate(String apiNameOrModuleWildcard, IReadOnlyList`1& preciseApi, CancellationToken cancellationToken) line 364
SuperGenerator.TryGenerate(String apiNameOrModuleWildcard, IReadOnlyList`1& preciseApi, IReadOnlyList`1& redirectedEnums, CancellationToken cancellationToken) line 76
SuperGenerator.TryGenerate(String apiNameOrModuleWildcard, IReadOnlyList`1& preciseApi, CancellationToken cancellationToken) line 62
GeneratorExtensions.TryGenerate(IGenerator generator, String apiNameOrModuleWildcard, CancellationToken cancellationToken) line 12
MultiMetadataTests.BasicServiceFabric(Boolean allowMarshaling) line 15
----- Inner Stack Trace -----
Throw.OutOfBounds()
MemoryBlock.PeekUtf8NullTerminated(Int32 offset, Byte[] prefix, MetadataStringDecoder utf8Decoder, Int32& numberOfBytesRead, Char terminator)
StringHeap.GetNonVirtualString(StringHandle handle, MetadataStringDecoder utf8Decoder, Byte[] prefixOpt)
StringHeap.GetString(StringHandle handle, MetadataStringDecoder utf8Decoder)
MetadataReader.GetString(StringHandle handle)
Generator.TryGetPropertyAccessorInfo(MethodDefinition methodDefinition, String ifaceName, Context context, IdentifierNameSyntax& propertyName, Nullable`1& accessorKind, TypeSyntax& propertyType) line 917
Generator.GetDeclarableProperties(IEnumerable`1 methods, String ifaceName, Boolean allowNonConsecutiveAccessors, Context context) line 870
Generator.DeclareInterfaceAsStruct(TypeDefinitionHandle typeDefHandle, ImmutableStack`1 baseTypes, Context context) line 137
Generator.DeclareInterface(TypeDefinitionHandle typeDefHandle, Context context) line 81
Generator.RequestInteropTypeHelper(TypeDefinitionHandle typeDefHandle, Context context) line 1203