episerver / upgrade-assistant-extensions

Apache License 2.0
10 stars 6 forks source link

EP0008 NullReferenceException #43

Closed rbottema closed 1 year ago

rbottema commented 1 year ago

Hi,

When running the Upgrade Assistant on our project we got the following exception:

[10:23:45 ERR] Unexpected error applying step
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxNodeRemover.SyntaxRemover.<>c.<.ctor>b__5_0(SyntaxNode n)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxNodeRemover.SyntaxRemover..ctor(SyntaxNode[] nodesToRemove, SyntaxRemoveOptions options)
   at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxNodeRemover.RemoveNodes[TRoot](TRoot root, IEnumerable`1 nodes, SyntaxRemoveOptions options)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode.RemoveNodesCore(IEnumerable`1 nodes, SyntaxRemoveOptions options)
   at Epi.Source.Updater.EpiMetadataAwareCodeFixProvider.RefactorToDisplayModeProvider(Document document, ClassDeclarationSyntax node, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(IProgressTracker progressTracker, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CodeActions.CodeAction.GetOperationsCoreAsync(IProgressTracker progressTracker, CancellationToken cancellationToken)
   at Microsoft.DotNet.UpgradeAssistant.Steps.Source.CodeFixerStep.TryFixDiagnosticAsync(Diagnostic diagnostic, Document document, CancellationToken token)
   at Microsoft.DotNet.UpgradeAssistant.Steps.Source.CodeFixerStep.ApplyImplAsync(IUpgradeContext context, CancellationToken token)
   at Microsoft.DotNet.UpgradeAssistant.UpgradeStep.ApplyAsync(IUpgradeContext context, CancellationToken token) in /_/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/UpgradeStep.cs:line 203

Skipping the step gives a new error:

[10:26:08 INF] Applying upgrade step Apply fix for EP0008: Convert IMetadataAware to IDisplayMetadataProvider
[10:26:08 ERR] Unexpected error applying step
System.ArgumentNullException: Value cannot be null. (Parameter 'document')
   at Microsoft.DotNet.UpgradeAssistant.Steps.Source.CodeFixerStep.TryFixDiagnosticAsync(Diagnostic diagnostic, Document document, CancellationToken token)
   at Microsoft.DotNet.UpgradeAssistant.Steps.Source.CodeFixerStep.ApplyImplAsync(IUpgradeContext context, CancellationToken token)
   at Microsoft.DotNet.UpgradeAssistant.UpgradeStep.ApplyAsync(IUpgradeContext context, CancellationToken token) in /_/src/common/Microsoft.DotNet.UpgradeAssistant.Abstractions/UpgradeStep.cs:line 203
Command (Apply next step (Apply fix for EP0008: Convert IMetadataAware to IDisplayMetadataProvider)) did not succeed

After some research I think it's likely because the class uses a fully qualified class name (like in #34). A trimmed version of the class is as follows:

public class FooSelectionFactory : ISelectionFactory, System.Web.Mvc.IMetadataAware
{        
    public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
    {
        return Enumerable.Empty<ISelectItem>();
    }

    public void OnMetadataCreated(System.Web.Mvc.ModelMetadata metadata)
    {
        ((ExtendedMetadata)metadata).ClientEditingClass = "epi-cms/contentediting/editors/CheckBoxListEditor";
        ((ExtendedMetadata)metadata).EditorConfiguration["style"] = "width: 100%";
        ((ExtendedMetadata)metadata).EditorConfiguration["class"] = "selectmanyfullwidth";
    }
}
shahramshahram commented 1 year ago

Thanks for reporting. We will look into

shahramshahram commented 1 year ago

I posted a workaround for the problem until we fix it. You can remove the qualified name from System.Web.Mvc.IMetadataAware. like IMetadataAware.

using System.Web.Mvc; public class FooSelectionFactory : ISelectionFactory, IMetadataAware {
public IEnumerable GetSelections(ExtendedMetadata metadata) { return Enumerable.Empty(); }

public void OnMetadataCreated(ModelMetadata metadata)
{
    ((ExtendedMetadata)metadata).ClientEditingClass = "epi-cms/contentediting/editors/CheckBoxListEditor";
    ((ExtendedMetadata)metadata).EditorConfiguration["style"] = "width: 100%";
    ((ExtendedMetadata)metadata).EditorConfiguration["class"] = "selectmanyfullwidth";
}

}

thanks.

shahramshahram commented 1 year ago

@rbottema pls take the latest version and the problem should be fixed. https://github.com/episerver/upgrade-assistant-extensions/releases/tag/v1.0.33

rbottema commented 1 year ago

Awesome, thanks for the quick workaround and fix!