dotnet / dotnet/roslyn

CS8625 code fix fails when declaration to fix is in another file

Open
#74,121 1 comment 0 reactions 0 assignees View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**: .NET 8.0.302

**Steps to Reproduce**:

1. Have code where code in one file sets a field/property in another file to `null` , e.g.

**Parent.cs**

```csharp
public class Parent
{
public void M(Child c)
{
c.next = null;
c.Next = null;
}
}
```

**Child.cs**

```
public sealed class Child
{
internal Child next;
public Child Next { get => next; set => next = value; }
}
```

2. Place the cursor on either of the `null` literals where CS8625 is raised.
3. Apply the suggested "Declare as nullable" code fix

**Diagnostic Id**: CS8625: Cannot convert null literal to non-nullable reference type.

**Expected Behavior**:

The code fix should append a `?` to the field/property type in the other file.

**Actual Behavior**:

```
System.ArgumentException : The node is not part of the tree.
Parameter name: node
at Microsoft.CodeAnalysis.Editing.SyntaxEditor.CheckNodeInOriginalTree(SyntaxNode node)
at Microsoft.CodeAnalysis.Editing.SyntaxEditor.ReplaceNode(SyntaxNode node,SyntaxNode newNode)
at async Microsoft.CodeAnalysis.CSharp.CodeFixes.DeclareAsNullable.CSharpDeclareAsNullableCodeFixProvider.FixAllAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeFixes.SyntaxEditorBasedCodeFixProvider.FixAllWithEditorAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass16_0.b__0()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T]()
```

Reproducible in both VS Code and Visual Studio 2022.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the Parent.cs and Child.cs example, then start at CSharpDeclareAsNullableCodeFixProvider.FixAllAsync in the stack trace. Trace how the field or property declaration is located across files and verify that applying the fix no longer raises the SyntaxEditor exception and appends ? to the declaration type.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.