dotnet / dotnet/project-system

AggregateRelationCollectionSource.Items returns null when collection is not yet set, causing NRE in callers

Open
#9,889 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1k
Forks
415
PR merge metrics
No merged PRs in 30d

Description

`AggregateRelationCollectionSource.Items` returns null when collection is not yet set, causing NRE in callers

When `AggregateRelationCollectionSource` is constructed without an `IAggregateRelationCollection` (the
deferred-collection pattern used by `DependenciesAttachedCollectionSourceProviderBase` subclasses), the `Items`
property returns `null`:

```csharp
IEnumerable? IAttachedCollectionSource.Items
{
get
{
_collection?.EnsureMaterialized();
return _collection; // null when SetCollection has not been called
}
}
```

This causes a `NullReferenceException` in the VS shell's internal `AggregateCollection.AddSourceCollection`, which
aggregates `IAttachedCollectionSource` instances across providers for a given hierarchy item. When one provider's source
has null Items, the NRE prevents all other providers' sources from being added to the aggregate — effectively
blocking other VS extensions from attaching their own child nodes to that hierarchy item.

The concrete case we hit: NuGet's `AssetsFileTopLevelDependenciesCollectionSourceProvider` returns an
`AggregateRelationCollectionSource` for projects that don't (yet) have an assets file. The backing collection is never
set, Items stays null, and the NRE prevents our extension's `IAttachedCollectionSourceProvider` from adding nodes to the
same `PackageDependency` hierarchy items. See https://github.com/NuGet/Home/issues/14758.

Proposed fix

Return an empty enumerable instead of null when the collection has not been set:

```csharp
IEnumerable? IAttachedCollectionSource.Items
{
get
{
_collection?.EnsureMaterialized();
return (IEnumerable?)_collection ?? Array.Empty();
}
}
```

This is consistent with `HasItems` already returning false (not null/throwing) when `_collection` is null, and with
`IsUpdatingHasItems` returning true to signal that items are not yet available.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.