godot: Generating the C# project fails when .Net 8 Sdk is installed.

Godot version

4.1.1.stable.mono

System information

Godot v4.1.1.stable.mono - Windows 10.0.23531

Issue description

When I install .Net 8 on my system (I’m using the preview version of Visual Studio, so I can’t uninstall .Net 8), I get the message “Failed to create C# project.” when I create a C# script file, and it doesn’t generate the corresponding project files.

modules/mono/glue/runtime_interop.cpp:1324 - System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. System cannot find the specified file.
  File name: 'System.Runtime, Version=8.0.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
  Transient parent has another exclusive child.

Steps to reproduce

  1. Install the latest preview version of .Net 8 Sdk.
  2. Create a new godot project.
  3. Add a scene root node.
  4. Attach a C# script to the node.

Minimal reproduction project

N/A

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 23 (9 by maintainers)

Most upvoted comments

Is there somewhere I can track the Godot/.net8 integration progress?

Godot currently supports net6.0 and net7.0, I don’t think net8.0 has been tested yet.

TBH, <TargetFramework>net8.0</TargetFramework> is a no-go because it triggers the exact same error as reported by OP:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at Godot.Bridge.ScriptManagerBridge.LookupScriptsInAssembly(Assembly assembly) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 322
   at GodotPlugins.Main.LoadProjectAssembly(Char* nAssemblyPath, godot_string* outLoadedAssemblyPath) in /root/godot/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs:line 154
ERROR: .NET: Failed to load project assembly.
   at: reload_project_assemblies (modules/mono/mono_gd/gd_mono.cpp:527)

Saw the same error, v4.2.2 rc1, was fixed after a cache clear with dotnet nuget locals -c

@tbhb It doesn’t seem like your output contains the Found .NET Sdk version message, which makes me think it’s a different issue. The original issue was about generating a C# project, is that also the case for you? I would recommend opening a new issue so we can follow-up on your specific issue.

@raulsntos Ah my apologies here, my case was actually a bit different. I was trying to set my TargetFramework to net8.0 to try out some things in 8.0.0.rc2 😃 Your comment in https://github.com/godotengine/godot/issues/83915#issuecomment-1779661812 actually fixed things for me though, setting DOTNET_ROLL_FORWARD_TO_PRERELEASE and now I’m able to run my .NET 8-compiled assembly!

I have the same issue in Godot 4.2 Beta 1 with .NET SDK 8.0.100-rc.2.23502.2, even after cleaning NuGet cache. It only occurs in Godot projects and standalone .NET projects work well.

perhaps we could consider having Godot use the same versions of hostfxr and Sdk

That’s what it tries to do first, it looks for the same major version and only falls back to something else if it can’t find it. I think this is happening because the version 8.0.100-rc.1.23455.8 can’t be parsed by Version.TryParse.

https://github.com/godotengine/godot/blob/e3e2528ba7f6e85ac167d687dd6312b35f558591/modules/mono/editor/GodotTools/GodotTools/Build/DotNetFinder.cs#L100-L101

the code that creates the project seems to be independent of the Sdk version

The C# project is created using the Microsoft.Build APIs. I believe this requires registering the SDK.

So yep, seems to be related to the Visual Studio update replacing the .NET SDK 7 with version 8:

.NET SDKs installed:
  5.0.101 [C:\Program Files\dotnet\sdk]
  5.0.203 [C:\Program Files\dotnet\sdk]
  8.0.100-rc.1.23455.8 [C:\Program Files\dotnet\sdk]

Manually reinstalling 7.0x in addition fixed the issue:

.NET SDKs installed:
  5.0.101 [C:\Program Files\dotnet\sdk]
  5.0.203 [C:\Program Files\dotnet\sdk]
  7.0.401 [C:\Program Files\dotnet\sdk]
  8.0.100-rc.1.23455.8 [C:\Program Files\dotnet\sdk]