stride3d/stride · error · InvalidOperationException

The name of a package reference cannot be set

Error message

The name of a package reference cannot be set

What it means

DirectDependencyReferenceViewModel.Name wraps the underlying PackageDependency's name, which is the identity key used by the dependency collection. Renaming it would desynchronize the view model from the dependency graph, so the setter throws InvalidOperationException.

Solutions

  1. Remove and re-add the dependency with the new name instead of renaming
  2. Skip Name assignment for PackageReferenceViewModel-derived nodes
  3. Add a type check before rename operations

Example fix

// before
packageRef.Name = newName;
// after
if (packageRef is DirectDependencyReferenceViewModel dep) { /* remove & re-add dependency with newName */ }
Defensive patterns

Strategy: type-guard

Validate before calling

if (item is DirectDependencyReferenceViewModel) throw new InvalidOperationException("Package reference names are read-only");

Type guard

bool CanRename(SessionItemViewModel i) => i is not DirectDependencyReferenceViewModel;

Try / catch

try { item.Name = newName; } catch (InvalidOperationException) { /* remove & re-add dependency instead */ }

Prevention

When it happens

Trigger: Assigning Name on a DirectDependencyReferenceViewModel (or through a property grid / rename command bound to it).

Common situations: Generic rename UI or scripts applying Name changes across selected session items, including package references.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/610933aa50f1d6d8. Report an issue: GitHub.

Appendix: source

Thrown at sources/editor/Stride.Core.Assets.Editor/ViewModel/PackageReferenceViewModel.cs:84

            }
        }
    }

    public class DirectDependencyReferenceViewModel : PackageReferenceViewModel
    {
        private readonly DependencyRange dependency;

        public DirectDependencyReferenceViewModel(DependencyRange dependency, PackageViewModel referencer, DependencyCategoryViewModel dependencies, bool canUndoRedoCreation)
            : base(referencer, dependencies)
        {
            this.dependency = dependency;
            InitialUndelete(canUndoRedoCreation);
        }

        public override string Name
        {
            get => dependency.Name;
            set => throw new InvalidOperationException("The name of a package reference cannot be set");
        }

        public override void AddReference()
        {
            if (!Referencer.Package.Container.DirectDependencies.Contains(dependency))
            {
                Referencer.Package.Container.DirectDependencies.Add(dependency);
            }
        }

        public override void RemoveReference()
        {
            Referencer.Package.Container.DirectDependencies.Remove(dependency);
        }
    }
}

View on GitHub (pinned to 96fad776d2)