godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.48k stars 21.26k forks source link

[dotnet] Cannot create or build C# solution after Visual Studio upgraded to 17.4.0 and .NET 7 #68652

Closed zaevi closed 1 year ago

zaevi commented 2 years ago

Godot version

4.0.beta4.mono, master(.mono)

System information

Win11, dotnet6 and dotnet7 installed

Issue description

When building or creating c# solution on Godot editor, it couldn't find correct msbuild and throw like this:

ERROR: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 系统找不到指定的文件。
File name: 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at GodotTools.ProjectEditor.ProjectGenerator.GenAndSaveGameProject(String dir, String name)
   at GodotTools.CsProjOperations.GenerateGameProject(String dir, String name) in /root/godot/modules/mono/editor/GodotTools/GodotTools/CsProjOperations.cs:line 13
   at: godotsharp_pusherror (modules/mono/glue/runtime_interop.cpp:1240)

dotnet info:

.NET SDKs installed:
  6.0.302 [C:\Program Files\dotnet\sdk]
  7.0.100 [C:\Program Files\dotnet\sdk]
$ msbuild --version
MSBuild version 17.4.0+18d5aef85 for .NET Framework
17.4.0.51802

Steps to reproduce

Install dotnet 7 (assuming you have .net6 installed).

Create new project and create C# solution on Godot editor.

Minimal reproduction project

No response

raulsntos commented 2 years ago

I can't reproduce it in Linux, maybe it's specific to Windows. If you open Godot with the --verbose argument, what .NET Sdk does it report to have found?

In my case, it uses hostfxr from .NET 7.0 and the Sdk from .NET 6.0:

Found hostfxr: /usr/share/dotnet/host/fxr/7.0.0/libhostfxr.so
Found .NET Sdk version '6.0.403': /usr/share/dotnet/sdk/6.0.403

In order to avoid including all of MSBuild in Godot, we use the MSBuild.Locator which should find MSBuild in your current installation and use that instead. For some reason it seems to be trying to load MSBuild, Version 15.1.0.0. Maybe something went wrong when registering the MSBuild path.

To register the MSBuild path we first try to find it ourselves using our DotNetFinder implementation which looks in the output of the dotnet --list-sdks command (same as the .NET SDKs installed part of dotnet --info).

For reference, here's the output of dotnet --list-sdks for me:

6.0.403 [/usr/share/dotnet/sdk]
7.0.100 [/usr/share/dotnet/sdk]
nowheredevel commented 2 years ago

Also experiencing the same issue, attempting to create a C# script with --verbose enabled just prints out the same thing zaevi received. I also have the same SDK and MSBuild versions.

raulsntos commented 2 years ago

@nowheredevel I was asking specifically for the lines that say Found hostfxr and Found .NET Sdk version to know which version Godot finds and tries to use. Also, I'm assuming you are also on Windows 11?

nowheredevel commented 2 years ago

Ohhh gotcha. Ran again, on project load it says Found hostfxr: C:\Program Files\dotnet\host/fxr/7.0.0/hostfxr.dll .NET: hostfxr initialized and Found .NET Sdk version '6.0.401': C:\Program Files\dotnet\sdk\6.0.401. I'm on Windows 10, but I don't think it will make that much of a difference.

zaevi commented 2 years ago

@raulsntos

.NET: Initializing module...
Found hostfxr: C:\Program Files\dotnet\host/fxr/7.0.0/hostfxr.dll
.NET: hostfxr initialized
.NET: GodotPlugins initialized
.NET: Failed to load project assembly
...
Found .NET Sdk version '6.0.302': C:\Program Files\dotnet\sdk\6.0.302

Seems find the correct SDK, but I'm not sure about MSBuild.

zaevi commented 2 years ago

Okay I finally solve this by reinstalling .NET 6 SDK manually.

I use Visual Studio 2022 to install and manage .NET SDK. And several days ago I upgrade VS to 17.4.0 and it brings .NET SDK 7.

Back to this issue, the path that editor found (C:\Program Files\dotnet\sdk\6.0.302) is an empty folder now, so I guess when VS is upgrading, it removes some components from .NET 6.0.302 (but I can still use .NET 6.0.302 to create or build .net project in VS).

So I download and install .net sdk (6.0.403) manually, and it works...

lufog commented 2 years ago

I noticed, if the project has a .sln file (generated before VS 17.4 update), then it can be compiled in Visual Studio even without the .Net 6 SDK. You can run the project from Visual Studio by setting up a custom Launch Profile: [OC] Run+Debug Godot4 C# projects from Visual Studio

Video https://user-images.githubusercontent.com/10379487/203366041-00b7deae-1e21-499d-b438-f56f7c3e4e99.mp4

Perhaps it's dotnet CLI related problem, like this one: https://github.com/dotnet/sdk/issues/28942

akien-mga commented 1 year ago

Crashes with .NET 7 specifically should be fixed by #71825.