Skip to content

LibraryImport fails with MarshalAsAttribute errors when return value or parameter types are from another assembly #112589

Open
@leandro-benedet-garcia

Description

@leandro-benedet-garcia

Description

While making a binding using SDL3-CS as a reference project I found a very weird error:

/bug_project/bug.cs(21,35): error SYSLIB1052: The specified 'MarshalAsAttribute' configuration for the return value of method 'SDL_ShaderCross_Init' is not supported by source-generated P/Invokes. If the specified configuration is required, use a regular 'DllImport' instead. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1052)

The binding in question is:

[LibraryImport("SDL3_shadercross")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial SDLBool SDL_ShaderCross_Init();

The declaration of SDLBool can be found here https://github.com/flibitijibibo/SDL3-CS/blob/42d1b438aabeabd68385cba3ed5c21e73e8cc877/SDL3/SDL3.Core.cs#L44-L75

Reproduction Steps

Minimum reproducible project:

binding_bug.zip

Expected behavior

I would expect the code to work with and without the project reference.

Or at least, be told that I cannot use bindings from a referenced project.

Actual behavior

A confusing error message.

Regression?

No response

Known Workarounds

If I instead remove the reference and add this to the project file:

<ItemGroup>
  <Compile Include="lib\SDL3-CS\SDL3\SDL3.Core.cs" />
</ItemGroup>

The code compiles fine.

Configuration

  • Which version of .NET is the code running on?
    Dotnet version 9.0.102
  • What OS and version, and what distro if applicable?
    Arch Linux kernel version 6.13.2-arch1-1
  • What is the architecture (x64, x86, ARM, ARM64)?
    x86_64
  • Do you know whether it is specific to that configuration?
    Unsure

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions