Skip to content

Error in ImmutableArray initialization using C#12 collection literals #92022

@eduherminio

Description

@eduherminio

Description

ImmutableArrays are throwing an exception when being initialized at runtime in some circumstances:

  • When debugging the project from Visual Studio
  • When publishing the project (self-contained, trimmed, single-file in my case).

However, they work from CLI

This can be especially painful because tests pass CI but then everything collapses at runtime initialization. Real life CI example:

Simplified example below:

Reproduction Steps

dotnet new console -n immutable

Program.cs

using System.Collections.Immutable;

Console.WriteLine($"Hello, World from {Constants.Coordinates[0]}");


public static class Constants
{
    public static readonly ImmutableArray<string> Coordinates =
    [
        "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
        "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
        "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
        "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
        "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
        "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
        "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
        "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"
    ];
}

immutable.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <LangVersion>preview</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Expected behavior

Runs normally, prints this in all the cases

Hello, World from a8

Actual behavior

Failes from Visual Studio or when the project is published:

$> dotnet run -c Release
Hello, World from a8
$> dotnet run -c Debug
Hello, World from a8
$> dotnet publish -c Release --self-contained /p:PublishSingleFile=true /p:PublishTrimmed=true --runtime win-x64 -o ./output && output/immutable.exe
Unhandled exception. System.TypeInitializationException: The type initializer for 'Constants' threw an exception.
 ---> System.TypeLoadException
   at Constants..cctor()
   --- End of inner exception stack trace ---
   at Program.<Main>$(String[]) in C:\dev\tmp\immutable\Program.cs:line 3
C:\dev\tmp\immutable>

From Visual Studio, while debugging:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Collections.Immutable
  StackTrace:
   at System.Collections.Immutable.ImmutableArray`1.Add(T item)
   at Constants..cctor() in C:\dev\tmp\immutable\Program.cs:line 8

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK:
 Version:   8.0.100-rc.1.23455.8
 Commit:    e14caf947f

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100-rc.1.23455.8\

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.0-rc.1.23419.4
  Architecture: x64
  Commit:       92959931a3
  RID:          win-x64

Microsoft Visual Studio Enterprise 2022 (64-bit) - Version 17.7.0

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLink.NET linker development as well as trimming analyzers

    Type

    No type

    Projects

    Status

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions