Skip to content

Known WinRT.SourceGenerator and CsWinRT issues #1809

Open
@manodasanW

Description

@manodasanW

There are 2 known issues related to the source generator included in the Windows SDK projection package referenced when using the Windows OS version target framework in the upcoming .NET SDK release for October.

Partial type nested within non-partial type

If you have a type marked partial implementing WinRT mapped interfaces nested within a type that isn't marked partial, you may see the below compiler error instead of a diagnostic message with a code fix indicating that the outer type needs to be made partial.

CS0260 Missing partial modifier on declaration of type '..'; another partial declaration of this type exists

To address this, you can either make the outer type partial to allow the generated source to compile or use the WindowSdkPackageVersion property to explicitly reference one of the versions with the fix that makes it a diagnostic message until it is available in the .NET SDK.

Special characters in assembly name (dashes)

If you have certain special characters in your assembly name, specifically a - and you have WinRT generic instantiation scenarios, you may notice the generated code doesn't compile due to the source generator fails to escape all possible special characters not allowed in identifiers.

The errors will be from one of these files and will be something like the below:

WinRT.SourceGenerator\Generator.WinRTAotSourceGenerator\WinRTGenericInstantiation.g.cs
WinRT.SourceGenerator\Generator.WinRTAotSourceGenerator\WinRTGlobalVtableLookup.g.cs

error CS0116: A namespace cannot directly contain members such as fields, methods or statements
error CS1514: { expected
error CS1022: Type or namespace definition, or end-of-file expected
error CS0118: 'WinRT.Text' is a namespace but is used like a variable
error CS0103: The name 'GenericHelpers' does not exist in the current context

To address this, you can use the WindowSdkPackageVersion property to explicitly reference one of the versions with the fix that properly escapes the assembly name when using it in generated code.

Solution

Both issues are fixed in the below Windows SDK projection package which will be ingested in the next .NET SDK update. Until then, a project can set the WindowsSdkPackageVersion property in their csproj, in a common props file, or in Directory.Build.props to explicitly reference that version. Replace xxxxx in the property with the OS version in your .NET target framework.

If your project targets .NET 6:

<WindowsSdkPackageVersion>10.0.xxxxx.46</WindowsSdkPackageVersion>

https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.26100.46
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.22621.46
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.22000.46
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.20348.46
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.19041.46
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.18362.46
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.17763.46

If your project targets .NET 8 or later:

<WindowsSdkPackageVersion>10.0.xxxxx.47</WindowsSdkPackageVersion>

https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.26100.47
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.22621.47
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.22000.47
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.20348.47
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.19041.47
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.18362.47
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.17763.47

If your project targets .NET 8 or later and needs UWP support:

<WindowsSdkPackageVersion>10.0.xxxxx.48</WindowsSdkPackageVersion>

https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.26100.48
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.22621.48
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.22000.48
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.20348.48
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.19041.48
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.18362.48
https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/10.0.17763.48

If you run into any other errors from code generated by WinRT.SourceGenerator, please open an issue in the CsWinRT repo.

Type redirection

If you run into type conflict errors between Microsoft.WinUI and Microsoft.Windows.SDK.NET.dll, see the CsWinRT 2.1.1 release notes for details about the forwarded types for WinAppSDK 1.6.

Obsolete errors

If you run into the error below indicating that ObjectReferenceWrapperAttribute has been obsolete, you can update to the latest CsWinRT version (2.1.x) to generate your projection which doesn't rely on it. If you are unable to move to the latest CsWinRT version, you can choose to fix your version of the Windows SDK projection to an older version before CsWinRT 2.1.x such as the .34 version by specifying the WindowsSdkPackageVersion property in your csproj or in a common props file until you are able to update.

'ObjectReferenceWrapperAttribute is obsolete.' This attribute is only used for the .NET Standard 2.0 projections.
<WindowsSdkPackageVersion>10.0.xxxxx.34</WindowsSdkPackageVersion>

** replace xxxxx with the OS version that your target in your target framework.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions