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:
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 NuGet/Home#14758.
Proposed fix
Return an empty enumerable instead of null when the collection has not been set:
IEnumerable? IAttachedCollectionSource.Items
{
get
{
_collection?.EnsureMaterialized();
return (IEnumerable?)_collection ?? Array.Empty<object>();
}
}
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.
AggregateRelationCollectionSource.Itemsreturns null when collection is not yet set, causing NRE in callersWhen
AggregateRelationCollectionSourceis constructed without anIAggregateRelationCollection(thedeferred-collection pattern used by
DependenciesAttachedCollectionSourceProviderBasesubclasses), theItemsproperty returns
null:This causes a
NullReferenceExceptionin the VS shell's internalAggregateCollection.AddSourceCollection, whichaggregates
IAttachedCollectionSourceinstances across providers for a given hierarchy item. When one provider's sourcehas 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
AssetsFileTopLevelDependenciesCollectionSourceProviderreturns anAggregateRelationCollectionSourcefor projects that don't (yet) have an assets file. The backing collection is neverset, Items stays null, and the NRE prevents our extension's
IAttachedCollectionSourceProviderfrom adding nodes to thesame
PackageDependencyhierarchy items. See NuGet/Home#14758.Proposed fix
Return an empty enumerable instead of null when the collection has not been set:
This is consistent with
HasItemsalready returning false (not null/throwing) when_collectionis null, and withIsUpdatingHasItemsreturning true to signal that items are not yet available.