CsprojModifier icon indicating copy to clipboard operation
CsprojModifier copied to clipboard

Fix NullReferenceException in InsertAdditionalImportFeature.cs in Unity 2022

Open tigrig29 opened this issue 1 year ago • 6 comments

Problem

image

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.

image

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");

tigrig29 avatar Apr 01 '24 12:04 tigrig29

It works for me, Unity 6000.0.3f

Niek352 avatar May 30 '24 21:05 Niek352

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.

tigrig29 avatar Sep 12 '24 14:09 tigrig29

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.

mayuki avatar Sep 13 '24 03:09 mayuki

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.

Niek352 avatar Sep 13 '24 06:09 Niek352

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.

image

  • 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 avatar Sep 13 '24 07:09 mayuki

@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.

tigrig29 avatar Sep 13 '24 13:09 tigrig29

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 avatar Nov 23 '24 06:11 mezum

@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.

tigrig29 avatar Nov 23 '24 06:11 tigrig29

Consolidate to #20

tigrig29 avatar Nov 23 '24 11:11 tigrig29