Configuration binder source generator emits unqualified List<T> when binding arrays without implicit usings

Aperta Adatta ai principianti
#136,972 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp

Direzione di ricerca

Inizia da gen/Emitter/CoreBindingHelpers.cs, in particolare da EmitBindCoreImplForArray, che, secondo quanto indicato nell’issue, emette il tipo non qualificato List<T>. Compila la riproduzione fornita con gli using impliciti disabilitati, quindi esamina il file generato BindingExtensions.g.cs per confermare il problema. Il lavoro è completato quando il binding degli array compila senza direttive using globali; verifica anche perché i test di #121364 non hanno individuato questo caso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area-Extensions-Configuration bug source-generator untriaged
Description

The configuration binder source generator produces code that fails to compile when binding an array with implicit usings disabled.

This is a remaining case of #118630: #121364 qualified the generated HashSet<> references, but the temporary List<> used for array binding remains unqualified.

Reproduction Steps

Create these two files and run dotnet build.

repro.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder"
                      Version="11.0.0-rc.3.26522.148" />
  </ItemGroup>
</Project>

Program.cs

using Microsoft.Extensions.Configuration;

new ConfigurationBuilder().Build().Get<int[]>();
Expected behavior

The generated code compiles without requiring global using directives from the consuming project.

Actual behavior

The build fails:

BindingExtensions.g.cs(66,29): error CS0246: The type or namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?)

The generated array-binding method contains:

var temp1 = new List<int>();

The generated file neither imports System.Collections.Generic nor fully qualifies this type.

Regression?

Not a new .NET 11 regression. The original example also fails with Binder 8.0.0, 9.0.0, and 10.0.14, where the unqualified HashSet<> reference causes a compilation error first. The .NET 11 fix removes that error but leaves this array-binding failure.

Known Workarounds

Enable implicit usings, or add:

global using System.Collections.Generic;
Other information

EmitBindCoreImplForArray in gen/Emitter/CoreBindingHelpers.cs emits new List<...>() directly. Fully qualifying that reference would address this case.

When investigating this, also consider looking into why testing of #121364 didn't find this issue.

[!NOTE]
This issue was drafted with GitHub Copilot assistance.

Lingua principale
C#
Stelle
18.4k
Fork
5.8k
Merge medio
2g 15h
PR unite (30g)
596

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/runtime

Tutte le issue di dotnet/runtime

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.