Skip to content

Scriban source include conflicts with Polyfill package due to duplicate AOT/trimming attributes #665

@ncipollina

Description

@ncipollina

Describe the bug

When using Scriban with PackageScribanIncludeSource=true in a netstandard2.0 source generator project, builds fail with multiple duplicate type/attribute errors when the project also references the Polyfill NuGet package.

The errors are caused by both Scriban and Polyfill providing implementations of trimming/AOT-related attributes such as:

  • DynamicallyAccessedMembersAttribute
  • RequiresDynamicCodeAttribute
  • RequiresUnreferencedCodeAttribute
  • UnconditionalSuppressMessageAttribute
  • DynamicallyAccessedMemberTypes

This results in compiler errors like:

  • CS0101: namespace already contains a definition
  • CS0579: duplicate attribute
  • CS0121 / CS0229: ambiguous calls

To Reproduce

Project setup:

  • Target framework: netstandard2.0
  • Project type: Roslyn source generator
  • Scriban: 7.0.5
  • Polyfill: 9.23.0

Relevant csproj configuration:

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
  <PackageScribanIncludeSource>true</PackageScribanIncludeSource>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Scriban" Version="7.0.5" IncludeAssets="build" PrivateAssets="all" />
  <PackageReference Include="Polyfill" Version="9.23.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
</ItemGroup>

Result:
Build fails with dozens of duplicate type/attribute errors due to overlapping definitions between:

  • Scriban source-included file: AotAttributes.cs
  • Polyfill content files under Trimming/*.cs

Expected behavior

Scriban source include should coexist with common polyfill packages without requiring manual exclusion of files.

Actual behavior

Compilation fails due to duplicate definitions of trimming/AOT attributes.

Workaround

Manually excluding the overlapping Polyfill files resolves the issue:

<ItemGroup>
  <Compile Remove="$(NuGetPackageRoot)polyfill\9.23.0\contentFiles\cs\netstandard2.0\Trimming\DynamicallyAccessedMembersAttribute.cs" />
  <Compile Remove="$(NuGetPackageRoot)polyfill\9.23.0\contentFiles\cs\netstandard2.0\Trimming\DynamicallyAccessedMemberTypes.cs" />
  <Compile Remove="$(NuGetPackageRoot)polyfill\9.23.0\contentFiles\cs\netstandard2.0\Trimming\RequiresDynamicCodeAttribute.cs" />
  <Compile Remove="$(NuGetPackageRoot)polyfill\9.23.0\contentFiles\cs\netstandard2.0\Trimming\RequiresUnreferencedCodeAttribute.cs" />
  <Compile Remove="$(NuGetPackageRoot)polyfill\9.23.0\contentFiles\cs\netstandard2.0\Trimming\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>

Additional context

This scenario is likely to affect source generator authors targeting netstandard2.0, where:

  • Source inclusion (PackageScribanIncludeSource=true) is preferred to avoid dependency leakage
  • Polyfill packages are commonly used to provide missing BCL features

Because both Scriban and Polyfill independently include the same AOT/trimming attributes, they are not composable without manual intervention.

Suggested improvement (optional)

Consider one of the following:

  • Provide a compile-time switch (e.g., SCRIBAN_NO_AOT_ATTRIBUTES) to disable Scriban’s attribute definitions
  • Conditionally include attributes only if they are not already defined
  • Document this interaction with Polyfill and similar packages

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions