- Notifications
You must be signed in to change notification settings - Fork 412
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:
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.