Fix NullReferenceException in InsertAdditionalImportFeature.cs in Unity 2022
Problem
This problem occurs when using CsprojModifier with Unity 2022.
In the following code, projectE was null.
var nsMsbuild = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003";
var projectE = xDoc.Element(nsMsbuild + "Project");
Fix
As shown below, the xmlns attribute was missing from the Unity 2022 csproj file.
In light of this, I have fixed it so that it works correctly even when the xmlns attribute is missing.
var nsMsbuild = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003";
var projectE = xDoc.Element(nsMsbuild + "Project") ?? xDoc.Element("Project");
It works for me, Unity 6000.0.3f
c77a330b6d75a9033c6129af281ed323477b379f
Unity Editor 2022.3.45f1 was used, which caused further errors and has been re-fixed.
The following namespace-using sections caused a general error and have been corrected to not use namespaces.
var nsMsbuild = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003";
@guitarrapc @mayuki I apologize for the abruptness of this communication. I have not received any response to this PR for a while, so I would appreciate it if you could check if it is possible.
I apologize for the delay in responding.
I checked into this issue and found that it only occurs when the External Editor is set to "Visual Studio Code". In other words, if I merge this fix, it will cause an error for Visual Studio and Rider.
Additionally, when merging XML using “Append/Prepend Content”, you will also need to make sure that the namespaces of both files match.
I apologize for the delay in responding.
I checked into this issue and found that it only occurs when the External Editor is set to "Visual Studio Code". In other words, if I merge this fix, it will cause an error for Visual Studio and Rider.
Additionally, when merging XML using “Append/Prepend Content”, you will also need to make sure that the namespaces of both files match.
I have used Rider, but it still works for me, can be this fix create errors in future, it just help info.
When I checked out the PR branch, specified “External Script Editor” as "Rider 2024.2.1", and generated a csproj, I got the same error as when using Visual Studio.
- Unity 2022.3.39f1
- JetBrains Rider Editor 3.0.31 (Unity Package Manager)
This is because the editor extension only outputs csproj in SDK-style project format when "Visual Studio Code" is specified, and it seems that the classic csproj (with explicit namespace specification) is currently output for other IDEs.
@mayuki Thank you for your response!
I did not do enough research to find out that the issue occurs only in VisualStudioCode. Thank you for pointing this out. Since you mentioned that the error occurs when using Visual Studio or Rider as it is, I would like to think about how to fix it once again.
Hi! I noticed this PR has been inactive for a while, and I think it's a valuable contribution. I've worked on adding support for both classic and SDK-style csproj formats. I would be happy to open a new PR based on this if that's ok with @tigrig29 and maintainers.
@mezum Thank you very much! Since I’ve been unable to work on this for a while, I would appreciate it if you could open a new PR and proceed with the improvements. Best regards.
Consolidate to #20