dotnet / dotnet/roslyn

Error during Code Cleanup: The node is not part of the tree

Open
#79,570 0 comments 1 reaction 0 assignees View on GitHub
Area-IDE untriaged
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/Error-during-Code-Cleanup:-The-node-is-n/10831822)._

---
[severity:It's more difficult to complete my work]
**Summary**
In Visual Studio, when I run Code Cleanup on my project, I get an error and the Code Cleanup fails to fix anything. It happens every time on this project. I've narrowed down the lines that cause the problem to just a few lines and created a simple project to demonstrate the problem. If I remove those lines from my real project, leaving thousands of other lines, I don't get the error, so there's something with these specific lines of code that cause the error.

**To reproduce:**
1) Create a new Console project with .NET9, storing the solution in c:\TestSolution\
2) Edit the file Program.cs to have these contents:

```
class Program
{
static void Main()
{
List<MyClass1> myClasses = new();

foreach (MyClass1 var1 in myClasses)
{
var1.MyProperty = null;
}
}
}

public class MyClass2 { }
```
3) Add a file MyClass1.cs with these contents:

```
public class MyClass1
{
public MyClass2 MyProperty { set { } }
}
```

4) Click Configure Code Cleanup and configure Profile 1 to include only these 2 items:
A) Fix all warnings and errors set in EditorConfig
B) Fix analyzer warnings and errors set in EditorConfig

5) Run that Code Cleanup profile on your solution. It should fail quickly with this error message popup:
>Microsoft Visual Studio
> The node is not part of the tree.
> Parameter name: node

**Notes**
Windows 11 Pro 24H2
Visual Studio 2022 17.12.4
This happens regardless of whether a file named ".editorconfig" exists in the solution folder or project folder.

**Demo project:**
[DemoProject.zip](https://aka.ms/dc/file?name=B4efe43caca2349918f28c14e1fb78a37638731108275808155_DemoProject.zip&tid=4efe43caca2349918f28c14e1fb78a37638731108275808155)

---
### Original Comments

#### Feedback Bot on 1/22/2025, 07:08 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

#### Feedback Bot on 1/22/2025, 09:27 AM:

This issue is currently being investigated. Our team will get back to you if either more information is needed, a workaround is available, or the issue is resolved.

#### Mark Davich on 2/12/2025, 08:47 PM:

Hi Jeremy Arnold, I was also having this issue in a project with a lot of large files. Thanks to your DemoProject.zip and and reproduction steps I was able to identify which of the thousands of errors and warnings was causing this to happen in the project.


I added three workarounds to this post that address different use cases.

---
### Original Solutions

#### Mark Davich solved on 2/12/2025, 08:42 PM, undefined votes:

I was experiencing this exact issue. I worked around it by addressing the

CS8625 - Cannot convert null literal to non-nullable reference type warning.


The Problem


image.png


Workaround One


Use the null-forgiving operator ! to tell the compiler that the null is expected



By using the null-forgiving operator, you inform the compiler that passing null is expected and shouldn’t be warned about. – Microsoft



// Program.cs

class Program
{
...
foreach (MyClass1 var1 in myClasses)
{
var. MyProperty = null!; // Use the null-forgiving operator
}

Workaround Two


Assign an instance to of MyClass2 to MyProperty


class Program

{
static void Main()
{
List<MyClass1> myClasses = new();

foreach (MyClass1 var1 in myClasses)
{
var1. MyProperty = new MyClass2(); // Assign an instance of MyClass2 to the non-nullable MyProperty
}
}
}

public class MyClass2 { }


Workaround Three


Make MyClass1.MyProperty nullable


// MyClass1.cs

public class MyClass1
{
public MyClass2? MyProperty { set { } }
}

// Program.cs
class Program
{
...
foreach (MyClass1 var1 in myClasses)
{
var. MyProperty = null;
}
}

Contributor guide

Open the contributing guide

Research direction

Start by opening the linked DemoProject.zip and reproducing the failure in Program.cs and MyClass1.cs with a .NET 9 Console project. Run the configured Code Cleanup profile containing the two EditorConfig items and compare the behavior with the listed nullable-warning workarounds. Done means Code Cleanup completes without the “The node is not part of the tree” error and applies its fixes.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.