Open default0 opened 9 years ago
I am facing the same issue when trying to remove 'Message' IdentifierNode from the ex?.GetBaseException.Message expression.
The stack trace is as follows:-
'parent.RemoveNode(messageNode, SyntaxRemoveOptions.KeepExteriorTrivia)' threw an exception of type 'System.ArgumentNullException'
at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.MemberAccessExpression(SyntaxKind kind, ExpressionSyntax expression, SyntaxToken operatorToken, SimpleNameSyntax name)\r\n at Microsoft.CodeAnalysis.CSharp.Syntax.MemberAccessExpressionSyntax.Update(ExpressionSyntax expression, SyntaxToken operatorToken, SimpleNameSyntax name)\r\n at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitMemberAccessExpression(MemberAccessExpressionSyntax node)\r\n at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxNodeRemover.SyntaxRemover.Visit(SyntaxNode node)\r\n at Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxNodeRemover.RemoveNodes[TRoot](TRoot root, IEnumerable1 nodes, SyntaxRemoveOptions options)\r\n at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode.RemoveNodesCore(IEnumerable
1 nodes, SyntaxRemoveOptions options)\r\n at Microsoft.CodeAnalysis.SyntaxNodeExtensions.RemoveNode[TRoot](TRoot root, SyntaxNode node, SyntaxRemoveOptions options)"
Any ETA on when this issue would be fixed?
Any ETA on when this issue would be fixed?
There is nothing to "fix" here, outside of throwing a more clear exception. What you are trying to do is illegal. A member-access-expression (i.e. expr.b
) is required to have a non-null left and non-null right. You cannot remove either side without causing an invariant exception (which manifests here as an argumentnullexception).
If you want to convert the above to ex?.GetbaseException.
, then right thing to do is to not RemoveNode on 'Message', but instead do a ReplaceNode of Message
with an empty IdentifierSyntax.
You can get a sense for what tree you need to convert the 'before' into by using a quoter tool like https://roslynquoter.azurewebsites.net/ . This will show you what the expected tree form is for something like ex?.GetbaseException.
. You will have to generate that form, otherwise the behavior of all further parts of the system is undefined.
Hi @CyrusNajmabadi I think the real bug (similar API, not identical) is that you can't tell based on the exception whether it's the first or second parameter that's the cause of the exception.
For example:
documentEditor.InsertBefore(firstMemberOfClass, declarationExpression);
@windhandel We'd welcome a PR that helped with that. Thanks!
The following Code causes an exception:
My assumption is that an AST with an ExpressionStatementSyntax that has no ExpressionSyntax is invalid, and thus an error occurs. The error message could be a lot more descriptive about that, though...
Exception: