Skip to content

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

@kfertitta

Description

@kfertitta

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions